Quick sort:
/*
* Quick Sort
*/
public class QuickSort {
/**
* Divide arrays
*/
public static int partition (long arr[],int left, int. Right,long point) {
int leftptr = left-1;
int rightptr = right;
while (true) {
Loop, will be smaller than the keyword left in the left side
while (Leftptr < rightptr && Arr[++leftptr] < point);
Loop, will be larger than the keyword left on the right side
while (Rightptr > Leftptr && arr[--rightptr] > point);
if (leftptr >= rightptr) {
Break
} else {
Long tmp = arr[leftptr];
ARR[LEFTPTR] = arr[rightptr];
ARR[RIGHTPTR] = tmp;
}
}
Swap the keyword with this one referred to by the current leftptr
Long tmp = arr[leftptr];
ARR[LEFTPTR] = Arr[right];
Arr[right] = tmp;
return leftptr;
}
public static void Displayarr (long[] arr) {
System.out.print ("[");
for (long Num:arr) {
System.out.print (num + "");
}
System.out.print ("]");
System.out.println ();
}
public static void sort (long[] arr, int left, int. right) {
if (right-left <= 0) {
Return
} else {
Set keywords
Long Point = Arr[right];
Get pointcuts and divide arrays
int partition = partition (arr, left, right, point);
Quickly sort the sub-array on the left
Sort (arr,left,partition-1);
Quickly sort the sub-array on the right
Sort (arr,partition + 1, right);
}
}
}
Test:
public class Testquicksort {
public static void Main (string[] args) {
long[] arr = new LONG[10];
for (int i = 0; i < 10;i++) {
Arr[i] = (long) (Math.random () * 99);
}
Quicksort.displayarr (arr);
Quicksort.sort (arr, 0, arr.length-1);
Quicksort.displayarr (arr);
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696506
Grinding data structures and algorithms-09 Quick Sort