快速排序一
2016-03-15 16:44
274 查看
public class ArrayIns { private long[] theArray; private int nElems; public ArrayIns(int max){ theArray=new long[max]; nElems=0; } public void insert(long value){ theArray[nElems]=value; nElems++; } public int size(){ return nElems; } public void display(){ System.out.print("A="); for(int j=0;j<nElems;j++) System.out.print(theArray[j]+" "); System.out.println(""); } public int partitionIt(int left,int right,long pivot){ int leftPtr=left-1; int rightPtr=right; while(true){ while(theArray[++leftPtr]<pivot){ } while(rightPtr>0&&theArray[--rightPtr]>pivot){ } if(leftPtr>=rightPtr){ break; }else{ swap(leftPtr,rightPtr); } } swap(leftPtr,right); return leftPtr; } public void swap(int dex1,int dex2){ long temp=theArray[dex1]; theArray[dex1]=theArray[dex2]; theArray[dex2]=temp; } public void quickSort(){ recQuickSort(0,nElems-1); } public void recQuickSort(int left,int right){ if(right-left<=0){ return; }else{ long pivot = theArray[right]; int partition = partitionIt(left,right,pivot); recQuickSort(left,partition-1); recQuickSort(partition+1,right); } } }
public class QuickSortApp { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int maxSize=16; ArrayIns arr; arr = new ArrayIns(maxSize); for(int j=0;j<maxSize;j++){ long n=(int)(Math.random()*99); arr.insert(n); } arr.display(); arr.quickSort(); arr.display(); } }
相关文章推荐
- 快速排序
- C#快速排序算法实例分析
- C++快速排序的分析与优化详解
- js实现数组冒泡排序、快速排序原理
- php简单实现快速排序的方法
- Java 快速排序(QuickSort)原理及实现代码
- 快速排序和分治排序介绍
- java 算法之快速排序实现代码
- Java实现快速排序算法(Quicktsort)
- Java中的数组排序方式(快速排序、冒泡排序、选择排序)
- 快速排序的原理及java代码实现
- C/C++实现快速排序的方法
- c语言实现冒泡排序、希尔排序等多种算法示例
- 深入单链表的快速排序详解
- C++实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- JavaScript实现快速排序的方法
- C#使用委托实现的快速排序算法实例
- C语言的冒泡排序和快速排序算法使用实例
- javascript与Python快速排序实例对比
- php实现快速排序的三种方法分享