Array sorting summary in JAVA

Source: Internet
Author: User

Public class SortAll {

/**
* Java Implementation of Bubble sorting, selection sorting, insertion sorting, and Shell sorting
*/
Public static void main (String [] args ){
Int [] I = {1, 5, 6, 12, 4, 9, 3, 23, 39,403,596, 87 };
System. out. println ("---- result of Bubble Sorting :");
MaoPao (I );
System. out. println ();
System. out. println ("---- select the sorting result :");
XuanZe (I );
System. out. println ();
System. out. println ("---- Insert the sorting result :");
ChaRu (I );
System. out. println ();
System. out. println ("---- result of Shell sorting :");
Shell (I );
}

// Bubble sort
Public static void maoPao (int [] x ){
For (int I = 0; I <x. length; I ++ ){
For (int j = I + 1; j <x. length; j ++ ){
If (x [I]> x [j]) {
Int temp = x [I];
X [I] = x [j];
X [j] = temp;
}
}
}
For (int I: x ){
System. out. print (I + "");
}
}

// Select sorting
Public static void xuanZe (int [] x ){
For (int I = 0; I <x. length; I ++ ){
Int lowerIndex = I;
// Find the smallest Index
For (int j = I + 1; j <x. length; j ++ ){
If (x [j] <x [lowerIndex]) {
LowerIndex = j;
}
}
// Exchange
Int temp = x [I];
X [I] = x [lowerIndex];
X [lowerIndex] = temp;
}
For (int I: x ){
System. out. print (I + "");
}
}

// Insert sorting
Public static void chaRu (int [] x ){
For (int I = 1; I <x. length; I ++) {// I from the beginning, because the first number is already sorted
For (int j = I; j> 0; j --){
If (x [j] <x [j-1]) {
Int temp = x [j];
X [j] = x [j-1];
X [j-1] = temp;
}
}
}
For (int I: x ){
System. out. print (I + "");
}
}

// Sort by hill
Public static void shell (int [] x ){
// Group
For (int increment = x. length/2; increment> 0; increment/= 2 ){
// Sort each group
For (int I = increment; I <x. length; I ++ ){
Int temp = x [I];
Int j = 0;
For (j = I; j> = increment; j-= increment ){
If (temp <x [j-increment]) {
X [j] = x [j-increment];
} Else {
Break;
}
}
X [j] = temp;
}
}

For (int I: x ){
System. out. print (I + "");
}
}
}

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.