Wounder
Wounder

Reputation: 9

Why this selection sort algorithm not work?

I have tried selection sorting algorithm using single for loop.

but the output is not as expeted.

is there any mistake in this code. pleases help.

#include <stdio.h>
int main() {
   int arr[10]={6,12,0,18,11,99,55,45,34,2};
   int n=10;
   int i=0, j,swap;
 
      for (j =1; j <n; j++) {
          if(j==(n-1)){
              i++;
              j=i+1;
          }
          
         if (arr[i] > arr[j]){
           swap = arr[j];
           arr[j] = arr[i];
           arr[i] = swap;}
     
     
      }
    for (int jj = 0; jj < n; jj++)
      printf("%d\t", arr[jj]);

   return 0;
}


output:  0       6       11      12      18      34      45      55      0       99

Upvotes: 0

Views: 87

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311010

For starters in this if statement

     if (arr[i] > arr[j]){
       swap = arr[j];
       arr[j] = arr[i];
       arr[i] = swap;}

there is used undeclared variable swap. You have to write

     if (arr[i] > arr[j]){
       int swap = arr[j];
       arr[j] = arr[i];
       arr[i] = swap;}

Secondly I have been unable to get the output you showed. If to write your updated program as shown above then its output is

0   6   11  12  18  34  45  55  2   99

The problem of your for loop is that the last element of the array takes part in the algorithm only when i is equal to n-2 due to this if statement

      if(j==(n-1)){
          i++;
          j=i+1;
      }

in this case j will be equal to n-1 before this if statement

     if (arr[i] > arr[j]){
       int swap = arr[j];
       arr[j] = arr[i];
       arr[i] = swap;}

Rewrite your for loop the following way as it is shown below in your updated program

#include <stdio.h>
int main() {
   int arr[10]={6,12,0,18,11,99,55,45,34,2};
   int n=10;
   int i=0, j;
 
      for (j =i; j <n; ) {
         if (arr[i] > arr[j]){
           int swap = arr[j];
           arr[j] = arr[i];
           arr[i] = swap;}
     
            if( ++j== n ){
              i++;
              j=i+1;
          }
          
   
      }
    for (int jj = 0; jj < n; jj++)
      printf("%d\t", arr[jj]);

   return 0;
}

In this case the program output is

0   2   6   11  12  18  34  45  55  99

Pay attention to that your approach is inefficient because there are many redundant swaps of elements of the array.

It is better to use the traditional approach to the implementation of the selection sort method. For example

#include <stdio.h>

void selection_sort( int a[], size_t n )
{
    for ( size_t i = 0; i + 1 < n; i++ )
    {
        size_t min_pos = i;
        
        for ( size_t j = i + 1; j < n; j++ )
        {
            if ( a[j] < a[min_pos] ) min_pos = j;
        }
        
        if ( min_pos != i )
        {
            int tmp = a[i];
            a[i] = a[min_pos];
            a[min_pos] = tmp;
        }
    }
}

int main( void ) 
{
    int a[] = { 6, 12, 0, 18, 11, 99, 55, 45, 34, 2 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    selection_sort( a, N );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d\t",  a[i] );
    }
    putchar( '\n' );

    return 0;
}

The program output is

0   2   6   11  12  18  34  45  55  99

Upvotes: 0

Răzvan Rujoiu
Răzvan Rujoiu

Reputation: 1465

Selection sort uses two for nested loops. Try this code:

    int arr[10]={6,12,0,18,11,99,55,45,34,2};
    int n = 10;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        if (arr[i] > arr[j]) {
            int swap = arr[i];
            arr[i] = arr[j];
            arr[j] = aux;
        }
      }
    }

Upvotes: 1

Related Questions