快速排序出现的问题

昨天看了一下快速排序,《算法》这本书给出的实现感觉理解起来不是很直观,所以自己结合其他的书自己写了一个快排算法。但是运行的时候没有办法出现结果,貌似Eclipse一直在运行,必须杀进程才可以。自己刚学也不是很理解,先把代码贴出来,希望有人可以给予指点。

 1 package Java;
 2 
 3 public class QuickSort1{
 4     
 5     public static void quickSort(int[] a){
 6         quickSort(a, 0, a.length - 1);
 7     }
 8     private static void quickSort(int[] a, int first, int last){
 9         if(first < last){     
10             int pivotIndex = partition(a, first, last);
11             quickSort(a, first, pivotIndex - 1);
12             quickSort(a, pivotIndex + 1, last);
13         }
14     }
15     
16     private static int partition(int[] a, int first, int last){
17     
18         int low = first + 1;
19         int high = last;
20         int pivot = a[first];
21         
22         while(true){
23             while(a[low] <= pivot && low < last);
24                 low++;
25             while(a[high] >= pivot && high > first);
26                 high--;
27             if(low >= high)
28                 break;
29             int temp = a[low];
30             a[low] = a[high];
31             a[high] = temp;
32         }
33         a[first] = a[high];
34         a[high] = pivot;
35         return high;
36     }
37     
38     private static void show(int[] a){
39         for(int i = 0; i < a.length; i++)
40             System.out.print(a[i] + " ");
41         System.out.println();
42     }
43     
44     public static void main(String[] args){
45         int[] a ={30, 1, 23, 232, 7, 10, 90, 17, 8};
46         quickSort(a);
47         show(a);
48     }
49 }

 ╮(╯▽╰)╭,我真是蠢,语法错误了,把内嵌的while循环分号去掉就可以了。

 1 package Java;
 2 
 3 public class QuickSort1{
 4     
 5     public static void quickSort(int[] a){
 6         quickSort(a, 0, a.length - 1);
 7     }
 8     private static void quickSort(int[] a, int first, int last){
 9         if(first < last){     
10             int pivotIndex = partition(a, first, last);
11             quickSort(a, first, pivotIndex - 1);
12             quickSort(a, pivotIndex + 1, last);
13         }
14     }
15     
16     private static int partition(int[] a, int first, int last){
17     
18         int low = first + 1;
19         int high = last;
20         int pivot = a[first];
21         
22         while(true){
23             while(a[low] < pivot && low < last)
24                 low++;
25             while(a[high] > pivot && high > first)
26                 high--;
27             if(low >= high)
28                 break;
29             int temp = a[low];
30             a[low] = a[high];
31             a[high] = temp;
32         }
33         a[first] = a[high];
34         a[high] = pivot;
35         return high;
36     }
37     
38     private static void show(int[] a){
39         for(int i = 0; i < a.length; i++)
40             System.out.print(a[i] + " ");
41         System.out.println();
42     }
43     
44     public static void main(String[] args){
45         int[] a ={30, 1, 23, 232, 7, 10, 90, 17, 80};
46         quickSort(a);
47         show(a);
48     }
49 }

总结:粗心真是害死人!

posted @ 2016-03-17 10:40  熊猫卡西法  阅读(417)  评论(0编辑  收藏  举报