Data structure and algorithm learning six, non-recursive ordering

Source: Internet
Author: User

http://blog.csdn.net/feixiaoxing/article/details/6844826

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.

voidBubble_sort (intArray[],intlength) {    intInner =0, outer =0; intMedian =0; if(NULL = = Array | |0==length)return;  for(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; }        }    }}

(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?

voidInsert_sort (intArray[],intlength) {    intInner =0; intOuter =0; intMedian =0; if(NULL = = Array | |0==length)return;  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. Its basic ideas are:

The first step is to sort by a sequence descending method. For example, there are 10 data, which we order in sequence 5, 3, 1. First is 5, then we arrange for 1 and 6, 2 and 7, 3 and 8, 4 and 9, 5 and 10;

The second round is 3, then the data 1, 4, 7, 10 arranged, and then 2, 5, 8 arranged, and 3, 6, 9 arranged;

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.

voidShell_sort (intArray[],intLengthintStep) {    intInner =0; intOuter =0; intMedian =0; if(NULL = = Array | |0==length)return;  for(; step >=1; Step-=2){         for(intindex =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]) {Median=Array[inner]; Array[inner]= Array[inner +Step]; Array[inner+ Step] =median; }                }            }        }    }}

Summarize:

(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"

Data structure and algorithm learning six, 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.