Algorithms and data Structures (16) Fast sequencing (Swift version 3.0)

Source: Internet
Author: User

On the previous blog we mainly talk about the more efficient merge sorting algorithm , this blog we will introduce another efficient sorting algorithm: Fast sorting . The idea of quick sorting is similar to the merge sort, which is sorted by the way of divide and conquer. the idea of quick sorting is to take the first value out of the unordered sequence, and then place the element that is larger than that value in front of the value by comparing the element that is smaller than the value. As a result, the data in front of the value is smaller than the value, and the data behind it is larger than that value. Then again to the first half and the second half of the unordered sequence of the above operations, so that the continuous operation, the scale of the disordered sequence is continuously reduced. When the scale of the problem is reduced to a certain extent, our sequence becomes orderly.

As we said before, when a problem can be divided into some of the same sub-problems, we can use the recursive return operation . So in the process of fast sequencing, we will gradually reduce the size of the problem by recursion, knowing that the sequence is sequential. This blog will give the process, according to, give the corresponding code implementation.

To split an unordered array

In this blog, let's talk first if you split the big question into some of the same sub-problems. We need to split the array that needs to be sorted, take a value out of the unordered sequence, and then, by comparison, place a value larger than that value behind it, smaller than that value, and put it in front of that value. In this section, we will give the corresponding and code implementation.

1. Splitting

Below is the process we have described above. It is also the process of quickly sorting the first round of sorting. First the first value in the unordered array is staged (temp = $), and after the following steps, we will put those elements that are smaller than 62 to the front of 62, and the elements larger than 62 are placed behind. Low is responsible for traversing the first half, putting the first half of the value greater than 62 in the back, while high is responsible for traversing the second half, and placing the second half of the value less than 62 in front . The specific steps are shown below.

  

2. Code implementation

According to the above, we can give the corresponding code implementation. If the above understanding, see the code below the implementation is relatively simple. The partition () function is responsible for converting an unordered array to the first value, and the smaller value to the front of the value, and the larger one behind the value. as shown below.

  

Second, quick sort

After we have implemented the split method, we should implement the code that is quickly sorted. The above code is the core of the fast line, the next thing to do is call the above function to split the unordered array, and then call the above function to split the order of the unordered decimal group, followed by execution, our array is ordered. is actually a recursive process. The quickSort () below is the process. First, the partition () method is not required to be split, and then the quickSort () method is called again to execute the first half, and the same call to the QuickSort () method executes the second half. The code is shown below.

  

After defining the core method of fast sorting, the next step is to use it. The QuickSort below is the corresponding fast sort class,QuickSort still follows the SortType sort protocol, and sort () The method is the interface of the external invocation as defined in the protocol. The specific code is shown below.

  

Third, test cases

With my QuickSort class followed the SortType method, we can still use the previous test case. Below is our test case, which has been used all along, just need to pass the object of the QuickSort class to our test function, as follows:

  

The results of this blog quick sort run are as follows:

  


This blog on the heap sort of introduction to the first here, the next blog we will introduce the "Cardinal sort" of the detailed content. The relevant code for this blog will still be shared on GitHub, with GitHub sharing the address below, as shown below:

GitHub code share address:https://github.com/lizelu/DataStruct-Swift/tree/master/AllKindsOfSort

Algorithms and data Structures (16) Fast sequencing (Swift version 3.0)

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.