C # Data Structure -- Sort [Top],

Source: Internet
Author: User

C # Data Structure -- Sort [Top],

Overview

After reading the sorting content for several days, I will share some common sorting methods with you.

  What is sorting?

Sort by personal understanding: Compare the values in the array, and finally obtain an ordered array by switching the positions. Sorting is divided into memory sorting and external sorting. The sort methods for this sharing are all sorted by memory.

What is the stability of sorting?

Assume that there are multiple records with the same keywords in the record sequence to be sorted. If the records are sorted, the relative sequence of these records remains unchanged, that is, in the original sequence, ri = rj, and ri before rj, while in the sorted sequence, ri is still before rj, it is called this sort algorithm is stable; otherwise it is called unstable.

Common sorting:

Bubble sorting, select sorting, insert sorting directly, Hill sorting, heap sorting, Merge Sorting, and quick sorting.

The following figure shows the sorting relationship:

 

Bubble Sort)

Sorting Ideology: Compares the keywords of adjacent records in pairs. If the reverse order is used, it is exchanged until there is no reverse order record.

Complexity:O (n ^ 2 ).

Stability:Stable.

Bubble Sorting is the first sorting algorithm I have come into contact with. It is easy to understand. Sorting entry-level, easy to understand, sorting by constantly switching locations.

Code example:

Int [] list = {50, 10, 90, 30, 70, 40, 20}; int temp; for (int I = 0; I <list. length; I ++) {for (int j = I + 1; j <list. length; j ++) {if (list [I]> list [j]) {temp = list [I]; list [I] = list [j]; list [j] = temp ;}} Console. writeLine ("Bubble sorting result: {0}", string. join (",", list ));

The first for loop obtains a value list [I] in the data, and the second for loop begins with the value list [j] after the next value (j = I + 1) in the first for loop. Then compare the size list [I]> list [j] of the two values in sequence. If the value in the front of the array is greater than the value in the back, replace the positions of the two values. Always keep the value on the left of the array less than the value on the right.

Bubble Sorting is not shared here for many other versions.

 

Simple Selection Sort)

Sorting Ideology: Each trip is in n-I + 1 (I = 1, 2 ,... N-1) records with the smallest keyword are selected as the I-th record in the sequence.

Complexity:O (n ^ 2 ).

Stability:Stable.

Code example:

Int [] list = {50, 10, 90, 30, 70, 40, 20}; int minIndex, temp; for (int I = 0; I <list. length; I ++) {minIndex = I;/* considers the value of the current loop as the minimum value */for (int j = I + 1; j <list. length; j ++) {if (list [j] <list [minIndex]) {minIndex = j; /* after N cycles, we find the minimum value and assign it to minIndex */} if (minIndex! = I) {temp = list [minIndex];/* replace the minimum value of the current loop with list [I. The data on the left is smaller than the data on the right */list [minIndex] = list [I]; list [I] = temp ;}} Console. writeLine ("result of sorting: {0}", string. join (",", list ));

 

In the first for loop array, the element list [I] sets the default value of the current loop to the minimum value. The record subscript is assigned to minIndex. The second for loop starts with the next value (j = I + 1) in the first for loop. The Value list [j]. Then compare it with list [minIndex] in sequence. If list [j] <list [minIndex] assigns the subscript of j to minIndex (keep minIndex as the subscript of the minimum value in the loop ). After the second for loop ends, replace the values of list [I] and list [minIndex] in the current loop. Always keep the value on the left of the array less than the value on the right.

The selected sorting is less frequently than the Bubble sorting and the sorting performance is slightly better.

 

Insert directly to sort (Straight Insertion Sort)

Sorting Ideology:

Each time the first element is extracted from the unordered table, it is inserted to the proper position of the ordered table, so that the ordered table is still ordered.
First, compare the first two numbers, and then insert the second number into the ordered table by size;
The second step is to scan the third data and the first two data, and insert the third data to the ordered table by size. The second step is to perform (n-1) after scanning, the entire sorting process is completed.

Complexity:O (n ^ 2 ).

Stability:Stable.

 Code example:

Int [] list = {50, 10, 90, 30, 70, 40, 20}; int temp, j; for (int I = 1; I <list. length; I ++)/* starts from the second bit of data and loops [unordered sequence] */{if (list [I] <list [I-1]) {temp = list [I]; for (j = I-1; j> = 0 & temp <list [j]; j --) /* [ordered sequence] */{list [j + 1] = list [j];} list [j + 1] = temp ;}} Console. writeLine ("insert the sorting result directly: {0}", string. join (",", list ));

The first for loop (starting from the second position of the array) element list [I] in the array, declare the Temporary Variable temp record list [I] if the current value of the loop list [I] is smaller than the value list [I-1] in the array. Then, based on the I value of the first for loop and the reverse loop array, query the subscript location smaller than list [I. Replace the value of list [j + 1.

 

Related Article

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.