Facebook
From umair, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 92
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void bubblesort(int array[], int size)
  5. {
  6.         for (int step = 0; step < (size -1 );++step)
  7.        
  8.         {
  9.                 int swapped = 0;
  10.                 for(int i=0; i<(size-step -1); ++i)
  11.                 {
  12.                         if (array[i]> array[i+1])
  13.                         {
  14.                                 int temp = array[i];
  15.                                 array[i]=array[i+1];
  16.                                 array [i+1] = temp;
  17.                                 swapped=1;
  18.                                
  19.                         }
  20.                 }
  21.                 if ( swapped ==0)
  22.                 break;
  23.         }
  24. }
  25.  
  26. void printArray(int array[], int size)
  27. {
  28.         for (int i=0; i < size; ++i)
  29.         {
  30.                 cout<<"  "<<array[i];
  31.         }
  32.         cout<<"n";
  33. }
  34.         int main()
  35.         {
  36.                 int data[]={-2, 45,0 ,11 ,-9};
  37.                 int size=sizeof(data)/sizeof(data[0]);
  38.                 bubblesort(data,size);
  39.                 cout<<"Sorted Array in Ascending Order:n";
  40.                 printArray(data, size);
  41.         }
  42.        
  43.  
  44.