Search code examples
cvariablesscopeselection-sort

Local variable loses its value: selection sort algorithm


I previously tested the same variable called "swaps" for bubble sort algorithm and it worked perfectly. Now, with selection sorting, variable loses its value even after incrementing it. Any help will be very much appreciated.

int list[] = {10, 5, 6, 3, 4, 11, 9, 7, 2};
int min = list[0], pos = 0, temp_max = 0;

// Loop until no swap is needed
for (int j = 0, n = sizeof(list) / sizeof(int); j < n; j++)
{
    int swaps = 0, 

    // Iterate through array to find min value
    for (int i = j, y = sizeof(list) / sizeof(int); i < y; i++)
    {
        if (list[i] < min)
        {
            min = list[i];
            pos = i;
        }
    }

    // Insert min value in left most position and add 1 to swaps, meaning array is not yet sorted
    if (pos > j)
    {
        temp_max = list[j];
        list[j] = min;
        list[pos] = temp_max;
        swaps++;
    }

    // The error might occur here: "swaps" keeping value 0 after previous if statement ends
    printf ("swaps = %d\n", swaps);

    // If no swaps ocurred, array is sorted
    if (swaps == 0)
    {       
        // Print sorted array and return  
    }
}

Solution

  • Adding to the other answers, which will fix the problem specifically with your code, you can also approach the selection sort algorithm like this.

    Steps to writing this algorithm for an array:

    1. Write a helper function to find the index of the biggest element in the array:

    size_t index_of_largest(int list[], size_t n) {
        size_t i, biggest;
    
        biggest = 0;
        for (i = 0; i < n; i++) {
            if (list[i] > list[biggest]) {
                biggest = i;
            }
        }
        return biggest;
    }
    

    2. Iterate over i=n to i=1, and find the biggest value between list[0] and list[i-1]. After this element is found, swap it into the last position. The function could look like this:

    void sort_list(int list[], size_t n) {
        size_t i, biggest;
    
        for (i = n; i > 0; i--) {
            biggest = index_of_largest(list, i);
            int_swap(&list[biggest], &list[i-1]); /* swapping function */
        }
    }
    

    3. Considering these ideas, you can write a simple version of the algorithm like this:

    #include <stdio.h>
    
    void sort_list(int list[], size_t n);
    size_t index_of_largest(int list[], size_t n);
    void print_array(int list[], size_t n);
    void int_swap(int *x, int *y);
    
    int main(void) {
        int list[] = {10, 5, 6, 3, 4, 11, 9, 7, 2};
        size_t n = sizeof list / sizeof *list;
    
        printf("Before: ");
        print_array(list, n);
    
        sort_list(list, n);
    
        printf("After: ");
        print_array(list, n);
    
        return 0;
    }
    
    void sort_list(int list[], size_t n) {
        size_t i, biggest;
    
        for (i = n; i > 0; i--) {
            biggest = index_of_largest(list, i);
            int_swap(&list[biggest], &list[i-1]);
        }
    }
    
    size_t index_of_largest(int list[], size_t n) {
        size_t i, biggest;
    
        biggest = 0;
        for (i = 0; i < n; i++) {
            if (list[i] > list[biggest]) {
                biggest = i;
            }
        }
        return biggest;
    }
    
    void print_array(int list[], size_t n) {
        size_t i;
    
        for (i = 0; i < n; i++) {
            printf("%d ", list[i]);
        }
        printf("\n");
    }
    
    void int_swap(int *x, int *y) {
        int temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
    

    Output:

    Before: 10 5 6 3 4 11 9 7 2
    After: 2 3 4 5 6 7 9 10 11
    

    Compiled with:

    gcc -Wall -Wextra -o progname progname.c