Bubble, quick sort comparison

Everyone must be familiar with bubble sorting. Each round of bubbling will sort the largest number to the front. The time complexity of each round is O(n). If the size of the array to be sorted is n, it needs to go through It takes n rounds to sort all the elements in the array, so the total time complexity is O(n 2 ).

Iterative bubble sort

 

void BubbleSort(int *a, int len) //Array a to be sorted and its length len

{

    int ordered = false;

    int temp, i;

 

    while(len && !ordered)

    {

        ordered = true; //maybe there is no exchange during the sorting process, indicating that the array is ordered

        for(i = 1; i < len; i++) //it is better for i to start from 1 than from 0

        {

            if(a[i-1] > a[i])

            {

                ordered = false;

                temp = a[i];

                a[i] = a[i-1];

                a[i-1] = temp;

            }

        }

        len--;

    }

}

 

intmain()

{

    int a[5] = {5,1,2,3,4};

    int i = 0;

    int len = length(a);

    BubbleSort (a, len);

    for(i = 0; i < len; i++)

    {

        printf("%d ",a[i]);

    }

    return 0;

 

}

 

----------------------------------------------------------------

 

For quick sort, select a pivot element, then compare the pivot element with all elements of the array, put the element larger than the pivot element on the right of the pivot element, put the element smaller than the pivot element on the left of the pivot element, and for a The quick sort needs to be compared n times, and the time complexity of each quick sort is O(n). Next, you need to perform recursive quick sort on the two subarrays divided by the quick sort. If each quick sort is very average Divide the array into two sub-arrays, then the depth of recursive quicksort is O(lgn), so the total time complexity is O(nlgn). But quicksort can degenerate into bubbling. If once every quicksort, unfortunately, the largest or smallest element is selected as the pivot element, then the recursion depth will become n, and the time complexity becomes O(n 2 ), At this time, the efficiency of quicksort is minimized and degenerates into bubbling. Therefore, quicksort is very important for the selection of hub elements. If you can select the hub elements that divide the array equally in each pass, then the efficiency of quicksort is the highest. How to choose a hub Elements will be the key to measuring the quicksort. I recommend using the three-way method to select. Before each quicksort, compare the three elements at the beginning, the middle, and the end of the array, and choose the middle one. The number of s is used as the pivot element, which can reduce the risk of degenerating into bubbling, or any number can be used as the pivot element, which can also reduce the risk.

 

----------------------------------------------------------------

Recursive bubble sort and recursive quicksort, you'll find the difference, why quicksort is better than bubbling, and how to write programs from bubbling to quicksort.

 

recursive bubble sort

int Sort (int * a, int len)

{

    int ordered = true; //maybe there is no exchange during the sorting process, indicating that the array is ordered

    int temp, i;

    for(i = 1; i < len; i++) //It can be seen that the time complexity of Sort() is O(n), which is related to the length of the array to be sorted.

    {

        if(a[i-1] > a[i])

        {

            ordered = false;

            temp = a[i];

            a[i] = a[i-1];

            a[i-1] = temp;

        }

    }

    return ordered;

}

 

void BubbleSort(int *a, int len) //Recursive algorithm of bubble sort

{   

    if(len == 1) //If there is only one element, it is the largest element, no need to bubble

        return;

    else

    {

        int ordered = Sort(a,len); //Select the largest element and place all elements smaller than the largest element to the left of the largest element, while quick sorting will use a pivot element, and put the elements larger than the pivot element on the left On the right of the hub, small on the left

        if(ordered) //If there is no exchange of elements, it means that it has been ordered

            return;

        BubbleSort(a,len-1); //Recursively bubble out all elements after the largest element, if the quick sort will be used as the left recursion of quick sort

        //If there is right recursion in quicksort

    }

}

 

intmain()

{

    int a[10] = {10,1,5,2,4,3,6,7,9,8};

    int i = 0;

    int len = length(a);

    BubbleSort (a, len);

    for(i = 0; i < len; i++)

    {

        printf("%d ",a[i]);

    }

    return 0;

 

}

 

----------------------------------------------------------------

recursive quicksort

 

int Sort(int *a, int low, int high)

{

    int pivot = a[low]; //Every pivot element here takes the first element of the array to be sorted, remember it is a[low], not a[0]

    if(low < high) //time complexity is O(n), n is the length of the array

    {

        while(a[high] >= pivot && low < high)

            high --;

        a[low] = a[high];

 

        while(a[low] <= pivot && low <high)

            low ++;

        a[high] = a[low];

    }

    a[low] = pivot;

    return low;

}

 

void QuickSort(int *a, int low, int high)

{

    if(low >= high)

        return ;

    else

    {

        int mid = Sort(a,low,high); //divide the sub-recursive array

        QuickSort(a,low,mid-1); //Left recursion

        QuickSort(a,mid+1,high); //Right recursion, once right recursion mid+1=high, it will degenerate into bubbling, the recursion depth will become n, n is the length of the array

    }

}

intmain()

{

    int a[5] = {3,1,5,2,4};

    int i = 0;

    int len = length(a);

    QuickSort(a,0,len-1);

    for(i = 0; i < len; i++)

    {

        printf("%d ",a[i]);

    }

    return 0;

}

 

Recursive bubbling is a recursive tree with only the right subtree, the recursion depth is O(n), and a good quick sort will generate a balanced binary tree with a recursion depth of O(lgn), so quicksort is right A huge improvement for Bubble.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326884342&siteId=291194637