Bubble Sort

//array : Array
//n     : size of array

void BubbleSort(int *array, int n) 
{
    for(int i = n-1; i > 0; i--)
        for(int j = 0; j < i; j++)
            if(a[j]>a[j+1]) {           //if no swap, we can use a flag to exit the loop. Best case: O(n)
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
}

results matching ""

    No results matching ""