Wait, I see a small error in your bubble sort Algorithm. The for loop using I as a index is correct, but the J-For loop is wrong. It's supposed to be 'for(j=I+1;j<5;j++)'. In the present configuration it always starts at 1 to progress through array. This allows the I to contain a values Less and Greater then J. This violates the way the bubble sort is supposed to work.
Example: Initial Array: 15, 23,1,8,12 First Pass(I = 0): 1,23,15,8,12 Second Pass(I = 1): 1,8,23,15,12 Third Pass(i = 2): 1,23,8,15,12 Fourth Pass(I = 3): 1,23,8,15,12 Fifth Pass (I = 4): 1,23,8,15,12 --- On Thu, 8/26/10, COOL DUDE <[email protected]> wrote: From: COOL DUDE <[email protected]> Subject: [c-prog] Help!!! :-( To: [email protected] Date: Thursday, August 26, 2010, 1:03 AM please help me for this program. i'm a beginner. For the below program tell me why it doesn't give the right output. Is there any problem in logic or loop i given. please please please help me! :-( :-( :-( /*write a program to sort five numbers in ascending order*/ /*********************************************************/ #include<iostream.h> #include<conio.h> void main() > { int array[5],i,j,temp=0; clrscr(); cout<<"Enter any five number:\n"; for(i=0;i<5;i++) { cin>>array[i]; } for(i=0;i<5;i++) { for(j=1;j<5;j++) { if(array[i]>array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } } cout<<"The sorted array is:"; for(i=0;i<5;i++) { cout<<"\n"<<array[i]; } getch(); } [Non-text portions of this message have been removed] [Non-text portions of this message have been removed]
