Step-by-step write algorithm (non-recursive ordering)

Source: Internet
Author: User

Original: Step-by-step writing algorithm (non-recursive ordering)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "

In one of the previous blogs, we found that the performance of normal find and sort lookups varies greatly. As a 1 million of the data, if the use of ordinary search methods, then each data lookup on average will be hundreds of thousands of times, then the binary search, 20 times can be done. The difference between the two is very obvious. Since the sorting has such a good effect, then in this blog, we will make a summary of the sorting calculation.

According to my own understanding, the sorting can be divided into two kinds: a kind of non-recursive ordering, it mainly according to the method of the recursive data sorting, that is, the main data shift and loop to complete; the other is the recursive method, we arrange the current data in order to arrange the data first, then the current data will be arranged. This method of constant recursive invocation is recursive ordering.

There are many methods of non-recursive ordering, such as bubble sort, insert sort, hill sort, and recursive methods, the methods described here are quick sorting, merging sorting and heap sorting. Sort of a lot of content, this blog mainly introduces non-recursive ordering, the content of the recursive ordering is mainly resolved in the next section.

(1) Bubble sort

The content of the bubbling sort is not complicated. Assuming that there are n data that need to be sorted, then we need to determine the number of n large-to-small data, each time we pick the nth-largest data, and enlarge the corresponding position. Until all the data is neatly arranged, our sorting is over.

void Bubble_sort (int array[], int length) {int inner = 0, outer = 0;int median = 0;if (NULL = = Array | | 0 = = length) return;f or (outer = length-1; outer >= 1; outer-) {for (inner = 0; inner < outer; inner + +) {if (Array[inner] > Array[inner + 1]) {median = Array[inner];array[inner] = array[inner + 1];array[inner + 1] = median;}}}
So is there any improvement in this program? Of course there is, if it is found in a traversal cycle, if there is no shift phenomenon, then it can be judged that this sort can be ended? Friends can think about this problem?

void Bubble_sort (int array[], int length) {int inner = 0, outer = 0;int Median = 0;int flag = 1;if (NULL = = Array | | 0 = = LE  ngth) return;for (outer = length-1; outer >= 1 && flag; outer-) {flag = 0;for (inner = 0; inner < outer; inner + +) {if (Array[inner] > Array[inner + 1]) {median = Array[inner];array[inner] = array[inner + 1];array[inner + 1] = media N;IF (flag = = 0) flag = 1;}}}

(2) Insert Sort

Inserting sort means that we divide the data into two parts, some of which are already sorted, and some of the data that is not currently finished. So, the process of sorting is not the process of inserting unsorted data into a queue that is already queued. You can try it on your own first, and then look at my code, right?

void Insert_sort (int array[], int length) {int inner = 0;int Outer = 0;int median = 0;if (NULL = = Array | | 0 = = length) retur N;for (outer = 1; Outer <length; outer + +) {for (inner = outer; inner >= 1; inner--) {if (Array[inner] < Array[inner -1]) {median = Array[inner];array[inner] = array[inner-1];array[inner-1] = median;} Else{break;}}}}
So does the insertion sort have an improved method like bubble sort? Not really. Because the position of each insertion sort is the result of a local comparison, the content of the bubbling sort is globally optimal. This can be seen from the number of data comparisons.


(3) Hill sort

Hill sort, I personally think can be seen as a bubble sort of variant. The basic idea is that the order is gradually sorted by a descending sequence method. For example, there are 10 data, which we order in sequence 5, 3, 1. The first is 5, then we arrange for 1 and 6, 2 and 7, 3 and 8, 4 and 9, 5 and 10, the second is 3, then the data 1, 4, 7, 10, and 2, 5, 8, and 3, 6, 9, and the third round is the same as bubble sort, which arranges each data. Its advantage is to make the whole queue basically orderly, reduce the number of data movement, thus reducing the computational complexity of the algorithm.

void Shell_sort (int array[], int length, int step) {int inner = 0;int Outer = 0;int median = 0;if (NULL = = Array | | 0 = len GTH) return;for (; step >= 1; step-=2) {for (int index = 0; index < step; index + +) {if ((length-1) < (index + step)) Continue;else{outer = index + step;while ((outer + Step) <= (length-1)) outer + = step; for (;  Outer >= (index + step);  Outer-= Step) {for (inner = index; inner <= outer-step; inner + = Step) {if (Array[inner] >= Array[inner + step]) {Medi an = Array[inner];array[inner] = array[inner + Step];array[inner + step] = median;}}}}}

Summary:

(1) The above sorting is non-recursive program, it is not difficult to understand, but the details of the problem needs attention, especially the length of the problem

(2) When writing code, it is important to pay attention to the design of test cases

(3) If possible, use more code and functions that have already been validated


"Preview: Next Blog Introduction to Quick-sort content"


Step-by-step write algorithm (non-recursive ordering)

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.