Quick Algorithm sorting

Source: Internet
Author: User

Quick sorting may feel fast when you hear this name, but the worst time complexity of his algorithm is the same as inserting sorting. The reason for quick sorting is that the average efficiency is faster than the heap sorting, and the fast sorting is based on the grouping idea and the Merge Sorting, but the quick sorting is the original address, you do not need to open up new buckets directly in the original array. The idea of fast sorting is very simple, that is, to select a keyword k to divide the original array into two parts g1 and g2, all elements in g1 are smaller or equal to k, all the data in g2 is larger than k or equal to k. In this way, g1 and g2 are sorted quickly, and what we get is an ordered array. The sort method in the Code is the description of the statement just now. The key algorithm is to find the k position and divide the original array into two parts: size. The getPlocation method is the core of fast sorting. Its implementation principle is a bit like insert sorting, but a little like. Each time, the element at the end position in the map is used as the keyword. The array is divided into two parts by comparison with the end element, while the I and j are two split lines, the number between I and j is a larger element than the core. I and j are like a greedy snake. When the next number of j is larger than the core, j + 1, the length of I to j is increased. If it is smaller than the core, I and j move forward and place the decimal point in front of I. After this loop, the start to end-1 is separated by size, and the core is placed in the middle. The return of the core is the dividing line.

public class QuickSort {public int getPlocation(int[] map,int start,int end){int core=map[end];int i=start-1;for(int j=start;j<=end-1;j++){if(map[j]<=core){i++;int cache=map[j];map[j]=map[i];map[i]=cache;}}i++;map[end]=map[i];map[i]=core;return i;}public void sort(int[] map,int start,int end){if(start
 
  


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.