Five common sorting methods in java and java

Source: Internet
Author: User

Five common sorting methods in java and java

Package com. chenyang. www. demo;

/**
* Created by red devils on.
*/
Public class Scortdemo {
/**
* Bubble sort <br/>
* <Li> compares adjacent elements. If the first is bigger than the second, exchange the two of them. </Li>
* <Li> perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number. </Li>
* <Li> repeat the preceding steps for all elements except the last one. </Li>
* <Li> continue to repeat the above steps for fewer and fewer elements each time until there is no number to be compared. </Li>
*
* @ Param numbers
* Integer array to be sorted
*/
Public static void bubbleSort (int [] numbers ){
Int temp; // records the temporary median value.
Int size = numbers. length; // array size
For (int I = 0; I <size-1; I ++ ){
For (int j = I + 1; j <size; j ++ ){
If (numbers [I] <numbers [j]) {// exchanges the positions of two numbers
;
;
;
// Fast sorting uses the divide and conquer method to divide a sequence into two subsequences.
/**
* Fast sorting <br/>
* <Ul>
* <Li> extract an element from the sequence, which is called a "benchmark" </li>
* <Li> re-sort the series. All elements are placed before the benchmark values smaller than the benchmark values, and all elements are placed behind the benchmark values larger than the benchmark values (the same number can reach either side ). After this split,
* This benchmark is its last position. This is called a partition operation. </Li>
* <Li> recursively sorts subsequences smaller than the reference value element and subsequences greater than the reference value element. </Li>
* </Ul>
*
* @ Param numbers
* @ Param start
* @ Param end
*/
Public static void quickSort (int [] numbers, int start, int end ){
If (start <end ){
Int base = numbers [start]; // selected benchmark value (the first value serves as the benchmark value)
Int temp; // records the temporary median value.
Int I = start, j = end;
Do {
While (numbers [I] <base) & (I <end ))
;
While (numbers [j]> base) & (j> start ))
;
If (I <= j ){
;
;
;
;
;
While (I <= j );
If (start <j)
QuickSort (numbers, start, j );
If (end> I)
QuickSort (numbers, I, end );
// Select sorting is a simple and intuitive sorting method. Each time you look for the minimum value in the sequence, you can put it at the end.
/**
* Select sorting <br/>
* <Li> Find the minimum element in the unordered sequence and store it to the starting position of the sorting sequence. </li>
* <Li> Search for the smallest element from the remaining unordered elements and put it at the end of the sorting sequence. </Li>
* <Li> Similarly, until all elements are sorted. </Li>
*
* @ Param numbers
*/
Public static void selectSort (int [] numbers ){
Int size = numbers. length, temp;
For (int I = 0; I <size; I ++ ){
Int k = I;
For (int j = size-1; j> I; j --){
If (numbers [j] <numbers [k]) k = j;
;
;
;
// The principle of insertion sorting is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. For detailed steps, see the code and comments.
/**
* Insert sorting <br/>
* <Ul>
* <Li> starting from the first element, this element can be considered to have been sorted </li>
* <Li> extracts the next element and scans the sorted element sequence from the back to the front </li>
* <Li> If the element (sorted) is greater than the new element, move the element to the next position. </li>
* <Li> repeat Step 3 until the position of the sorted element is smaller than or equal to that of the new element is found. </li>
* <Li> Insert new elements to this position </li>
* <Li> repeat Step 2 </li>
* </Ul>
*
* @ Param numbers
*/
Public static void insertSort (int [] numbers ){
Int size = numbers. length, temp, j;
For (int I = 1; I <size; I ++ ){
;
For (j = I; j> 0 & temp <numbers [J-1]; j --)
1];
;
// Merge Sorting is an effective sort algorithm based on the merge operation. Merge refers to the operation of merging two sorted sequences into one. The reference code is as follows:
/**
* Merge and sort <br/>
* <Ul>
* <Li> applied space, which is the sum of two sorted sequences. The space is used to store the merged sequence. </li>
* <Li> set two pointers. The initial positions are the starting positions of the two sorted sequences respectively. </li>
* <Li> compare the elements pointed to by the two pointers, select a relatively small element, and move the pointer to the next position. </li>
* <Li> repeat Step 3 until a pointer reaches the end of the sequence </li>
* <Li> copy all the remaining elements of the other sequence to the end of the merging sequence. </li>
* </Ul>
*
* @ Param numbers
*/
Public static void mergeSort (int [] numbers, int left, int right ){
Int t = 1; // number of elements in each group
Int size = right-left + 1;
While (t <size ){
Int s = t; // number of elements in each group in this cycle
2 * s;
Int I = left;
While (I + (t-1) <size ){
Merge (numbers, I, I + (s-1), I + (t-1 ));
;
If (I + (s-1) <right)
Merge (numbers, I, I + (s-1), right );
/**
* Merge algorithm implementation
*
* @ Param data
* @ Param p
* @ Param q
* @ Param r
*/
Private static void merge (int [] data, int p, int q, int r ){
Int [] B = new int [data. length];
Int s = p;
Int t = q + 1;
Int k = p;
While (s <= q & t <= r ){
If (data [s] <= data [t]) {
;
;
Else {
;
;
;
If (s = q + 1)
;
Else
;
For (int I = p; I <= r; I ++)
;
// Sort all the preceding sorting algorithms into the NumberSort class and code
// Sorting class implemented by Java
Public class NumberSort {
// Private constructor to disable instantiation
Private NumberSort (){
Super ();
// Sort by bubble
Public static void bubbleSort (int [] numbers ){
Int temp; // records the temporary median value.
Int size = numbers. length; // array size
For (int I = 0; I <size-1; I ++ ){
For (int j = I + 1; j <size; j ++ ){
If (numbers [I] <numbers [j]) {// exchanges the positions of two numbers
;
;
;
// Quick sorting
Public static void quickSort (int [] numbers, int start, int end ){
If (start <end ){
Int base = numbers [start]; // selected benchmark value (the first value serves as the benchmark value)
Int temp; // records the temporary median value.
Int I = start, j = end;
Do {
While (numbers [I] <base) & (I <end ))
;
While (numbers [j]> base) & (j> start ))
;
If (I <= j ){
;
;
;
;
;
While (I <= j );
If (start <j)
QuickSort (numbers, start, j );
If (end> I)
QuickSort (numbers, I, end );
// Select sorting
Public static void selectSort (int [] numbers ){
Int size = numbers. length, temp;
For (int I = 0; I <size; I ++ ){
Int k = I;
For (int j = size-1; j> I; j --){
If (numbers [j] <numbers [k])
;
;
;
;
// Insert sorting
// @ Param numbers
Public static void insertSort (int [] numbers ){
Int size = numbers. length, temp, j;
For (int I = 1; I <size; I ++ ){
;
For (j = I; j> 0 & temp <numbers [j-1]; j --)
1];
;
// Merge and sort
Public static void mergeSort (int [] numbers, int left, int right ){
Int t = 1; // number of elements in each group
Int size = right-left + 1;
While (t <size ){
Int s = t; // number of elements in each group in this cycle
2 * s;
Int I = left;
While (I + (t-1) <size ){
Merge (numbers, I, I + (s-1), I + (t-1 ));
;
If (I + (s-1) <right)
Merge (numbers, I, I + (s-1), right );
// Merge algorithm implementation
Private static void merge (int [] data, int p, int q, int r ){
Int [] B = new int [data. length];
Int s = p;
Int t = q + 1;
Int k = p;
While (s <= q & t <= r ){
If (data [s] <= data [t]) {
;
;
Else {
;
;
;
If (s = q + 1)
;
Else
;
For (int I = p; I <= r; 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.