Advertisement
UntitledPotato

bubbleSort

Sep 19th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int i , j ;
  7.     int size , temp ;
  8.    
  9.     cout << "Enter the size of the array : " ;
  10.     cin >> size ;
  11.    
  12.     int array[size];
  13.    
  14.     for (i = 0 ; i < size ; i++)
  15.     {
  16.         cout << "Enter a number : ";
  17.         cin >> array[i];
  18.     }
  19.    
  20.    
  21.     for (i = 0 ; i < size ; i++)
  22.     {
  23.         for (j = 0 ; j < size-i-1 ; j++)
  24.         {
  25.             if (array[j] > array[j+1])
  26.             {
  27.                 temp = array[j];
  28.                 array[j] = array[j+1];
  29.                 array[j+1] = temp;
  30.             }
  31.         }
  32.     }
  33.    
  34.     for (i = 0 ; i < size ; i++)
  35.     {
  36.         cout << array[i] << endl;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement