Hi, im new in IT programming, our professor asks me to create or find a program that is capable of showing how a bubble sort works.. this is the program..

/*********************************************
*   Demonstration program of Bubble sorting  * 
*        (about n*n comparisons used).       *
* ------------------------------------------ *
* Reference: "A book on C By Al Kelley and   *
* Ira Pohl, The Benjamin/Cummings Publishing *
* Company, Inc, 1984" [BIBLI 09].            *
*                                            *
*                C++ version by J-P Moreau.  *
* ------------------------------------------ *
* SAMPLE RUN:                                *
*                                            *
* Initial table A:                           *
* 7  3  66  3  -5  22  -77  2  36  -12       *
*                                            *
* Sorted table A:                            *
* -77  -12  -5  2  3  3  7  22  36  66       *
*                                            *
*********************************************/
#include <stdio.h>

//return p,q in ascending order
void Order(int *p,int *q) {
  int temp;
  if(*p>*q) {
	temp=*p;
	*p=*q;
	*q=temp;
  }
}

//Buuble sorting of integer array A[]
void Bubble(int *a,int n) {
  int i,j;

  for (i=0; i<n; i++)
	for (j=n-1; i<j; j--)
      Order(&a[j-1], &a[j]);

}

void main() {
  int i,n=10;
  static int a[] = {7,3,66,3,-5,22,-77,2,36,-12};

  printf("\n\n Initial table A:\n");
  for(i=0; i<n; i++)
	printf(" %d ",a[i]);

  Bubble(a,n);

  printf("\n\n Sorted table A:\n");
  for(i=0; i<n; i++)
	printf(" %d ",a[i]);

  printf("\n\n");
}

//end of file bubble.cpp

there's a problem here.. when i run this program, i got this error message..

//return p,q in ascending order <- Declaration Syntax Error and the error highlighted the // stuff..

[IMG]http://i2.photobucket.com/albums/y12/smize33/Error.jpg[/IMG]

what should i do about that..

It's your antiquarian Turbo C 2.0 compiler problem: no // line comments in this ancient C (but not C++ ) language implementation for DOS. Well, use /* */ comments...
That's your problem...

antiquarian -- The compiler is not a person. Antiquated is the proper term.

Thank you, WaltP. Regrettably, in my dictionary antiquarian is an adjective, a person is antiquary.
Thanks again.

the solution given for bubble sort is vast. can anyone help me with a shorter one???

> the solution given for bubble sort is vast. can anyone help me with a shorter one???

:$ silly rumi, nothing in this entire post is vast, except perhaps the silence caused by those shaking their heads at your question.

I use Turbo C/C++ a LOT, and it supports //comments, with either the C or C++ compiler.

My version is 1.01

Your install may have become corrupted. Look for the Turbo C/C++ version, NOT the Turbo C version (which was earlier, and had significant bugs).

Turbo C/C++ 1.01 is good, but it doesn't have the newer features. If your teacher/school allows it, moving up to Visual Express or Code::Blocks would be a big step up. Both are free.

I don't do much of anything with bubble sort, but I believe this is right. Test it for yourself, however:

void bubbleSort(int A[]) {   
  int i, n, swapped;
  n = MAX;
  do {
    swapped = 0;
    for(i =0; i < n; i++) {  
      if(A[i] > A[i + 1 ]) {
        swap(A, i, i + 1);
        swapped = 1;
      }
    } //Look, these comments work fine!
    --n;
  }while(swapped);
}

void swap(int A[], int l, int m) {     //more comments!
  int temp;
  temp = A[l];
  A[l] = A[m];
  A[m] = temp;
}

Can anyone tell me who actually uses bubble sort in a real world application? I believe this algorithm is the most basic functional sorting algorithm there is, but so highly inefficient that I just can't imagine anyone using it for anything except super tiny little lists.

I don't personally find Bubble sort to be intuitive. This is intuitive to me, simpler, and on random numbers, slightly quicker than Bubble sort:

for(i=0;i<Max-1;i++) {
  for(j=i+1;j<Max;j++) {
    if(a[i] > a[j]) {
      temp = a[i];
      a[i] = a[j];
      a[j] = temp;
    }
  }
}

It's like Selection sort, without the "selection". It's easy to memorize, and faster than quicksort for arrays with less than 100 values.

I've called this algorithm Bubble sort and Selection sort, but it's really a simplified blend of the two. So now I call it Easy sort.

"A rose by any other name..."

Insertion sort is the fastest sorting algorithm for less than 100 values, (especially when the values are already partly sorted), but it's not as easy to memorize. I use it to optimize Quicksort when the sub-arrays are small. Very helpful.

This article has been dead for over six months. Start a new discussion instead.