Java basic sorting and java sorting

Source: Internet
Author: User

Java basic sorting and java sorting

Record several sort java Codes

Public class Index {

Public static void main (String [] args ){

Int [] a = {1, 4, 5, 6, 8, 2, 3, 9, 6 ,};
// SelectSort ();
// BubleSort ();
// InsertSort ();
// QuickSort (a, 0, a. length-1 );
// QuickSort (a, 0, a. length-1 );
ShellSort ();
For (int B: ){
System. out. println (B );
}

}

// Select sort, and select the largest one from the list to the front.
Public static void selectSort (int [] arr ){

For (int I = 0; I <arr. length-1; I ++ ){

For (int j = I + 1; j <arr. length; j ++ ){

If (arr [I] <arr [j]) {
Int tem = arr [j];
Arr [j] = arr [I];
Arr [I] = tem;
}

}
}

}

// Buble sort, which is in the sub-cycle. The values are compared and exchanged in the form of bubble, so as to put a large number behind it.
Public static void bubleSort (int [] arr ){

For (int I = 0; I <arr. length-1; I ++ ){

For (int j = 0; j <arr. length-1-I; j ++ ){

If (arr [j]> arr [j + 1]) {
Int tem = arr [j + 1];
Arr [j + 1] = arr [j];
Arr [j] = tem;
}
}

}

}

// Insert sort. Assume that the previous part is ordered. In the subcycle, use the target element to compare it with the previous part. If it is large, move forward. If it is small, insert the position.
Public static void insertSort (int [] arr ){

For (int I = 1; I <arr. length; I ++ ){

Int j = I;
Int temp = arr [I];
While (j> 0 ){
If (temp <arr [j-1]) {

Arr [j] = arr [j-1];

Arr [j-1] = temp;
}
J --;

}

}

}

// Quick sort, similar to the half-fold idea, so that the left side of a value is smaller than it, the right side is greater than it, And then recursion on both sides
Public static void quickSort (int [] arr, int low, int high ){
Int start = low;
Int end = high;
Int key = arr [start];

While (end> start ){

While (end> start & arr [end]> = key ){
End --;
}
If (arr [end] <= key ){
Int tem = arr [end];
Arr [end] = arr [start];
Arr [start] = tem;
}
While (start <end & arr [start] <= key ){
Start ++;
}
If (arr [start]> = key ){
Int tem = arr [start];
Arr [start] = arr [end];
Arr [end] = tem;
}

}

If (start> low ){
QuickSort (arr, low, start-1 );

}
If (end QuickSort (arr, end + 1, high );
}

}

// Shell sort, similar to sorting, but with incremental, the idea of first rough and then fine (first rough sorting, then careful sorting)
Public static void shellSort (int [] arr ){
Int d = arr. length/2;

While (d> = 1 ){
For (int I = 0; I <arr. length; I ++ ){
For (int j = I; j <arr. length-d; j = j + d ){
If (arr [j]> arr [j + d]) {
Int tem = arr [j];
Arr [j] = arr [j + d];
Arr [j + d] = tem;
}
}
}
D = d/2;

}
}

}

Related Article

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.