快速排序Java源码(递归和非递归)

http://blog.csdn.net/bnuside/article/details/6906688


  1. ackage com.side.quicksort;  
  2.   
  3. import com.side.tests.Stack;//作者自己定义的栈类  
  4.   
  5. public class QuickSort {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         QuickSort t=new QuickSort();  
  13.         t.test();  
  14.     }  
  15.     public void test(){  
  16.         //int a[]={10,1,4,7,8,6,3,4,4,4,4,4,2,5,9,4,2};  
  17.         int a[]={};  
  18.         printArray(a);  
  19.         //quickSort(a,0,a.length-1);  
  20.         //nonRecrutSort(a);  
  21.         nonRecrutQuickSort(a);  
  22.         printArray(a);  
  23.         //partition(a, 0, 5);  
  24.     }  
  25.     public void quickSort(int[] a,int start,int end){//递归快排  
  26.         if(start<end){  
  27.             quickSort(a,start,partition(a,start,end)-1);  
  28.             quickSort(a,partition(a,start,end)+1,end);  
  29.         }  
  30.     }  
  31.     public void nonRecrutSort(int[] a){//非递归快排,两个栈  
  32.         //设置两个栈,一个用于保存  
  33.         if(a==null||a.length<0return;  
  34.         Stack<Integer> startStack=new Stack<Integer>();//保存当前划分的最高位  
  35.         Stack<Integer> endStack=new Stack<Integer>();//保存当前划分的最低位  
  36.         int start=0;  
  37.         int end=a.length-1;  
  38.               
  39.         int pivotPos;  
  40.           
  41.         startStack.push(start);  
  42.         endStack.push(end);  
  43.           
  44.         while(!startStack.isEmpty()){  
  45.             start=startStack.pop();  
  46.             end=endStack.pop();  
  47.             pivotPos=partition(a, start, end);  
  48.             if(start<pivotPos-1){  
  49.                 startStack.push(start);  
  50.                 endStack.push(pivotPos-1);  
  51.             }  
  52.             if(end>pivotPos+1){  
  53.                 startStack.push(pivotPos+1);  
  54.                 endStack.push(end);  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     public void nonRecrutQuickSort(int a[]){  
  60.         if(a==null||a.length<=0)return;  
  61.         Stack<Integer> index=new Stack<Integer>();  
  62.         int start=0;  
  63.         int end=a.length-1;  
  64.           
  65.         int pivotPos;  
  66.               
  67.         index.push(start);  
  68.         index.push(end);  
  69.               
  70.         while(!index.isEmpty()){  
  71.             end=index.pop();  
  72.             start=index.pop();  
  73.               
  74.             pivotPos=partition(a,start,end);  
  75.             if(start<pivotPos-1){  
  76.                 index.push(start);  
  77.                 index.push(pivotPos-1);  
  78.             }  
  79.             if(end>pivotPos+1){  
  80.                 index.push(pivotPos+1);  
  81.                 index.push(end);  
  82.             }  
  83.         }     
  84.     }  
  85.       
  86.     public int partition(int[] a,int start,int end){//分块方法,在数组a中,对下标从start到end的数列进行划分  
  87.         int pivot=a[start];                     //把比pivot(初始的pivot=a[start]小的数移动到pivot的左边  
  88.         while(start<end){                       //把比pivot大的数移动到pivot的右边  
  89.             while(start<end&&a[end]>=pivot) end--;  
  90.             a[start]=a[end];  
  91.             while(start<end&&a[start]<=pivot) start++;  
  92.             a[end]=a[start];              
  93.         }  
  94.         a[start]=pivot;  
  95.         return start;//返回划分后的pivot的位置  
  96.         //printArray(a);  
  97.     }  
  98.       
  99.     public void printArray(int a[]){//打印数组内容的方法,用于测试  
  100.         for(int i=0;i<a.length;i++){  
  101.             System.out.print(a[i]+" ");  
  102.         }  
  103.         System.out.println();  
  104.     }  
  105.   


快速排序是一种高效的排序算法,它基于分治策略,通过一趟排序将待排记录分隔成独立的两部分,其中一部分的所有数据都比另一部分小,然后分别对这两部分继续进行排序,直到整个序列有序。在Java中,快速排序既可以用递归实现,也可以用循环遍历实现。 **递归实现**: ```java public class QuickSortRecursion { public void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); // 对左半部分递归排序 quickSort(arr, pivotIndex + 1, high); // 对右半部分递归排序 } } private int partition(int[] arr, int low, int high) { int pivot = arr[high]; // 选择最后一个元素作为基准 int i = (low - 1); // 指针,初始指向左侧 for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; // 如果当前元素小于基准,向右移动指针 swap(arr, i, j); } } swap(arr, i + 1, high); // 将基准放到正确的位置 return i + 1; } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } ``` **遍历实现**: ```java public class QuickSortIteration { public void quickSort(int[] arr) { int len = arr.length; if (len <= 1) return; // 基线条件:长度为1或0的数组已经排序 stackify(arr, 0, len - 1); // 使用栈模拟递归过程 while (!stack.isEmpty()) { pivotSort(arr, stack.pop(), stack.pop()); } } private void stackify(int[] arr, int low, int high) { while (low < high) { push(stack, low, high); while (!stack.isEmpty() && lessThan(arr, stack.peek(0), stack.peek(1))) { pivotSort(arr, stack.pop(), high); high--; } while (!stack.isEmpty() && lessThan(arr, low, stack.peek(0))) { pivotSort(arr, low, stack.pop()); low++; } } } private void pivotSort(int[] arr, int pivotStart, int pivotEnd) { int pivot = arr[pivotEnd]; int i = pivotStart - 1; for (int j = pivotStart; j < pivotEnd; j++) { if (arr[j] <= pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, pivotEnd); } // 其他辅助方法... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值