program for selection sort in c language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sun215
    New Member
    • Aug 2010
    • 2

    #1

    program for selection sort in c language

    i have written this code which s follows:
    Code:
    #include<stdio.h>
    #include<conio.h>
    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

    Code:
    #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
  • srbakshi
    New Member
    • Aug 2008
    • 18

    #2
    In line 22 you are printing the address and not the value.
    Change this:
    Code:
    printf("%d\t",&arr[i]);
    to this:
    Code:
    printf("%d\t",arr[i]);

    Comment

    • sun215
      New Member
      • Aug 2010
      • 2

      #3
      thanx a ziillion
      :)

      Comment

      • ankit chandra

        #4
        this is not selection sort, it is bubble sort

        Comment

        • mathsmaster
          New Member
          • Jan 2012
          • 1

          #5
          yeah this is bubble sort . can someone give the exact code foe selection sort???

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Review the selection sort algorithm and write your code. Let us know where you get stuck.

            Comment

            Working...