快速排序——day5

19. April 2017 深度学习 0
//第五天 快速排序

public class quickSort_day5 {

staticfinalintMax = 10;

staticint[] array1 = new int[Max];

public static void main(String[] args) {

int i;

for (i = 0; i < Max; i++) {

array1[i] = (int) (100 + Math.random() * (100 + 1));

}

System.out.println("排序前数据为:");

for (i = 0; i < Max; i++) {

System.out.print(array1[i]+" ");

}

System.out.println("\n");

quicktSort(0, Max - 1);

System.out.println("排序后数据为:");

for (i = 0; i < Max; i++) {

System.out.print(array1[i] + " ");

}

}

privatestaticvoid swap(int x, int y) {

int tep = array1[x];

array1[x] = array1[y];

array1[y] = tep;

}

privatestaticvoid quicktSort(int start, int end) {

if (start >= end)

return;

int temp;

int left, right;

left = start;

right = end - 1;

temp = array1[end];

while (left < right) {

while (array1[left] <= temp && left < right) {

left++;

}

while (array1[right] >= temp && left < right) {

right--;

}

swap(left, right);

}

if (array1[left] >= array1[end]) {

swap(left, end);

}

else {

left++;

}

quicktSort(start, left - 1);

quicktSort(left + 1, end);

}

}