Advertisement
pcwizz

bubble sort

Oct 14th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /*
  2. Bubble sort like a boss
  3. */
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. void outputArray(int array[], int length);
  9. int main (){
  10.     int myArray[] = {12,116,2,8,7,9,10,0,1,3,4,5};
  11.     int length = sizeof(myArray) / sizeof(myArray[0]);
  12.     outputArray(myArray, length);
  13.     for (int j=0; j < length -1; ++j){
  14.         for (int i=0; i < length - 1; ++i){
  15.             if (myArray[i] > myArray[i+1]){
  16.                 int temp = myArray[i+1];
  17.                 myArray[i+1] = myArray[i];
  18.                 myArray[i] = temp;
  19.             }
  20.         }
  21.         outputArray(myArray, length);
  22.     }
  23.     return 0;
  24. }
  25.  
  26. void outputArray(int array[], int length){
  27.     for (int i=0; i < length; ++i){
  28.         cout << array[i] << " ";
  29.     }
  30.     cout << endl;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement