这段程序是我看过别的代码后自己想着写的,可是老出错,小弟水平浅看不出来,请各位帮忙看看,顺便指点一下小弟的排序算法
多谢了!
public class TestSort {
public static void main(String[] args) {
int[] i_arr = new int[]{ 12, 4, 8, 55, 6, 78, 43, 45, 6, 8 };
//bubbleSort(i_arr);
//selectionSort(i_arr);
//insertSort(i_arr);
quickSort(i_arr, 0, i_arr.length - 1);
for(int i=0; i<i_arr.length; i++) {
System.out.print(i_arr[i] + " ");
}
}
public static void quickSort(int[] a, int low, int high) {
if(low >= high)
return;
int start = low;
int end = high;
int pivot = a[low];
while(true) {
while(a[end] >= pivot)
end --;
while(a[start] <= pivot)
start ++;
if(start >= end)
break;
swap(a, start, end);
}
quickSort(a, low, start - 1);
quickSort(a, end + 1, high);
}
public static void swap(int[] a, int b, int c) {
int temp = a[b];
a[b] = a[c];
a[c] = temp;
}
}
[code="java"]
static void BubbleSort(int a []){
int temp=0;
for (int i = 0; i < a.length ; i++) {
for (int j = 0; j < a.length - i - 1; j++){
if (a[j]>a[j + 1]){ //把这里改成大于,就是升序了
temp=a[j];
a[j]=a[j + 1];
a[j + 1]=temp;
}
}
}
}[/code]
这是比较经典的排序算法,你写的那个我也没怎么看懂,不过看起来有点麻烦。
不过思路很好。