快速排序随机化版本 Java实现 newQuickSort


public class newQuickSort {

/**
* 快速排序的随机化版本
* @param args
*/
public static void main(String[] args) {
int[] arr = {10, 9, 7, 8, 6, 4, 5, 2, 3, 1, 45, 12, 13};
randomized_quickSost(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++){
System.out.print(arr[i]);
if (i != arr.length - 1){
System.out.print(", ");
}
}
}

public static void randomized_quickSost(int[] arr, int p, int r){
if(p < r){
int q = randomized_partition(arr, p, r);
randomized_quickSost(arr, p , q-1);
randomized_quickSost(arr, q+1, r);
}
}

public static int randomized_partition(int[] arr, int p, int r) {
int i = (int)(Math.random()*(r - p + 1)) + p;
quickSort.exchange(arr, r, i);
return quickSort.partition(arr, p, r);
}
}

相关主题
相关文档
最新文档