Write algorithms in one step (non-recursive sorting)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

In the previous blog, we found that the performance of common search and sort search is very different. As a 1 million data, if a common search method is used, each data query requires hundreds of thousands of times on average. Therefore, the binary search can be done more than 20 times. The difference is very obvious. Since sorting has such a good effect, we will summarize the sorting calculation in this blog.

 

According to my personal understanding, sorting can be divided into two types: Non-recursive sorting, which sorts data mainly by non-recursive method, that is, the shift and loop of the main data are completed; another method is recursion. When we sort the current data, we first sort the sub-data in order before we can sort the current data. This method of continuous recursive calling is recursive sorting.

 

There are many non-recursive sorting methods. Here we mainly introduce Bubble sorting, insert sorting, and Hill sorting. There are also many recursive methods. Here we will introduce fast sorting, Merge Sorting, and heap sorting. There are many sorting contents. This blog introduces non-recursive sorting. The content of recursive sorting is mainly solved in the next section.

 

(1) Bubble Sorting

 

The content of the bubble sort is not complex. Suppose there are n pieces of data to be sorted, then we need to determine the number of n pieces of data from big to small. Each time we select the number of n pieces of data, and enlarge the corresponding position. Until all the data is arranged neatly, the sorting is over.

 

 

Void bubble_sort (int array [], int length)

{

Int inner = 0, outer = 0;

Int median = 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;

}

}

}

}

Void bubble_sort (int array [], int length)

{

Int inner = 0, outer = 0;

Int median = 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;

}

}

}

} Is there any improvement for this program? Of course, if we find that there is no shift in a traversal loop, can we judge whether this sorting can end? Can you 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 = length)

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] = median;

 

If (flag = 0)

Flag = 1;

}

}

}

}

Void bubble_sort (int array [], int length)

{

Int inner = 0, outer = 0;

Int median = 0;

Int flag = 1;

 

If (NULL = array | 0 = length)

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] = median;

 

If (flag = 0)

Flag = 1;

}

}

}

}

(2) Insert sorting

 

Insert sorting means that we divide the data into two parts, one is the sorted data, and the other is the data that has not been sorted yet. In this case, is the sorting process to insert unsorted data one by one into a queue that has already been sorted. You can give it a try first, and then look at my code again, right?

 

 

Void insert_sort (int array [], int length)

{

Int inner = 0;

Int outer = 0;

Int median = 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;

}

}

}

}

Void insert_sort (int array [], int length)

{

Int inner = 0;

Int outer = 0;

Int median = 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;

}

}

}

} Is there any improvement method like bubble sort for insertion sorting? Actually no. Because the position of each insert sort is the result of local comparison, and the content of each bubble sort is global optimal. This can be seen from the number of data comparisons.

 

 

 

 

(3) Hill sorting

 

I personally think it can be seen as a variant of the bubble sort. The basic idea is: first, gradually sort by a method of descending sequence. For example, if there are 10 pieces of data, we sort the data in the sequence 5, 3, and 1. The first is 5, so we arrange 1 and 6, 2 and 7, 3 and 8, 4 and 9, 5 and 10; the second round is 3, then arrange data 1, 4, 7, and 10, then arrange data 2, 5, and 8, and arrange data 3, 6, and 9. The third round is the same as the Bubble sorting, to sort each data. Its advantage is to keep the entire queue in order, reduce the number of data moves, and thus reduce the computing 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 = length)

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]) {

Median = array [inner];

Array [inner] = array [inner + step];

Array [inner + step] = median;

}

}

}

}

}

}

Void shell_sort (int array [], int length, int step)

{

Int inner = 0;

Int outer = 0;

Int median = 0;

 

If (NULL = array | 0 = length)

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]) {

Median = array [inner];

Array [inner] = array [inner + step];

Array [inner + step] = median;

}

}

}

}

}

}

Summary:

 

(1) The sorting above is a non-recursive program, which is not difficult to understand, but attention should be paid to the details, especially the length issue.

 

(2) Pay attention to the design of test cases when writing code.

 

(3) If possible, use more verified code and functions.

 

 

 

 

[Notice: The next blog will introduce the content of quick sorting]

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.