Various ordering algorithms Java implementation

xiaoxiao2021-04-04  303

}}} 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] pivot; sortutil.swap (data, l, r);} while (l 0) {INT J = stack [top--]; in Ti = 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 ((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 0) && (data [j]); j -) {sortutil.swap (DATA, J, J-1);}}}}}. Rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author Treeroot * @since 2006-2-2 * @version 1.0 * / public class mergeSort Implements Sortutil.Sort {/ * (non-javadoc) * @see org.rut.util.algorithm.sortutil.Sort # sort (int []) * / public void sort (int [] data) {int [] temp = new int [data.length] Mrgesort (Data, Temp, 0, Data.Length-1);} private void mergesort (int [] data, int [] temp, int L, int R) {INT MID = (L R) / 2; IF (l == r) RETUR N; MrgeSort (DATA, TEMP, L, MID); MERGESORT (Data, Temp, MID 1, R); For (int i = L; i <= r; i ) {TEMP [i] = data [i] } INT I1 = L; INT I2 = MID 1; for (int Cur = L; Cur <= R; CUR ) {if (i1 == mid 1) DATA [CUR] = Temp [i2 ]; Else IF (i2> r) Data [Cur] = Temp [I1 ]; ELSE IF (Temp [I1]

}}} 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/

转载请注明原文地址:https://www.9cbs.com/read-131898.html

New Post(0)