Vien Nguyen
Vien Nguyen

Reputation: 21

Can't find anything wrong with my selection sort code in C, but the code won't work

I'm a complete beginner in C, and I'm trying to learn how to do this selection sort algorithm to create an ascending list. But for some reason it doesn't run, and I can't seem to figure out why for an entire morning?

please tell me how I can fix it?

int main() {

    int x, i, j, tempt, min;
    int array[] = { 2,9,15,7,5,3,11,8,4 };
    int size = sizeof(array) / sizeof(array[0]);

    for (i = 0; i < (size - 1); i++)
    {
        min = i;

        for (j = i + 1; j < size; j++)
        {
            if (array[j] < array[min])
            {
                min = j;
            }
            if (min != i)
            {
                tempt = array[i];
                array[i] = array[min];
                array[j] = tempt;
            }

        }
        for (i = 0; i < (size); i++) {
            printf("%d\n", array[i]);
        }
    }
}

This is what I got

2
9
15
7
5
3
11
8
4

--------------------------------
Process exited after 0.02851 seconds with return value 0
Press any key to continue . . .

Upvotes: 0

Views: 95

Answers (2)

Thug Life
Thug Life

Reputation: 109

There are two problems with your code:-

First, You are using i inside the primary loop that you are using to sort. For example, let's think that:

  1. your loop started with i=0;
  2. your min got the value of i which is 0
  3. now the second loop that uses j ran and did it's work.
  4. now your control will shift to the third loop. where you are printing the array. Since you have written it inside the main for loop of your program, it can change i. It will start with i's initial value which is 0 and print the values of array till i<size. So, we can say at the end of this loop your i will be equal to size. Now the value of i is equal to size. Now the main for loop will check the condition if i<size-1 and it's false and the main for loop will not run and your program will exit after your main for loop ran for just once.

Second Problem is that even if you put the last for loop out and print it after the whole sorting(). Still, when you replace the array[min] variable with the array[i] variable then technically the min index also changes because you changed the variable at this index and send the minimum variable at another location through swapping. So you should change your min to j after each swapping. After considering all these possibilities, your code should look like this:-

#include <stdio.h>

int main() {

    int x, i, j, tempt, min;
    int array[] = { 4,9,15,7,5,3,11,8,4 };
    int size = sizeof(array) / sizeof(array[0]);

    for (i = 0; i < (size - 1); i++)
    {
        min = i;

        for (j = i + 1; j < size; j++)
        {
            if (array[j] < array[min])
            {
                min = j;
            }
            if (min != i)
            {
                tempt = array[i];
                array[i] = array[min];
                array[j] = tempt;
                min=i;
            }

        }
    }
    for (i = 0; i < (size); i++) {
            printf("%d\n", array[i]);
        }
}

Upvotes: 1

machine_1
machine_1

Reputation: 4454

  1. Move the block:

    if (min != i)
    {
        tempt = array[i];
        array[i] = array[min];
        array[j] = tempt;
    }
    

outside the inner loop.

  1. fix the typo; min should be used instead of j for swapping:

    if (min != i)
    {
        tempt = array[i];
        array[i] = array[min];
        array[min] = tempt;
    }
    
  2. Move the printing loop outside of the outer loop:

for (i = 0; i < (size); i++) {
    printf("%d\n", array[i]);
}
  1. Remove x, it is not being used anywhere.

Your code should look like this:

#include <stdio.h>

int main() {

    int i, j, tempt, min;
    int array[] = { 2,9,15,7,5,3,11,8,4 };
    int size = sizeof(array) / sizeof(array[0]);

    for (i = 0; i < (size - 1); i++)
    {
        min = i;

        for (j = i + 1; j < size; j++)
        {
            if (array[j] < array[min])
            {
                min = j;
            }

        }
        if (min != i)
        {
            tempt = array[i];
            array[i] = array[min];
            array[min] = tempt;
        }
    }
    for (i = 0; i < (size); i++) {
        printf("%d\n", array[i]);
    }
    return 0;
}

Upvotes: 1

Related Questions