Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, HOw do i program a program to sort numbers? It will sort from the 10 numbers i enter. Please help thanks

================================================================I'm using [ and ] in place of the greater-than and less-than signs.
void swap(int i1,i2){
int i3;
i3=i1;
i1=i2;
i2=i3;
}int nums[10];
void main(){
int x,y;
for (x=0;x[11;x++){
for (y=x;y[11;y++){
if x]y then swap(x,y);
}
}You will have to modify this slightly to mix it into your program but the method is clearly displayed here.
borelli34

No, that is C, not C++.
Just be sure to substitute the [ and ] for the appropriate less than (instead of [), greater than (instead of ]) signs

G'day,
These two examples work with TurboC 2.0. The first one is a straight copy of the basic bubble sort algorithm adapted for the C programming language. The second is a version using a function swap()- ( acknowlegement Borelli34) and involves pointers.
/***************************/
/***** Bubble sort Example,12/3/03 */
/************ Author: Traditional * /
#include
#include/* Main Function */
void main(void){
int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;clrscr();
for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}
printf("The array is as follows : \n");
for (index=0;index
#include/********Function Prototype********/
void swap(int *x, int *y);/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};/* Main Function */
void main(void){int top, search,index;
clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);}/* End Main*/
/*********Function Swap****/
void swap(int *x ,int *y){
int temp;temp=*x;
*x=*y;
*y=temp;
}You might like to code these into your machine (probably the first one) and set a few watches and and then single step (F8) thru' the code to see how the algorithm works.
Hope this helps.
regards,
Elric

G'day,
I have absolutely no idea what happend to that formatting!!!
I hope these get through better.../****************** */
/*******Bubble sort Example 12/3/03 */
/*******Author: Traditional * /
#include
#include/* Main Function */
void main(void){
int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;clrscr();
for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}printf("The array is as follows : \n");
for (index=0;index
#include/********Function Prototype********/
void swap(int *x, int *y);/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};/* Main Function */
void main(void){int top, search,index;
clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);}/* End Main*/
/*********Function Swap****/
void swap(int *x ,int *y){
int temp;temp=*x;
*x=*y;
*y=temp;
}I hope these get thru' OK
regards,
Elric

G'day,
Heck!! This is longer than Ben Hur!!
I can't work out why these will not format, so I am going to send them individually and see if that works...fingers crossed!!#include
#include/* Main Function */
void main(void){
int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;clrscr();
for(top=0;top table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}printf("The array is as follows : \n");
for (index=0;index 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);}
This looks fine in my editor...
regards,
Elric

G'day,
Still won't work. It's those greater than and less than keys (what's up ??????????)
I'll have to modify my programme and use [ and ] instead (now I know why Borelli34 used them...)#include [stdio.h]
#include [conio.h]/* Main Function */
void main(void){
int table[10]={23,45,1,5,65,24,8,7,10,55};
int top, search, hold,index;clrscr();
for(top=0;top[9;top++)
for(search=top+1;search[10;search++){
if(table[search] ] table[top]){
hold=table[top];
table[top]=table[search];
table[search]=hold;
}
}printf("The array is as follows : \n");
for (index=0;index[ 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);}
/************ ************/
/************ Bubble sort Example 12/3/03 */#include [stdio.h]
#include [conio.h]/********Function Prototype********/
void swap(int *x, int *y);/**********Test Array********************/
int table[10]={23,45,1,5,65,24,8,7,10,55};/* Main Function */
void main(void){int top, search,index;
clrscr();
/*********Bubble Sort Algorithm***********/
for(top=0;top[9;top++)
for(search=top+1;search[10;search++){
if(table[search] ] table[top]){
swap(&table[top],&table[search]);
}
}
/********** Print Results *************/
printf("The array is as follows : \n");
for (index=0;index[ 10;index++)
printf("Element %-2d : %5d \n",index,table[index]);}/* End Main*/
/*********Function Swap****/
void swap(int *x ,int *y){
int temp;temp=*x;
*x=*y;
*y=temp;
}That seems to have worked. Note only replace [ (less than) and ] (greater than) in the appropiate places ie I also use them for array indexes (should only be replaced in the if and for next loops).
That was a lot more complicated than it should have been, but at least I have learned something too...
regards,
Elric

I am typing this straight from my textbook. Hope it helps
//sorting
for (out=0; outlist[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");

sorry, this IS the correct one..
I am typing this straight from my textbook. Hope it helps
//sorting
for (out=0; outlist[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");

for (out=0; out[size-1; out++)
{
for (in=out+1; in[size; in++)
{
if (list[out] ] list[in])
{
temp=list[in];
list[in]=list[out];
list[out]=temp;
}
}
}
printf("The sorted array in ascending order is.....\n");

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |