All pastes #2052548 Raw Edit

Untitled

public text v1 · immutable
#2052548 ·published 2011-05-01 16:40 UTC
rendered paste body
void BubbleSort(int a[], int n)
{
    int i, j;// needed for the loops
    int tmp;// needed for swapping values between two variables
       
    if (n <= 1) return;// check if the array has no elements or has 1 element
                       // in that case there is no need of sorting
    i = 1;// initializing the variable i
    do// the start of a do/while loop
    {
       
       
        for (j = n - 1; j >= i; --j)// for loop that goes backwards and compares
        {
           
            if (a[j-1] > a[j])// if the condition is met then swap
            {
                tmp = a[j-1];
                a[j-1] = a[j];               
                a[j] = tmp;               
            }
        }
       
    } while (++i < n);
}