Several Simple sorting algorithms

Source: Internet
Author: User

The simplest sorting algorithm-Bubble Sorting:

 1 void bubble_sort(int *arr, int len) 2 { 3    int up, down; 4    for(up = 0; up != len; ++ up) 5    { 6        for(down = 0; down != len - up - 1; ++ down) 7        { 8            if(arr[down] > arr[down + 1]) 9                swap(arr[down], arr[down + 1]);10        }11    }12 }
View code

There is another way of thinking: from the beginning to the end, and then from the end to the front, this approach is the same as the time complexity of the above approach, and will not be implemented here.

When there are not many elements in the array, the quick sorting algorithm is insert sorting. The implementation code is as follows:

void insert_sort(int *arr, int len){    int pos, index, key;    for(pos = 1; pos != len; ++ pos)    {        key = arr[pos];        for(index = pos - 1; index >= 0; -- index)        {            if(arr[index] > key)                arr[index + 1] = arr[index];            else                break;        }        arr[index + 1] = key;    }}
View code

Insert sorting changes an array from the beginning to a small ordered sequence, and then inserts the next element into an appropriate position to change the array to an ordered sequence with a length plus 1, in this way, you do not need to traverse the entire array at a time.

In the same case, another fast sorting algorithm is -- select sorting. The implementation code is as follows:

void select_sort(int *arr, int len){    int index, pos, min;    for(pos = 0; pos != len; ++ pos)    {        min = pos;        for(index = pos + 1; index != len; ++ index)        {            if(arr[min] > arr[index])                min = index;        }        if(min != pos)            swap(arr[min], arr[pos]);    }}
View code

Quick sorting: you do not need to swap elements every time, which saves the system some steps to swap elements.

Quick sorting is a good option when there are many array elements.

The first implementation code is as follows:

Static int func1 (int * arr, int low, int high) // common method {int key = arr [low]; while (low View code

This implementation mainly relies on the recursive method to divide the array into two parts each time. The previous part is smaller than the intermediate element, and the latter part is larger than the intermediate element. In this way, the recursion ends to the end, this array becomes an ordered sequence.

The second implementation code is as follows:

Static int func2 (int * arr, int low, int high) // speed pointer {int fast = low + 1, last = low; int key = arr [low]; for (; fast <= high; ++ fast) {If (ARR [Fast] <key) {swap (ARR [last + 1], arr [Fast]); + + Last ;}} swap (ARR [low], arr [last]); return last;} void quick_sort (int * arr, int Len) {If (LEN <= 10) insert_sort (ARR, Len); else {int Index = func2 (ARR, 0, len-1); quick_sort (ARR, index + 1 ); quick_sort (ARR + index + 1, len-index-2 );}}
View code

This is the first method, mainly relying on the fast and slow pointer method. Each time the elements of the fast pointer are compared to the key hour, the fast pointer is exchanged with the next element of the slow pointer. In this way, fast pointers always point to more elements than keys. Slow pointers always point to smaller elements than keys. Finally, arr [low] is switched to slow pointers, the key is changed to the median value again. The idea is the same as that of a common method, and the implementation method has some improvements.

Heap sorting is to create arrays every time, so that the largest element is always at the beginning. Then we can exchange this element with the corresponding elements to obtain the ordered sequence.

The code for heap sorting is as follows:

static void func(int *arr, int low, int high){    int index;    int key = arr[low];    for(index = 2 * low + 1; index <= high; index = 2 * index + 1)    {        if(index < high && arr[index] < arr[index + 1])            ++ index;        if(key > arr[index])            break;        arr[low] = arr[index];        low = index;    }    arr[low] = key;}void heap_sort(int *arr, int len){    if(len <= 10)        insert_sort(arr, len);    else    {        int index;        for(index = (len - 2) / 2; index >= 0; -- index)            func(arr, index, len - 1);        for(index = len - 1; index > 0; -- index)        {            swap(arr[0], arr[index]);            func(arr, 0, index - 1);        }    }}
View code

Create a stack of methods, that is, multiply the element subscript by 2 each time to get the value of the lowest layer, compare the larger value in the lower layer with the parent value, if it is larger than the parent value, the value is assigned to the parent value. Otherwise, the system exits and assigns the parent value saved by the key to the current underlying element.

Because algorithms are hard to understand in some places, these algorithms can only be simply written. Of course, there may be some imperfections that will be improved in the future.

Several Simple sorting algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.