Various sorting methods implemented in Java, including insertion sorting, Bubble sorting, selection sorting, shell sorting, quick sorting, Merge Sorting, heap sorting, and sortutil.
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 <data. length; I ++ ){
For (Int J = I; (j> 0) & (data [J] <data [J-1]); j --){
Sortutil. Swap (data, J, J-1 );
}
}
}
}
Bubble Sorting:
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 <data. length; I ++ ){
For (Int J = data. Length-1; j> I; j --){
If (data [J] <data [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 <data. length; I ++ ){
Int lowindex = I;
For (Int J = data. Length-1; 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 <I; j ++ ){
Insertsort (data, J, I );
}
}
Insertsort (data, 0, 1 );
}
/**
* @ Param data
* @ Param J
* @ Param I
*/
Private void insertsort (INT [] data, int start, int Inc ){
Int temp;
For (INT I = start + Inc; I <data. length; I + = Inc ){
For (Int J = I; (j> = Inc) & (data [J] <data [J-INC]); j-= Inc ){
Sortutil. Swap (data, J, J-Inc );
}
}
}
}
Quick sorting:
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 struct tindex = (I + J)/2;
// Swap
Sortutil. Swap (data, structured tindex, J );
Int K = partition (data, I-1, J, data [J]);
Sortutil. Swap (data, K, J );
If (k-I)> 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] <strong );
While (R! = 0) & Data [-- R]> tables );
Sortutil. Swap (data, L, R );
}
While (L <R );
Sortutil. Swap (data, L, R );
Return L;
}
}
Improved quick sorting:
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 (INT [] data ){
Int [] stack = new int [max_stack_size];
Int Top =-1;
Int timeout;
Int struct tindex, L, R;
Stack [++ top] = 0;
Stack [++ top] = data. Length-1;
While (top> 0 ){
Int J = stack [top --];
Int I = stack [top --];
Required tindex = (I + J)/2;
Dependencies = data [inittindex];
Sortutil. Swap (data, structured tindex, J );
// Partition
L = I-1;
R = J;
Do {
While (data [++ L] <strong );
While (R! = 0) & (data [-- R]> tables ));
Sortutil. Swap (data, L, R );
}
While (L <R );
Sortutil. Swap (data, L, R );
Sortutil. Swap (data, L, J );
If (l-I)> 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 <data. length; I ++ ){
For (Int J = I; (j> 0) & (data [J] <data [J-1]); j --){
Sortutil. Swap (data, J, J-1 );
}
}
}
}
Merge Sorting:
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. 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 mid = (L + r)/2;
If (L = r) return;
Mergesort (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] <temp [I2])
Data [cur] = temp [I1 ++];
Else
Data [cur] = temp [I2 ++];
}
}
}
Improved Merge Sorting:
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 <B ){
Data [k] = temp [I ++];
A = temp [I];
} 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 + Len; I ++ ){
For (Int J = I; (j> Start) & Data [J] <data [J-1]; j --){
Sortutil. Swap (data, J, J-1 );
}
}
}
}
Heap sorting:
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 <data. length; I ++)
H. Remove ();
System. arraycopy (H. queue, 1, data, 0, Data. Length );
}
Private Static class maxheap {
Void Init (INT [] data ){
This. Queue = new int [data. Length + 1];
For (INT I = 0; I <data. length; I ++ ){
Queue [++ size] = data [I];
Fixup (size );
}
}
Private int size = 0;
Private int [] queue;
Public int get (){
Return queue [1];
}
Public void remove (){
Sortutil. Swap (queue, 1, size --);
Fixdown (1 );
}
// Fixdown
Private void fixdown (int K ){
Int J;
While (j = k <1) <= size ){
If (j <size & queue [J] <queue [J + 1])
J ++;
If (queue [k]> queue [J]) // No 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 [I];
Data [I] = data [J];
Data [J] = temp;
}
}