Quick sorting of Aha Algorithms

Source: Internet
Author: User

Quick sorting of Aha Algorithms
Efficiency issues

The Bubble Sorting mentioned above can be said to be our first real algorithm, it solves the problem that bucket sorting occupies a large amount of space (because bucket sorting requires a specified memory according to the number of sorted data), but Bubble Sorting sacrifices a lot of efficiency, suppose we want to sort the number of 0.1 billion, because the time complexity of Bucket sorting is O (M + N), while the bubble is the square of O (N2) N, assume that the computer speed is 1 billion times per second, the bucket sorting takes only 0.1 seconds, and the bubble processing takes 10 million seconds, or about 115 days. This is a terrible number.


Let's assume that we want to sort 6, 1, 2, 7, 9, 3, 4, 5, 10, and 8. This is the number. First, we need to select a reference number (Do not be scared by this number, this is what we use for reference). Let's take 6 as the benchmark number.
The sorting principle is explained below. We need to put a number larger than 6 to the right of 6 and a number smaller than 6 to the left. How can we do this? We need to use the method used in the previous article bubble, it is to compare and switch the positions, but this time we start from the two ends, assuming there are two sentinns j and I. I started from 6, that is, the start of the team, j started from 8, that is, the end of the team, and then the two guards started to look for numbers greater than or less than 6 in the middle, sensen j sets out first (Why does j set out first? You can think about it very seriously). After the first search is completed, now we have achieved the position of greater than 6 on the right side, smaller than on the left side, and the position of 6 has been determined, and then we start to sort the positions of about 6 (because 6 has been reset) select 3 on the left as the reference number, and then start searching from the two ends of the queue consisting of 3, 1, 2, 5, and 4 on the left. The result after the first sorting is, 4. Then, sort in half a fold. In this way, the final sorting is achieved, that is, the half is always folded and then a number is randomly selected as the benchmark number. Of course, if the benchmark number is left, it starts from the right, and if it is right, it starts from the left, it is important to understand that only in this way can the benchmark number be correctly restored.
The reason why efficiency and fast sorting is faster is that compared to Bubble sorting, each switching position is in a leaping manner. A benchmark number is set for each sorting, and a benchmark number smaller than the benchmark number is placed on the left, and a benchmark number greater than the benchmark number is placed on the right, in this way, it will not be as troublesome to compare and exchange locations with adjacent numbers at a time as the Bubble sorting method. The number of comparisons is much less, which improves the efficiency. Therefore, the worst case of Bubble Sorting is the exchange of adjacent numbers, which is the same as the time complexity of Bubble sorting, that is, O (N square ), its average time complexity is O (nlogN). In fact, fast sorting reflects a "binary" idea. Okay. The code below will help you understand it.
Instance
# Include <stdio. h> int a [101], n; void quickSort (int left, int right) {// fast sorting int I, j, t, temp; if (left> right) {// determine the correctness of the parameter return;} temp = a [left]; I = left; j = right; while (I! = J) {// determine whether to encounter while (a [j]> = temp & I <j) {// whether the value on the right is smaller than the reference number j --;} while (a [I] <= temp & I <j) {// is the value on the left greater than the baseline I ++;} if (I <j) {// find the value greater than the reference number on the left and the value smaller than the reference number on the right to switch t = a [I]; a [I] = a [j]; a [j] = t;} a [left] = a [I]; // when the baseline is met, adjust the position of the baseline number. a [I] = temp; quickSort (left, I-1); // The Recursive Method adjusts the number on the left of the baseline number to quickSort (I + 1, right ); // recursive method to adjust the number on the right of the reference number} int main (int argc, const char * argv []) {int I, n; scanf ("% d ", & n); // number of inputs for (I = 1; I <= n; I ++) {// scanf ("% d ", & a [I]) ;}quicksort (1, n); // sort the number of inputs in quick order for (I = 1; I <= n; I ++) {// output the number of sorted printf ("% d", a [I]);}

I believe that you can understand the code with comments. I will not elaborate on the result as follows:

The result also shows that the code is okay. Okay, good night.

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.