Fast sequencing of AHA algorithm

Source: Internet
Author: User
Tags sorts

about efficiency issues

The bubble sort mentioned in the last chapter is our first real algorithm, he solves the problem that the bucket sort takes up too much space (because the bucket sort is to request the specified memory according to the number of sorts), but the bubble sort has sacrificed very much efficiency, suppose we want to order 100 million number, because the time complexity of the bucket sort is O (M+n), and the Bubble is O (N2) N Squared, assuming that the speed of the computer is 1 billion times per second, the bucket sort only takes 0.1 seconds, and bubble takes 10 million seconds, about 115 days, this is a very scary number, think can scare people.


Base NumberSuppose we want to 6,1,2,7,9,3,4,5,10,8, this is the number to sort, we first have to select a base number (do not be frightened by this number, this is what we take to make reference), we take 6 as the base number.
sorting principle explainedBelow we want to put the number than 6 to the right of 6, put 6 small number to the left, how to do it, this will use the previous bubble used method, is to compare and then swap position, but this time we start from both ends, assuming there are two Sentinel J and I. I start from 6, that is, the beginning of the team, J starting from 8, that is, the end of the team, and then the two Sentinels began to look in the middle of greater than, less than 6 of the number, Sentinel J first Start (why J first start, you can seriously think this is important), then after the first search: 3,1,2,5,4,6,7,9,10, 8, now realized greater than 6 on the right, less than on the left, 6 of the position has been determined, and then about 6 two are sorted (because 6 has been returned) to the left of the selection of 3 as the base number, and then from the left side of the 3,1,2,5,4 five number of the queue to start looking at the two ends, The result of the first sort is 2,1,3,5,4 followed by sorting at binary once. This is achieved the final sort, is always binary and then randomly select a number when the base number, of course, if the base number on the left, which starts from the right, if the right to start from the left, the only way to make the base number can be correctly returned, it is important to understand.
About efficiencyfast sorting is faster than the bubbling sort of each swap location is a jumping, each sorting set a base number, the less than the base number on the left, greater than the number of the base to the right, so that it does not have to be compared with the number of adjacent to a bubble sort at a time to compare the exchange location so the matter, The number of comparisons is much less, which increases efficiency. So the worst bubble sort is the number of neighbors to exchange, so that the time complexity of the bubble sort is the same as O (the square of N), its average time complexity is O (Nlogn), in fact, the fast sort embodies a "two-point" thought. All right, here's the code to get a good understanding.
Example
#include <stdio.h>int a[101],n;void quickSort (int left,int right) {//quick sort int i,j,t,temp;    if (Left > right) {//To determine the correctness of the parameter return;    } temp = A[left];    i = left;    j = right;        while (i! = j) {//determine if the encounter while (A[j] >= temp && i < J) {///right is less than the base number j--;        } while (A[i] <= temp && i < J) {//The left is not greater than the base number i++;            } if (I < J) {//Find the left is greater than the base number, the right is less than the base number to exchange T = A[i];            A[i] = A[j];        A[J] = t; }} A[left] = A[i];    Met, the base number position adjustment a[i] = temp; QuickSort (left, i-1);    The recursive method makes adjustments to the number on the left of the base number QuickSort (i + 1, right), or//recursive method to adjust the number of the base number of the}int main (int argc, const char * argv[]) {int i,n; scanf ("%d", &n);    Number of inputs for (i = 1; I <= n; i++) {//Input data scanf ("%d", &a[i]); } quickSort (1, N);    Sorts the number of inputs using quick sort for (i = 1; I <= n; i++) {//output sorted by printf ("%d", a[i]); }}

I believe you can read the code together with the comments, I will not elaborateThe result is:

The results can reflect my code, okay, good night, everybody.

Fast sequencing of AHA algorithm

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.