Advertisement
Felanpro

Array Sorting

Jul 16th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. Array Sorting, effective method (Bubble-Sort)
  2.  
  3. #include <iostream>
  4. #include <iomanip>>
  5.  
  6. using namespace std;
  7.  
  8. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  9.  
  10. int main() {
  11.    
  12.     int temp;
  13.     int n = 4;
  14.    
  15.     int arr[n] = {7, 8, 2, 1};
  16.    
  17.     for(int x = 0; x < n; x++)
  18.     {
  19.         for(int k = 0; k < n; k++)
  20.         {
  21.             if(arr[k] > arr[k + 1])
  22.             {
  23.                 temp = arr[k];
  24.                 arr[k] = arr[k + 1];
  25.                 arr[k + 1] = temp;
  26.             }
  27.         }
  28.     }
  29.      
  30.     for(int c = 0; c < n; c++)
  31.         cout << arr[c] << endl;
  32.    
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement