Local variable loses its value: selection sort algorithm

119 views Asked by At

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  
    }
}
4

There are 4 answers

3
RoadRunner On BEST ANSWER

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
5
user2736738 On

That means your if statement is not becoming true in the meantime.

min should be set in each loop of j.

min=list[j] in for(j=...){min=list[j]; ... }

And also pos=j

12
barak manos On

Move the declaration int swaps = 0 outside the for loop.


In other words, change this:

for (int j = 0, n = sizeof(list) / sizeof(int); j < n; j++)
{
    int swaps = 0;
    ...
}

To this:

int swaps = 0;
for (int j = 0, n = sizeof(list) / sizeof(int); j < n; j++)
{
    ...
}
0
Marcello Scattolini On

I want to thank you all very much. I have solved the problem with your help. Turns out the error had to do with the variable scope (where it was declared). Follow below the working code.

int main (void)
{

//Declare list to be sorted and other variables
int list[] = {9, 5, 7, 8, 4, 3, 2, 1, 6};
int minValPos = 0, maxTempVal = list[0];

for (int j = 0, siz = sizeof (list) / sizeof (int); j < siz; j++)
{
    int swaps = 0, minVal = list[j];

    // Look for min value after each j iteration
    for (int i = j; i < siz; i++)
    {

        // Find minimum value (minVal) and store its position (minValPos)
        if (list[i] < minVal)
        {
            minVal = list[i];
            minValPos = i;
        }

    }

    // Once with MinVal pinpointed, proceed to swap with jth item
    if (minValPos > j)
    {
        maxTempVal = list[j];
        list[j] = minVal;
        list[minValPos] = maxTempVal;
        swaps++;
    }

    // When array did not need any swaps, it means it is sorted
    if (swaps == 0)
    {
        for (int r = 0; r < siz; r++)
        {
            printf ("Position [%d] = %d\n", r, list[r]);
        }
    }
}

}