[Data Structure] Quick Sort

  1. Select a reference element, usually select the first element or the last element, through a scan, divide the sequence to be sorted into two parts, one is smaller than the reference element, and the other is greater than or equal to the reference element.
  2. At this time, the reference element is in the correct position after sorting, and then the two parts are sorted recursively in the same way.
public class quickSort {

inta[]={49,38,65,97,76,13,27,49,78,34,12, 64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};

public quickSort(){

quick(a);

for(int i=0;i
System.out.println(a[ i]);

}

public int getMiddle(int[] list, int low, int high) {

int tmp = list[ low]; //The first of the array is used as the central axis

while (low
while (low = tmp) {

high--;

}

list[low] = list[high]; //The record smaller than the middle axis is moved to Low-end

while (low
low++;

}

list[high] = list[low]; //The record larger than the middle axis is moved to the high end

}

list[l ow] = tmp; //The bottom line is recorded to the end

return low; //The position of the bottom line is returned

}

public void _quickSort (int[] list, int low, int high) {

if (low
int middle = getMiddle(list, low, high); // Divide the list array into two

_quickSort(list, low, middle-1); //recursively sort the low word list

_quickSort(list, middle + 1, high); //Recursively sort the high word list

}

}

public void quick(int[] a2) {

if (a2.length> 0) {//Check if the array is empty

_quickSort(a2, 0, a2.length-1);
< br />}

}

}

Leave a Comment

Your email address will not be published.