A fast sort instance _java of Java algorithm start

Source: Internet
Author: User

Quick sort a listen to the name may feel fast, but his algorithm time complexity is the worst case is the same as the insertion order. The reason for the quick sort is that his average efficiency is faster than the heap sort, and the fast sort is based on the idea of splitting and merging, but the quick sort is the original one, and there is no need to open up new storage space directly in the primitive array operation. The idea of a quick sort is simple, is to select a keyword K to divide the original array into two G1 and g2,g1 all elements are smaller or equal than K, and all the data in G2 is larger or equal than K, so the G1 and G2 are sorted quickly, and finally we get an ordered array. The sort method in the code is the description of the statement just now. The key algorithm is to find the location of K to divide the original array into two parts of the size of the process. Method Getplocation is the core of fast sorting. His principle of implementation is a bit like inserting sort just like. Each time the element in the end position in the map is used as the keyword, the array is divided into two parts by contrast to the end element, while I and J are two split lines, and the number between I and J is larger than the core, I and J are like a greedy snake, when J's next number is bigger than the core j+ The length of the 1,i to J increases, and if it's smaller than the core, I and J go forward and put that decimal in front of I. After this cycle, start to end-1 is divided by size, and finally put the core in the middle, the position of the core to return is the dividing line.

Copy Code code as follows:

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<end) {
int p=getplocation (map, start, end);
Sort (map, start, p-1);
Sort (map,p+1,end);
}
}
public static void Main (string[] args) {
Int[] Map=new int[]{4,1,5,3,7,12,65,7};
QuickSort qs=new QuickSort ();
Qs.sort (map, 0, map.length-1);
for (int i = 0; i < map.length; i++) {
System.out.println (Map[i]);
}
}
}

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.