Realization of several algorithms in java

xiaoxiao2021-03-31  241

Insert sort:

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]

Bubble Sort:

Package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author Treeroot * @since 2006-2-2 * @version 1.0 * / public class bubblesort implements sortutil.sort {/ * (Non-javadoc) * @see org.rut.util.algorithm.Sortutil.Sort # sort (int []) * / public void sort (int [] data) {int Temp; for (int i = 0 i i; j -) {IF (data [j] "[j-1]) {sortutil.swap (data , J, J-1);}}}}}

Select Sort:

Package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author Treeroot * @since 2006-2-2 * @version 1.0 * / public class SelectionSort Implements Sortutil.Sort {/ * * (Non-javadoc) * * @see org.rut.util.algorithm.sortutil.Sort # sort (int []) * / public void sort (int [] data) {int Temp; for (int i) = 0; i i; j -) {IF (Data [J]

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]

Quick sort:

Package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author treeroot * @since 2006-2-2 * @version 1.0 * / public class quicksort implements Sortutil.Sort {/ * (Non-javadoc) * @see org.rut.util.algorithm.sortutil.Sort # sort (int []) * / public void sort (int [] data) {Quicksort (data, 0, data.length -1);} private void Quicksort (int [] data, int i, int j) {int spreadindex = (i j) / 2; // swap sortutil.swap (data, pivotindex, j); int k = partition (DATA, I-1, J, Data [J]); Sortutil.Swap (Data, K, J); IF ((ki)> 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 pivot) {DO {while (Data [ L] pivot); Sortutil.Swap (Data, L, R);} While L

package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; / ** * @author treeroot * @since 2006-2-2 * @version 1.0 * / public class ImprovedQuickSort implements SortUtil.Sort {Private static int max_stack_size = 4096; private static int threshold = 10; / * (non-javadoc) * @see org.rut.util.algorithm.Sortutil.Sort # sort (int []) * / public void sort [] data) {int [] stack = new int [MAX_STACK_SIZE]; int Top = -1; int Pivot; Int PivotIndex, L, R; Stack [ TOP] = 0; Stack [ Top] = DATA. Length-1; while (TOP> 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.sw AP (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

i ) {for (int J = i; (j> 0) && (data [j]

Package org.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.Algorithm.Sortutil.Sort # sort (int []) * / public void sort (int [] data) {int [] temp = new int = .length]; MergeSort (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) Return; 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]

Improved merger 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 implements SortUtil.Sort {Private Static Final Int threshold = 10; / * * (Non-javadoc) * * @see org.rut.util.algorithm.Sortutil.Sort # sort (int []) * / public void sort (int [] data) {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) 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 [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 <= r; k ) {IF (a

) {For (int J = i; (j> start) && data [j]

Pile Sort:

Package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sortutil; / ** * @Author treeroot * @since 2006-2-2 * @version 1.0 * / public class Heapsort Implements Sortutil.Sort {/ * (Non-javadoc) * @see org.rut.util.algorithm.Sortutil.Sort # sort (int []) * / public void sort (int [] data) {maxheap h = new maxheap (); h .init (data); for (int i = 0; i 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.selectionsSort; 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 mege = 7; public final static int improved_merge = 8; public final static int Heap = 9; public static void sort (int [] data) {sort (data, improwd_q Uick); Static sort [] ompl = new sort [] {new INSERTSORT (), New SeleSort (), New Shellsort (), New Shellsort (), New ImprovedQuicksort (), New iMGESort (), new iMGESort () New Heapsort ()}; public static string toString (int algorithm) {return name [algorithm-1];

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

New Post(0)