各类排序算法java的实现

各类排序算法java的实现

插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class InsertSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { int temp; for(int i=1;i 0)&&(data[j] i;j--){ if(data[j] i; j--) { if (data[j] < data[lowIndex]) { lowIndex = j; } } SortUtil.swap(data,i,lowIndex); } } } Shell排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class ShellSort implements SortUtil.Sort{ /* (non-Javadoc) * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[]) */ public void sort(int[] data) { for(int i=data.length/2;i>2;i/=2){ for(int j=0;j =inc)&&(data[j] 1) quickSort(data,i,k-1); if((j-k)>1) quickSort(data,k+1,j); } /** * @param data * @param i * @param j * @return */ private int partition(int[] data, int l, int r,int pivot) { do{ while(data[++l] pivot); SortUtil.swap(data,l,r); } while(l 0){ int j=stack[top--]; int i=stack[top--]; pivotIndex=(i+j)/2; pivot=data[pivotIndex]; SortUtil.swap(data,pivotIndex,j); //partition l=i-1; r=j; do{ while(data[++l] pivot)); SortUtil.swap(data,l,r); } while(l THRESHOLD){ stack[++top]=i; stack[++top]=l-1; } if((j-l)>THRESHOLD){ stack[++top]=l+1; stack[++top]=j; } } //new InsertSort().sort(data); insertSort(data); } /** * @param data */ private void insertSort(int[] data) { int temp; for(int i=1;i 0)&&(data[j] r) data[cur]=temp[i1++]; else if(temp[i1] = THRESHOLD) mergeSort(data, temp, l, mid); else insertSort(data, l, mid - l + 1); if ((r - mid) > THRESHOLD) mergeSort(data, temp, mid + 1, r); else insertSort(data, mid + 1, r - mid); for (i = l; i <= mid; i++) { temp = data; } for (j = 1; j <= r - mid; j++) { temp[r - j + 1] = data[j + mid]; } int a = temp[l]; int b = temp[r]; for (i = l, j = r, k = l; k <= r; k++) { if (a < b) { data[k] = temp[i++]; a = temp; } else { data[k] = temp[j--]; b = temp[j]; } } } /** * @param data * @param l * @param i */ private void insertSort(int[] data, int start, int len) { for(int i=start+1;i start) && data[j] <="size)" private return static int[] if 堆排序: HeapSort MaxHeap h="new" MaxHeap(); h.init(data); h.remove(); System.arraycopy(h.queue,1,data,0,data.length); MaxHeap{ init(int[] data){ this.queue="new" int[data.length+1]; queue[++size]="data;" fixUp(size); queue; get() queue[1]; remove() SortUtil.swap(queue,1,size--); fixDown(1); fixdown fixDown(int k) j; while ((j="k" << 1) (j && queue[j] queue[j]) //不用交换 break; SortUtil.swap(queue,j,k); k = j; } } private void fixUp(int k) { while (k > 1) { int j = k >> 1; if (queue[j]>queue[k]) break; SortUtil.swap(queue,j,k); k = j; } } } } SortUtil: package org.rut.util.algorithm; import org.rut.util.algorithm.support.BubbleSort; import org.rut.util.algorithm.support.HeapSort; import org.rut.util.algorithm.support.ImprovedMergeSort; import org.rut.util.algorithm.support.ImprovedQuickSort; import org.rut.util.algorithm.support.InsertSort; import org.rut.util.algorithm.support.MergeSort; import org.rut.util.algorithm.support.QuickSort; import org.rut.util.algorithm.support.SelectionSort; import org.rut.util.algorithm.support.ShellSort; /** * @author treeroot * @since 2006-2-2 * @version 1.0 */ public class SortUtil { public final static int INSERT = 1; public final static int BUBBLE = 2; public final static int SELECTION = 3; public final static int SHELL = 4; public final static int QUICK = 5; public final static int IMPROVED_QUICK = 6; public final static int MERGE = 7; public final static int IMPROVED_MERGE = 8; public final static int HEAP = 9; public static void sort(int[] data) { sort(data, IMPROVED_QUICK); } private static String[] name={ "insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap" }; private static Sort[] impl=new Sort[]{ new InsertSort(), new BubbleSort(), new SelectionSort(), new ShellSort(), new QuickSort(), new ImprovedQuickSort(), new MergeSort(), new ImprovedMergeSort(), new HeapSort() }; public static String toString(int algorithm){ return name[algorithm-1]; } public static void sort(int[] data, int algorithm) { impl[algorithm-1].sort(data); } public static interface Sort { public void sort(int[] data); } public static void swap(int[] data, int i, int j) { int temp = data; data = data[j]; data[j] = temp; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值