Summary of my sorting algorithm

Source: Internet
Author: User

1. Insert sort (Stable sort)

The basic idea is to insert a record into an ordered table that has been sorted so that a new ordered table with a record number of 1 is added. Algorithm time complexity O (n^2)

1 int insert_sort ( int *array, int len)
2 {
3      int I, J;
4     int key;
5
6     for (J =1 J < Len; j +)
7     {
8   & nbsp;     key = Array[j];
9         i = j-1;
        While (i >=0 && array[i] > key)
11         {
             ARRAY[I+1] = array[i];
            i--;
       }
        array[i+1] = key;
   }
    return  0;
I}

2. Hill sort (unstable sort)

Also known as "narrowing the incremental sort", it is also a sort of insertion, but the time efficiency is better than the previous sorting methods. It divides the sequence into several subsequence sequences by an increment, and then inserts the sequence into each subsequence, narrowing the increment until the increment is reduced to 1, so that the sequence can be sorted by a small amount of movement and insertion.

1voidShell_sort (int*v,intN
2 {
3intGap, I, J, TMP;
4
5 for(Gap = N/2 gap >0; gap >>= 1)
6 {
7 for(i = gap; i < n; i++)
8 {
9 for(j = i-gap J >=0 && V[j] > V[j+gap]; J-= Gap)
10 {
TMP = V[j];
V[J] = V[j+gap];
V[J+GAP] = tmp;
14}
15}
16}
17}

3. Bubble sort (Stable sort)

Basic idea: Keywords smaller records float, keywords large records sinking

1 #defineSWAP (_t_, X, Y)Todo{/
2 _t_ tmp = (X);
3 (X) = (Y);
4 (Y) = tmp;/
5} while(0)
6
7voidBuble_sort (int*array,intN
8 {
9intExchange
10intI, J;
11
12 for(i = n-1 i >=0; i--)
13 {
Exchange = 0;
15 for(J =0 J <= I-1; j + +)
16 {
17if(Array[j] > Array[j+1])
18 {
Exchange = 1;
SWAP (int, Array[j], array[j+1]);
21}
22}
23if(Exchange ==0)
24 {
25 Break;
26}
27}
28}

4. Quick sort (unstable sort)

The quick sort is an improved version of the bubble sort, and its basic idea is to divide the records into two parts by a single pass, in which some of the records are smaller than the other, so that the whole sequence is sorted by sorting the two parts of the records separately.

int partition (int array[], int low, int high)
{
    int key = Array[low];
    
    while (Low < high) {while
        (Low < high && Array[high] >= key) {
            high--
        }
        Array[low] = Array[high];
        while (Low < high && Array[low] < key) {
            low++
        }
        Array[high] = Array[low];
    }
    Array[low] = key;
    return low;
}

void Quick_sort (int array[], int low, int high)
{
    if (Low < high) {
        int q = partition (array, low, high);
        Quick_sort (array, low, q-1);
        Quick_sort (array, q+1, high);
    }


5. Select sort (unstable sort)

Basic idea: Keyword comparison, select the smallest keyword each time

1voidSelection_sort (int*array,intLen
2 {
3intK
4intI, J;
5
6 for(I =0 i < len-1; i++)
7 {
8 k = i;
9 for(j = i+1 J < Len; j + +)
10 {
11if(Array[j] < array[k])
12 {
K = J;
14}
15}
SWAP (int, Array[i], array[k]);
17}
18
19}
20

6. Heap sort (unstable sort)

Select an improved version of the sort and make full use of the results of the previous comparison.

Definition of heap:

The sequence of n elements {k1, K2, K3 ... kn} is called a heap if and only if the following relationship is satisfied.

Ki <= k2i and ki <= k2i+1

Or:

Ki >= k2i and ki >= k2i+1


1 #include <stdio.h>
2
3 #define SIZE 10
4 #define SWAP (_t_, A, B)Todo{/
5 _t_ tmp = a;/
6 A = b;/
7 B = tmp;} while(0)
8
9
10voidHeapsort (intA[],intHEAPSIZE);
11voidMaxheapify (intA[],intSinte);
12
13intMainintargcChar**ARGV)
14 {
15intA[size] = {16, 4, 10, 14, 7, 9, 3, 2, 8, 1};
/* Note a[0] are not used*/
17intI, heapsize;
18
HeapSize = SIZE;
Heapsort (A, heapsize);
21st
22 for(I =0 i < SIZE i++)/* Print the sorted array * *
printf ("%d", a[i]);
printf ("n");
25
26 return0;
27}
28
29/*
* Heapsort:contains Two procedures, one is to build the Max-heap,
The other are to delete Max to gain the sorted array.
32 */
33voidHeapsort (intA[],intHeapSize)
34 {
35intI
36
37 for(i = HEAPSIZE/2 i >= 0; i--)
38 {
Maxheapify (A, I, heapsize-1);//build heap from I to (heapsize-1)
40}
41
42 for(i = heapsize-1 i >0; i--)
43 {
SWAP (int, A[0], a[i]);
Maxheapify (A, 0, i-1);
46}
47}
48
49/*
Maxheapify:used to let the value at A[i] ' float down ' in the
"Max-heap so" subtree rooted at index I becomes a max-heap.
52 */
53voidMaxheapify (intA[],intSintE
54 {
55intrc = A[s];
56intJ
57
58 for(j =2*s + 1; j <= E; J =2*j + 1)
59 {
60if((j + 1) <= e && a[j] < a[j+1])
61 {
+ j + +; Right child is larger, choose it
63}
64if(RC > A[j])//we
65 {
66 Break;
67}
A[s] = A[j];
The S = j;
70}
A[s] = RC; Store the key word
72}
73

7. Merge sort (Stable sort)

The ordered table group is synthesized into a new ordered table.

1voidMergeintA[],intLowintMidintHigh
2 {
3int*tmp = (int*) malloc ((high-low+1) *sizeof(int));
4intS1 = low;
5ints2 = mid+1;
6inte1 = mid;
7inte2 = high;
8intI
9
10 for(i = 0; (S1 <= E1) && (S2 <= E2); i++)
11 {
12if(A[S1] < A[S2])
13 {
Tmp[i] = a[s1];
s1++;
16}
17Else
18 {
Tmp[i] = A[s2];
s2++;
21}
22}
23
24if(S1 <= E1)
25 {
26 for(; S1 <= E1; s1++)
27 {
Tmp[i] = a[s1];
i++;
30}
31}
32if(S2 <= E2)
33 {
34 for(; S2 <= E2; s2++)
35 {
36
Panax Notoginseng Tmp[i] = a[s2];
i++;
39}
40}
41
memcpy (&a[low], TMP, (high-low + 1) *sizeof(int));
Free (TMP);
44}
45
46
47voidMerge_sort (intA[],intFirstintLast
48 {
49intMID = 0;
50
51if(I < last)
52 {
MID = (A/last)/2;
Merge_sort (A, I, mid);
Merge_sort (A, mid+1, last);
Merge (A, I, Mid, last);
57}
58}
59

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.