By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
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
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.     int arr[5]={4,3,5,2,1};
  6.     int i,k,j,temp;
  7.     clrscr();
  8.     for(j=0;j<4;j++)
  9.     {
  10.         for(k=j+1;k<5;k++)
  11.         {
  12.             if(arr[j]>arr[k])
  13.             {
  14.                 temp=arr[j];
  15.                 arr[j]=arr[k];
  16.                 arr[k]=temp;
  17.             }
  18.         }
  19.     }
  20.     printf("\nThe sorted array is:  ");
  21.     for(i=0;i<5;i++)
  22.         printf("%d\t",&arr[i]);
  23. }
  24.  
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
  1. #include "stdio.h"
  2. void main( )
  3. {
  4.     int arr[5] = { 25, 17, 31, 13, 2 } ;
  5.     int i, j, temp ;
  6.     for ( i = 0 ; i <= 3 ; i++ )
  7.     {
  8.         for ( j = i + 1 ; j <= 4 ; j++ )
  9.         {
  10.             if ( arr[i] > arr[j] )
  11.             {
  12.                 temp = arr[i] ;
  13.                 arr[i] = arr[j] ;
  14.                 arr[j] = temp ;
  15.             }
  16.         }
  17.     }
  18.  
  19.     printf ( "\n\nArray after sorting:\n") ;
  20.  
  21.     for ( i = 0 ; i <= 4 ; i++ )
  22.         printf ( "%d\t", arr[i] ) ;
  23. }
  24.  
and the output to this code is coming correct
kindly explain the problem

thank you
Aug 28 '10 #1
Share this Question
Share on Google+
5 Replies


P: 18
In line 22 you are printing the address and not the value.
Change this:
Expand|Select|Wrap|Line Numbers
  1. printf("%d\t",&arr[i]);
to this:
Expand|Select|Wrap|Line Numbers
  1. 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

Post your reply

Sign in to post your reply or Sign up for a free account.