473,505 Members | 13,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program for selection sort in c language

2 New Member
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
5 14188
srbakshi
18 New Member
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
sun215
2 New Member
thanx a ziillion
:)
Aug 29 '10 #3
this is not selection sort, it is bubble sort
Oct 16 '10 #4
mathsmaster
1 New Member
yeah this is bubble sort . can someone give the exact code foe selection sort???
Jan 10 '12 #5
donbock
2,426 Recognized Expert Top Contributor
Review the selection sort algorithm and write your code. Let us know where you get stuck.
Jan 10 '12 #6

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

Similar topics

4
5860
by: shaveta | last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
1
4151
by: gemacjr1201 | last post by:
I need hints for an algorithm using string compare in a selection sort. I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
1
3634
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
12
2554
by: ShaveDave27 | last post by:
HI, I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a...
1
1720
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I want to make a "GRAPHICS PROGRAM" in C language that shows sorting of lines. Something like this:- ------------ ------------------- ------ ------------------------------------ after...
1
1993
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I want to make a "GRAPHICS PROGRAM" in C language that shows sorting of lines. Something like this:- ------------ ------------------- ------ ------------------------------------ after...
1
3396
by: neyugncul | last post by:
Okay, I have this homework assignment which is to sort a 2D array of characters using selection sort. I've written the selection sort but I can't seem to get it working. (it's not sorting!) ...
1
5537
by: fatimaballes | last post by:
how to apply bubble and selection sort in assembly?this is for my subject IT103Lab.. were using notepad++ for encoding then amke everything appear in the command prompt.. another thing... our...
1
1474
mfshake
by: mfshake | last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.