static public void main(String[] args) { int array[]={3,0,12,8,6,1,7,9,2,4}; quickSort(array,0,array.length-1); //nonRec_quickSort(array,0,array.length-1); printArray(array); } static private void quickSort(int array[],int start,int end){ Stack<Integer> stack=new Stack<>(); stack.push(start); stack.push(end); int key; while(!stack.isEmpty()){ int _end=stack.pop(); int _start=stack.pop(); key=array[_start]; int l=_start; int h=_end; while(l<h){ while(array[h]>=key&&l<h) h--; while(array[l]<=key&&l<h) l++; if(l<h) { int temp1 = array[h]; array[h] = array[l]; array[l] = temp1; } } if(key>array[l]) { int temp=array[_start]; array[_start]=array[l]; array[l]=temp; } if(_start<l-1) { stack.push(_start); stack.push(l-1); } if(_end>l+1) { stack.push(l+1); stack.push(_end); } } }
java 快速排序非递归正确版
最新推荐文章于 2024-04-15 08:19:32 发布