Java sorting methods ..

Source: Internet
Author: User
There are too many questions in the forum. So I will send them up. One month later, I hope they will be able to reach the top for you to see. Please add the incomplete questions.

This is a note prepared by our students. After he allows the documents to be sent to the Internet, He excerpted the documents from books or the Internet and hoped to help everyone.

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 );
}
}
}
}

}

 
 
 
 
?
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 );
}
}
}

}

Top
 
 
 
 
?
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 );
}
}
}

Top
 
 
 
 
?
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;
}
}

}

 

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.