JAVA Implementation of various sorting algorithms

Source: Internet
Author: User

JAVA Implementation of various sorting algorithms

Insert sorting: 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 sorting: 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 partition) {do {While (data [++ L] Bytes); sortutil. Swap (data, L, R);} while (L 0) {Int J = stack [top --]; int I = stack [top --]; small tindex = (I + J)/2; small = data [small tindex]; sortutil. swap (data, small tindex, J); // partition L = I-1; r = J; do {While (data [++ L] ); 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] Queue [J]) // you do not need to 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 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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.