[7] algorithm path-3-axis Algorithm for fast sorting

Source: Internet
Author: User

Left axis, center axis, and right axis

Question:

Quick Sort is currently one of the most recognized fastest sorting methods (depending on the problem-solving objects). Although Quick Sort can reach O (n2) in the worst case ), however, in most cases, the efficiency of quick sorting is quite good.


Quick sorting-Algorithm

1. The basic spirit of quick sorting is to find the appropriate axis in the series, and then split the series into two.

2. Sort the columns on the left and right respectively.


Left axis calculation:

// Quick sorting-Left axis calculation // 1. with two pointers left/right, and set the number of leftmost to the initial pivot vertex // 2. search from the right to left, find the first number smaller than the limit, and cache it to the Pivot Position // 3. search from left to right, find the first number greater than the number of shards, and cache it into a location smaller than the number of shards. 2. loop // 4. the valid null position obtained after the last exchange is a [I], and then the value of I is assigned to a [I]. At this time, the number on the left of a [I] is smaller than that on a [I, the number on the right is greater than a [I] // sort int quicksort2 (int A [], int left, int right) on the left and left respectively) {If (left <right) {int I = left, j = right, fig = A [left]; while (I <j) {While (I <J & A [J]> = javastloc) j --; if (I <j) A [I ++] = A [J]; while (I <J & A [I] <pivotloc) I ++; if (I <j) A [j --] = A [I];} A [I] = repeated tloc; quicksort2 (A, left, I-1); quicksort2 (A, I + 1, right);} return 0 ;}
int QuickSort5(int a[],int left,int right){if(left<right){int i = left,j=right,pivotloc=a[left];while(i < j){while(i<j && a[i]<=pivotloc) i++;while(i<j && a[j]>pivotloc) j--;if(i<j){SWAPER(a[i],a[j]);i++;j--;}}if(a[j]<a[left]) {SWAPER(a[left],a[j]);}QuickSort5(a,left,j-1);QuickSort5(a,j,right);}return 0;}

Central Axis Calculation

// Quick sorting int quicksort3 (int A [], int left, int right) {int I, j, distinct; If (left <right) {cursor = A [(left + right)/2]; I = left-1; j = right + 1; while (I <j) {While (A [++ I] <signature); While (A [-- J]> signature); if (I <j) swaper (A [I], A [J]);} quicksort3 (A, left, I-1); quicksort3 (A, J + 1, right);} return 0 ;}


Right axis Calculation

// Quick sorting calculation // 1. select the rightmost element as the pivot character // 2. search from left to right, find the number smaller than limit, and switch it with the first unexchanged element on the left. // 3. when the rightmost result is found, the number of exchanges on the left side is smaller than that on the left side, and the number of unexchanged values is larger than that on the left side. // 4. swap the first unexchanged element on the left with the unexchanged element on the rightmost, and return the index of the unexchanged element. // at this time, the element on the left of I is smaller than that on the I, larger than I on the right. // enter the program. the loop recursively performs (fast sorting) on 0 → I-1 and the rightmost I + 1 → right respectively. Eventually, the sequence will become an ordered int partition2 (int A [], int left, int right) {int I = left, J; int right = A [right]; for (j = left; j <right; j ++) {if (a [J] <strong) {if (I! = J) swaper (A [I], a [J]); I ++ ;}} if (I! = Right) swaper (A [I], a [right]); return I;} int quicksort4 (int A [], int left, int right) {int comment; if (left <right) {partition = partition2 (A, left, right); quicksort4 (A, left, semi-1); quicksort4 (A, semi + 1, right);} return 0 ;}



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.