# java Seven Sorts - 6_Quick Sort

Keywords: Java less

## 1. Quick Sorting:

Find a baseline element in the elements to be arranged, then compare the baseline element with other elements, base on the baseline element, put the element that is more accurate behind, less than
The element of the base is placed in front.Then quickly sort the good left and right two small areas
There are two ways to divide the intervals by the base elements:
First, set two reference variables less,great, less to iterate back and forth from the first element until the current element found is larger than the base element.
Then let great iterate forward from the last element until it finds that the current element is less than the base element, swapping the values that the current less and great point to.
Then start with less, repeat the above actions, and the condition for the end of traversal is less >=great;
At the end of the traversal, the value pointed to by the current less (or great) is exchanged with the value of the base element.Next small interval search

## 2. Diagram       Note: The range of the two newly divided intervals is:
Paragraph 1: The original left goes before the final position of the last base element; that is, [left,pivotIndex-1]
Paragraph 2: The last bit of the last base element's final position goes to its original right; that is, [pivotIndex+1,right]

The end result: ## 3. Code implementation

```public static void quickSort ( int[] array){
int left = 0;
int right = array.length - 1;
quickSortInternal1(array, left, right);
}
public static void quickSortInternal ( int[] array, int left, int right){
if (left >= right) {
return;
}
int pivotIndex = partion1(array, left, right);//Functions for finding base values
// int[] indice=partion4(array,left,right);
// quickSortInternal(array,left,indice-1);
// quickSortInternal(array,indice+1,right);
quickSortInternal(array, left, pivotIndex - 1);//Attention interval range
quickSortInternal(array, pivotIndex + 1, right);

}
private static int partion1 ( int[] array, int left, int right){
int pivot = array[right];
int less = left;
int great = right;
while (less < great) {
while (less < great && array[less] <= pivot) {
less++;
}
while (less < great && array[great] >= pivot) {
great--;
}
swap(array, less, great);
}
swap(array, less, right);
return less;
}```

The second type: digging
Find the base element pivot, set two variables less and great,less to traverse backwards from the first number until you find a number larger than pivot, stop, and place the value of array[less] at array[great].(that is, array[big]=array[less])
Then let right traverse forward from the last number in the current interval until it finds a number less than pivot, stops, and performs the array[less]=array[great].Then less++ iterates backwards, repeating the above operation, ending with left>=right;
Assign the pivot value to the current less(great) array element at the end
Diagram:     Note: pivot base elements are optional, but for illustration purposes, select the last element of the interval each time
final result ## code implementation

```private static int partion1 ( int[] array, int left, int right){
int pivot = array[right];//Baseline value
int less = left;
int great = right;
while (less < great) {
while (less < great && array[less] <= pivot) {
less++;
}
array[great] = array[less];
while (less < great && array[great] >= pivot) {
great--;
}
array[less] = array[great];
}
array[less] = pivot;
return less;
}```

Posted by poe on Wed, 04 Sep 2019 16:15:22 -0700