Data Structure notes-sorting

Source: Internet
Author: User

Sort;
1. Insert sorting (insert sorting directly and sort by Hill)
2. Select sorting (directly select sorting and heap sorting)
3. Exchange sorting (Bubble sorting and quick sorting)
4. Merge and sort
5. Base sorting

---------------------
Insert sort directly
Note: add the last number to the sorted order one by one. In the direct insertion sorting process, the insertion sorting of one of the records is called once.

Sorting; direct insertion sorting starts from the second record. Therefore, the sequence of records whose length is n needs to be sorted n-1 times to complete the whole process.

Sequence sorting. The time complexity is O (n2 ).
Void insertsort (elemtype X [], int N)

{
Int I, J;
Elemtype S;
For (I = 0; I <n-1; I ++)
{
S = x [I + 1];
J = I;
While (j>-1 & S. Key <X [J]. Key)
{
X [J + 1] = x [J];
J --;
}
X [J + 1] = s;
}
}

---------------------
Hill sorting
(Hill sorting, also known as downgrading incremental sorting, can have a variety of incremental di methods, but the last incremental sorting must be 1, the simplest)

Single-choice di + 1 = di/2 (small ). The time complexity is O (N (log2n) 2 ).

Void shellsort (elemtype X [], int N, intd [], int number)

{
Int I, J, K, M, span;
Elemtype S;
For (m = 0; m <number; m ++)
{
SPAN = d [m];
For (k = 0; k <span; k ++)
{
For (I = K; I <n-1; I + = span)
{
S = x [I + SPAN];
J = I;
While (j>-1 & S. Key <X [J]. Key)
{
X [J + SPAN] = x [J];
J-= span;
}
X [J + SPAN] = s;
}
}
}
}

----------------------------
Select sort directly
Note: each time the smallest part is found and inserted into the preceding sorted order. Similarly, n-1 sorting is performed for sequences with N records.
The time complexity is O (n2 ).
Void selectsort (elemtype X [], int N)

{
Int I, j, small;
Elemtype temp;
For (I = 0; I <n-1; I ++)
{
Small = I;
For (j = I + 1; j <n; j ++)
If (X [J]. Key <X [small]. Key)
Small = J;

If (small! = I)
{
Temp = x [I];
X [I] = x [small];
X [small] = temp;
}
}
}

--------------------------
Bubble Sorting
Note: Two comparisons move the larger one back. By the first Bubble sorting, the records with the largest keywords in the N records to be sorted are ranked

The last position of the sequence. Then, the n-1-1 records in the sequence are sorted by bubble for the second time... For the sequence of N records

The Rows Are bubble sorted n times. The time complexity is O (n2 ).

Void bubblesort (elemtype X [], int N)

{
Int I, j, flag = 1;
Elemtype temp;
For (I = 1; I <n & flag = 1; I ++)
{
Flag = 0;
For (j = 0; j <n-I; j ++)
{
If (X [J]. Key> X [J + 1]. Key)
{
Flag = 1;
Temp = x [J];
X [J] = x [J + 1];
X [J + 1] = temp;
}
}
}
}

-----------------------------
Quick sorting
Note: partition exchange sorting is an improvement in the Bubble sorting method. The time complexity is O (nlog2n ).

Void quicksort (elemtype X [], int low, int high)

{
Int I, J;
Elemtype temp;

I = low;
J = high;
Temp = x [low];

While (I <j)
{

While (I <J & temp. Key <= x [J]. Key) j --;
If (I <j)
{
X [I] = x [J];
I ++;
}


While (I <J & X [I]. Key <temp. Key) I ++;
If (I <j)
{
X [J] = x [I];
J --;
}
}
X [I] = temp;


If (low <i-1) quicksort (x, low, I-1 );
If (J + 1 }

-------------------------
Merge Sorting
Note: the so-called Merge Sorting refers to the process of merging two or more ordered data sequences into one ordered data sequence.
The time complexity is O (nlog2n ).

Void Merge (R, L, M, H, R1, R2)
Sqlist R, R2;
Int L, M, h;
{
Int I, J, K;
K = L;
I = L;
J = m + 1;

While (I <= M & J <= H)
{
If (R [I]. Key <= R [J]. Key)
{
R2 [k] = R [I];
I ++;
}
Else
{
R2 [k] = R [J];
J ++;
}
K ++;
}
If (I> m)
While (j <= H)
{
R2 [k] = R [J];
J ++; k ++;
}
Else
While (I <= m)
{
R2 [k] = R [I];
I ++; k ++;
}
}

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.