数组排序(冒泡和快速)

 public static void sort(int[]  values){


int temp;

for(int i=0 ; i < values.length ; ++i){

for(int j=0; j <values.length - i - 1; ++j){

if(values[j] > values[j + 1]){
temp = values[j];
values[j] = values[j + 1];
values[j + 1] = temp;


}
}
}
}

 

public static void main(String[] args) {
int[] arr = {1,4,7,2,5,8,3,6,9};
quickSort(arr);
}

public static void quickSort(int[] a) {
quickSort(a, 0, a.length - 1);
}

private static void quickSort(int[] a, int start, int end) {
int left = start;
int right = end - 1;
int pivot = a[end];

while (left < right) {
if (a[left] <= pivot) {
left++;
continue;
}
if (a[right] > pivot) {
right--;
continue;
}
swap(a, left++, right);
}

if (a[left] < pivot) {
left++;
}
swap(a, left, end);

if(left - 1 > start) {
quickSort(a, start, left - 1);
}
if(left + 1 < end) {
quickSort(a, left + 1, end);
}
}



posted on 2012-02-13 21:17  每当变幻时  阅读(327)  评论(0编辑  收藏  举报

导航