Quick Sort Java

Source: Internet
Author: User
Set the array to sort is a[0] ... A[N-1], first select any data (usually the first number of arrays) as the key data,
Then put all the smaller numbers in front of it, all the larger numbers behind it, and this process is called a quick sort.
Then proceed to the preceding and subsequent sections of the keyword before the previous steps.
A quick sort of algorithm is:
1 set two variables I, J, the start of the order: i=0,j=n-1;
2 the first array element as the key data, assign value to key, namely key=a[0];
3 starting from the beginning of the search, that is, from the start forward search (j--), find the first value less than key A[j], will a[j] and A[i] interchange;
4 from I start backward search, that is, from the front start backward search (i++), find the first larger than key A[i], will a[i] and A[j] interchange;
5 Repeat the 3rd, 4 steps until the I=J (3,4 step, not find a qualifying value, that is, 3 of a[j] is not less than the key,4 in a[i) when not more than key change the value of J, I,
Make j=j-1,i=i+1 until it is found. Find the value that matches the condition, and the position of the J pointer is unchanged when the exchange is made.
In addition, the I==J process must be exactly the time the i+ or j-completes, at which point the loop ends.
The following is a quick sort implementation that selects the first element as a hub element:
public static void QuickSort (int[] datas, int low, int high) {if (Low < high) {int privot = Getprivot (datas, Low,
			High);
			QuickSort (datas, Low, privot-1);
		QuickSort (datas, Privot + 1, high);
		} private static int Getprivot (int[] datas, int low, int high) {int temp = Datas[low];
			while (Low < high) {while (Low < high && Datas[high] > Temp) {high--;
			} Datas[low] = Datas[high];
			while (Low < high && Datas[low] < temp) {low++;                                                                                                                                                                                                                     } Datas[high] = Datas[low];                                                                                                                          } Datas[low] = temp;
	return to Low; }


Complexity of Time:
Worst case: Pivot element is always the smallest element, O (n^2)
public static void QuickSort (int[] datas, int low, int high) {//t (N)
if (Low < high) {
int privot = Getprivot (datas, Low, high); CN here on both sides of low, Hight moved to the middle, the total number of moves N
QuickSort (datas, Low, privot-1); T (i)
QuickSort (datas, Privot + 1, high); T (n-i-1)
}
}
---> t (N) = t (i) + t (n-i-1) + CN
Order i = 0,
---> t (N) = t (N-1) + CN + 1 (t (0) = 1)
Ignore T (0) = 1,
--->
T (N) = t (N-1) + CN
T (N-1) = t (N-2) + C (N-1)
T (N-2) = t (N-3) + C (N-2)
....
T (2) = t (1) + C (2)
Add both sides
--->
T (N) = t (1) + = O (n^2)
Best case: Hub element Right in the middle, O (n log n)
Suppose two sub arrays are just half of the original array
The
T (N) = t (i) + t (n-i-1) + CN
T (N) = t (N/2) + t (N/2) + CN = 2 T (N/2) + CN
--->
T (N)/n = = 2 T (N/2)? N + C
...
--->
T (N)/n = t (1)/1 + C Logn
T (N) = Cnlogn + N = O (NLOGN)
Average situation: O (n log n)
Selection of Pivot elements:
It is usually possible to choose the first element as the pivot element, and if it is random at the time of input, then this is possible, but if the input is in a predefined or reverse order, then such a hub will produce a relatively inferior
Segmentation. If the first element is used as a hub element and is entered in the sort, the quick sort takes two of times. Pre-sorted input or a sort of input is very common. So it is not advisable to choose the first one to do the pivot element.
A safe scheme is to select the pivot element randomly. Random pivot Element (random number generator) cannot always produce inferior pivot element continuously, but the generation of random number is very expensive, it can't reduce the average running time of the remainder of the algorithm at all.

Three-digit median segmentation method: the best choice for pivot element is to select the median of the array. However, this is difficult to work out and will significantly slow down the speed of sorting. Such median estimates can be obtained by randomly selecting three elements and using their median digits. You can generally select the median number of three elements in the left, right, and center position as pivot elements.




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.