435,264 Members | 1,087 Online
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

# program for selection sort in c language

 P: 2 i have written this code which s follows: Expand|Select|Wrap|Line Numbers #include #include void main() {     int arr[5]={4,3,5,2,1};     int i,k,j,temp;     clrscr();     for(j=0;j<4;j++)     {         for(k=j+1;k<5;k++)         {             if(arr[j]>arr[k])             {                 temp=arr[j];                 arr[j]=arr[k];                 arr[k]=temp;             }         }     }     printf("\nThe sorted array is:  ");     for(i=0;i<5;i++)         printf("%d\t",&arr[i]); }   and my output is the sorted array is: 8236 8238 8240 8242 8244 i also searched from net and i got dis code Expand|Select|Wrap|Line Numbers #include "stdio.h" void main( ) {     int arr[5] = { 25, 17, 31, 13, 2 } ;     int i, j, temp ;     for ( i = 0 ; i <= 3 ; i++ )     {         for ( j = i + 1 ; j <= 4 ; j++ )         {             if ( arr[i] > arr[j] )             {                 temp = arr[i] ;                 arr[i] = arr[j] ;                 arr[j] = temp ;             }         }     }       printf ( "\n\nArray after sorting:\n") ;       for ( i = 0 ; i <= 4 ; i++ )         printf ( "%d\t", arr[i] ) ; }   and the output to this code is coming correct kindly explain the problem thank you Aug 28 '10 #1
5 Replies

 P: 18 In line 22 you are printing the address and not the value. Change this: Expand|Select|Wrap|Line Numbers printf("%d\t",&arr[i]); to this: Expand|Select|Wrap|Line Numbers printf("%d\t",arr[i]); Aug 28 '10 #2

 P: 2 thanx a ziillion :) Aug 29 '10 #3

 P: n/a this is not selection sort, it is bubble sort Oct 16 '10 #4

 P: 1 yeah this is bubble sort . can someone give the exact code foe selection sort??? Jan 10 '12 #5

 Expert 100+ P: 2,400 Review the selection sort algorithm and write your code. Let us know where you get stuck. Jan 10 '12 #6