GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jul-2006, 12:39
nikhilthemacho nikhilthemacho is offline
New Member
 
Join Date: Jul 2006
Posts: 8
nikhilthemacho is on a distinguished road

Bubble Sort Program


I have written the following program for bubble sort, is it correct? Can somebody run it and check the output to confirm its correctness?
Actually, the number of runs for the following sequence : 44,33,55,22,11 is 8 i.e. swap=8.
I also put this code on wikipedia reference, but there people said, its wrong as it takes too many turns to sort. But my point is that number of swaps is only 8, the 20 steps obtained after entering above numbers shows the number of steps required, or the number of times the outer loop for variable "i" runs. So i think it is right. but still any advice would be welcome.
CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>

void main()
{
int orig[20];
int i,j,temp,swap=0,n,m,step=0;

clrscr();

printf("Number of data : ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("enter number %d : ",i+1);
scanf("%d",&orig[i] );
}

clrscr();

printf("step 0  :  ");

for(i=0;i<n;i++)
printf("%3d  ",orig[i]);
printf("   (original array)\n");

for(i=0;i<n;i++)
{

	for(j=0;j<n-1;j++)
	{

	 step++;
	 	if(orig[j]>orig[j+1])
		 {
		  temp=orig[j];
		  orig[j]=orig[j+1];
		  orig[j+1]=temp;
		  swap++;
		 }//end of if()

	 printf("step %3d: ",step);
	 for(m=0;m<n;m++)
	 printf("%3d  ",orig[m]);
	 printf("\n");

	}//end of j loop

}//end of outermost i loop


printf("\n\n-------------------------\n");
printf("NUMBER OF SWAPS : %d",swap);

getch();
}//end of main
Last edited by cable_guy_67 : 02-Jul-2006 at 14:58. Reason: Please surround your C code with [c] your code [/c] code tags.
  #2  
Old 02-Jul-2006, 19:32
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 966
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Bubble Sort Program


have a look at an alternative algorithm here:
http://linux.wku.edu/~lamonml/algor/sort/bubble.html
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11
help with Bubble sort program that uses vectors instead of arrays sasuke101 C++ Forum 9 25-Oct-2005 11:26
need help with bubble sort for c++ sasuke101 C++ Forum 2 21-Sep-2005 20:12
Type casts ? kai85 C++ Forum 12 23-Jun-2005 12:04
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:07.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.