/*****************************************************
* Demonstration program of Bubble sorting * 
* (about n*n comparisons used). *
* ------------------------------------------ *
* ------------------------------------------ *
* 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 ----
 
//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 for (j=n-1; i 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 printf(" %d ",a[i]);
 
Bubble(a,n);
 
printf("\n\n Sorted table A:\n");
for(i=0; i printf(" %d ",a[i]);
 
printf("\n\n");
}
 
//end of file bubble.cpp
 
 
 
 
Program-2
 
 
 
#include <stdio.h>
#include <iostream.h>
 
void bubbleSort(int *array,int length)//Bubble sort function 
{
      int i,j;
      for(i=0;i<10;i++)
      {
            for(j=0;j<i;j++)
            {
                  if(array[i]>array[j])
                  {
                        int temp=array[i]; //swap 
                        array[i]=array[j];
                        array[j]=temp;
                  }
 
            }
 
      }
 
}
 
void printElements(int *array,int length) //print array elements
{
      int i=0;
      for(i=0;i<10;i++)
            cout<<array[i]<<endl;
}
 
 
void main()
{
 
      int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort 
    bubbleSort(a,10);                 //call to bubble sort  
      printElements(a,10);               // print elements 
}

 

 

 

 

 

Home

Sortings >>