[ create a new paste ] login | about

Link: http://codepad.org/7fg0k6LS    [ raw code | output | fork ]

C++, pasted on Jul 3:
#include <iostream>
using namespace std;
 
void bubble_sort(int *a, int length)
{
     for (int i = 0; i < length-1; i++) {
        bool swapped = false;
         for (int j = 0; j < length-i-1; j++) {
             if (a[j] > a[j+1]) {
                 int b = a[j]; 
                 a[j] = a[j+1];
                 a[j+1] = b;
                 swapped = true;
             }
         }
         
         if(!swapped)
            break;
     }
 }

int main(){
    int i, n;
    int A[] = {-1, 3, -8, 14, 5, 2, 15};
    n = sizeof(A)/sizeof(A[0]);
    cout<<"\nINPUT : ";
    for( i = 0; i < n; i++ )
        cout<<A[i]<<" ";
    bubble_sort(A, n);
    cout<<"\nOUTPUT : ";
    for( i = 0; i < n; i++ )
        cout<<A[i]<<" ";
    return 0;
}


Output:
1
2
3

INPUT : -1 3 -8 14 5 2 15 
OUTPUT : -8 -1 2 3 5 14 15 


Create a new paste based on this one


Comments: