8 Replies - 283 Views - Last Post: 25 June 2012 - 09:04 AM Rate Topic: -----

#1 bonethug95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-August 11

error in bubble sort

Posted 24 June 2012 - 09:10 PM

/* Bubble Sort*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],temp,n = 4,k;
cout << "enter an array number and it will be sorted"<<endl;
	for (int i = 0; i<5; i++)
	{
	cin >> a[i];
	}
//sorting begins here
for (int j = 0; j<=n;j++)
{
	if (a[j]>= a[j+1])
	{
		temp = a[j+1];
		a[j+1] = a[j];
		a[j] = temp;
		k = a[j];
	}
else
	{
		k = a[j+1];
	}
	if (a[n] == k)
	{
		n = n - 1;
	}
}
for (i = 0; i<5; i++)
	{
	cout << a[i]<<" ";
	}
	cout << endl;
getch();
}


Okay the problem I am getting here is that the program does not give the right output for eg if u enter
3,4,1,5,2
you will get 2,3,1,4,0

Is This A Good Question/Topic? 0
  • +

Replies To: error in bubble sort

#2 RookieC++User  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-December 10

Re: error in bubble sort

Posted 24 June 2012 - 10:26 PM

Bubble sort usually uses two nested for loops, so I would try nesting them. Then rerun your code and see what happens, and I would also not use void main(). You should always use int main(), and do not use getch() use cin.get().
Was This Post Helpful? 0
  • +
  • -

#3 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 19-December 09

Re: error in bubble sort

Posted 25 June 2012 - 12:05 AM

Why are you doing this?


	 
if (a[n] == k) {
  n = n - 1;
}



For me the best way to implement a bubble sort algorithm is the one below. This is an implementation in C that you could understand easy. In this case i'm trying to sort a four element array:

int array[] = {8,5,3,0};
	
int i,j,aux;
		
for(i = 0;i<3;i++) {
  for(j = 0;j<3;j++) {
	if(array[j] >= array[j+1]) {
		aux = array[j+1];
		array[j+1] = array[j];
		array[j] = aux; 
	}	
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 busyme12srv  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 18-June 12

Re: error in bubble sort

Posted 25 June 2012 - 12:35 AM


[b]/* Here is a simplified and corrected version of bubble sort*/[/b]


/* Bubble Sort*/ 

 #include<iostream.h>  

 #include<conio.h>  

 void main()  
{  

 clrscr();  

 int a[5],temp,n = 4; 

cout << "enter an array number and it will be sorted"<<endl;  

     for (int i = 0; i<5; i++)  

    {  

     cin >> a[i];  

    }  

//sorting begins here  
for (int i=0;i<=n;i++
{
 for (int j = i+1; j<=n;j++)  

 {  

    if (a[i]>a[j])  

    {  

         temp = a[j]

         a[j] = a[i];  

         a[i] = temp; 
          

    }  


   }  

 }
 for (i = 0; i<5; i++)  

     {  

     cout << a[i]<<" ";  

    }  

     cout << endl;  

 getch();  

} 








Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4949
  • View blog
  • Posts: 11,356
  • Joined: 16-October 07

Re: error in bubble sort

Posted 25 June 2012 - 06:18 AM

This should not compile:
for (int i=0;i<=n;i++
{
 for (int j = i+1; j<=n;j++)  



It's not a bubble sort, but some form a selection sort. It's also incorrect on that level.
const int SIZE = 5;
int a[SIZE];
// bad and pointless int n = 4; 
// ...
for (int i=0; i<SIZE-1; i++) {
	for (int j=i+1; j<SIZE; j++)  {




View PostRookieC++User, on 25 June 2012 - 01:26 AM, said:

Bubble sort usually uses two nested for loops


No, it does not. A "bubble sort" has a single iteration and flag. So the outer loop is most appropriately a while loop. Sorry, pet peeve.
Was This Post Helpful? 1
  • +
  • -

#6 bonethug95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-August 11

Re: error in bubble sort

Posted 25 June 2012 - 08:20 AM

View PostRookieC++User, on 25 June 2012 - 12:11 PM, said:

Bubble sort usually uses two nested for loops, so I would try nesting them. Then rerun your code and see what happens, and I would also not use void main(). You should always use int main(), and do not use getch() use cin.get().

I know the commands u saying are wrong but its the way I am being taught at school so can't help it!
Was This Post Helpful? 0
  • +
  • -

#7 bonethug95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-August 11

Re: error in bubble sort

Posted 25 June 2012 - 08:51 AM

View Postpablo9891, on 25 June 2012 - 01:50 PM, said:

Why are you doing this?


	 
if (a[n] == k) {
  n = n - 1;
}



For me the best way to implement a bubble sort algorithm is the one below. This is an implementation in C that you could understand easy. In this case i'm trying to sort a four element array:

int array[] = {8,5,3,0};
	
int i,j,aux;
		
for(i = 0;i<3;i++) {
  for(j = 0;j<3;j++) {
	if(array[j] >= array[j+1]) {
		aux = array[j+1];
		array[j+1] = array[j];
		array[j] = aux; 
	}	
   }
}


I dunno C
Was This Post Helpful? 0
  • +
  • -

#8 bonethug95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-August 11

Re: error in bubble sort

Posted 25 June 2012 - 08:57 AM

View Postbaavgai, on 25 June 2012 - 08:03 PM, said:

This should not compile:
for (int i=0;i<=n;i++
{
 for (int j = i+1; j<=n;j++)  



It's not a bubble sort, but some form a selection sort. It's also incorrect on that level.
const int SIZE = 5;
int a[SIZE];
// bad and pointless int n = 4; 
// ...
for (int i=0; i<SIZE-1; i++) {
	for (int j=i+1; j<SIZE; j++)  {




View PostRookieC++User, on 25 June 2012 - 01:26 AM, said:

Bubble sort usually uses two nested for loops


No, it does not. A "bubble sort" has a single iteration and flag. So the outer loop is most appropriately a while loop. Sorry, pet peeve.

In case u didn't realize you too are doing nested loop but u make the code slightly more versatile.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4949
  • View blog
  • Posts: 11,356
  • Joined: 16-October 07

Re: error in bubble sort

Posted 25 June 2012 - 09:04 AM

You've lost me. I never offered a bubble sort. I merely cleaned up the offered selection sort, called a bubble sort, so that it might work properly.

The start of a bubble sort would look more like:
bool swapped = true;
while(swapped) {
   swapped = false;
   for (int i=0; i<SIZE-1; i++) {
      // no other loops here
      // the outer loop monitors the flag, as noted previously


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1