}}} Sortutil.swap (Data, I, Lowindex);}}}} shell sort: 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.length / 2; i> 2; I / = 2) {for (int J = 0; j = inc) && (Data [J] 1) Quicksort (DATA, I, K-1); IF ((JK)> 1) Quicksort (Data, K 1, J);} / ** * @
PARAM DATA * @Param i * @Param J * @return * / private INT Partition (int [] data, int L, int r, int spread) {do {while (Data [ L]
TOP] = L-1;} IF ((jl)> 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 }}} After the improvement, sort: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author treeroot * @since 2006-2-2 * @version 1.0 * / public class improvedmergesort {private static final int threshold = 10; / * * (non-javadoc) * * @see org.rut.util.algorithm.sortutil.Sort # sort (int []) * / Public void sort (int [] temp = new int [data.length]; mergesort (data, temp, 0, data.length-1);} private void mergesort (int [] data, int [ ] TEMP, INT L, INT R {INT I, J, K; INT MID = (L R) / 2; IF (L == R) Return; IF ((MID - L)> = Threshold) Merges DATA, TEMP, L, MID); Else Insertsort (DATA, L, MID - L 1); IF ((R - MID)> Threshold MergeSort (Data, Temp, MID 1, R); Else Insertsort , MID 1, R - MID); for (i = L; i <= mid; i ) {TEMP [i] = data [i];} 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
START LEN; I ) {for (int J = i; (J> Start) && Data [J] Queue [J]) // Don't exchange 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 static int insert = 1; public final static int bubble = PUBLIC FINAL INT SHELL = 3; Public Final Static Int Shell = 4; Public Final Static Int Quick = 5; Public Final Static Int Improved_quick = 6; Public Final Static Int ME RGE = 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", "Quick", "Improved_quick", "Merge", "Improved_Merge", "HEAP"}; private static sort [] IMPL = New Sort [] {New Insert 4, New SeleSort (), New Shellsort (), New Shellsort (), New Improvedquicksort (), New MergeSort (), New Mergesort (), New Mergesort () 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 [i]; data [i] = data [j]; data [J ] = Temp;}} Author Blog: http://blog.9cbs.net/treeroot/