Java classic algorithm sorting

Source: Internet
Author: User

Java classic algorithm sorting

I haven't written several sorting methods for a long time. Today, I repeat it again. I mainly choose sorting, Bubble sorting, and insert sorting.

Select sort

Principle: a compares each element in the array with the first element
If this element is smaller than the first one
Two elements are exchanged.
B uses the rule in every round to select a minimum element.
Put it in the first position.
C is sorted by n-1 round comparison
Put simply: Put the smallest choice in each round to the front.

Instance code

Public class SelectSort {

/**
* @ Deprecated select the sort exercise
* @ Author wangliang
*
*/
Public static void main (String [] args ){
Integer [] intgArr = {5, 9, 1, 4, 2, 6, 3, 8, 0, 7 };
SelectSort insertSort = new SelectSZ success? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> encrypt + decrypt + CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoaW50b2JqJiM0Mzs = "");
}
}

/**
* @ Deprecated exchange data
* @ Author wangliang
* @ Param intgArr
* @ Param x
* @ Param y
*
**/
Public static void swap (Integer [] intgArr, int x, int y ){

Int temp;
Temp = intgArr [x];
IntgArr [x] = intgArr [y];
IntgArr [y] = temp;
}

/**
* @ Deprecated: sort the specified elements in the array.
* @ Author wangliang
* @ Param array the array to be sorted
* @ Param from
* Where to start sorting
* @ Param end
* Destination
* @ Param c
* Comparator
*/
Public void select (Integer [] array ){
Int minIndex; // minimum Index
/**
** Loop the entire array (in fact, the upper bound here is array. length-1, because when I = array. length-1
**, The last element is already the largest. If array. length is used, the inner loop does not exist .)
** The first element is the smallest element. If a smaller element than the first element can be selected from the first element, the minimum element and the first element are allowed *
* Element exchange
*/
For (int I = 0; I <array. length; I ++ ){
MinIndex = I; // assume that the first element in each round is the smallest element.
// Starts from the next element of the minimum element.
For (int j = I + 1; j <array. length; j ++ ){
// If an element is found to be smaller than the current array [smallIndex], write down the index of this element in smallIndex.
If (array [j]. compareTo (array [minIndex]) <0 ){
MinIndex = j;
}
}
// Previously, only the minimum element index is recorded. After the minimum element index is determined, it is exchanged with the first element in each round.
Swap (array, I, minIndex );

}

}

}

Bubble Sorting

Principle: a compares two adjacent elements in the array one by one.
If the number is smaller than the preceding number, the elements are exchanged.
After a round comparison, B must have the largest row
In the last position.
C. Compare the remaining elements each time. After n-1 comparisons, you can
Sort
To put it simply: Compare and exchange adjacent elements, and the maximum drift at the end of each time

Sample Code

Public class BubbleSort {

/**
* @ Param args
* @ Author wangliang
* @ Deprecated Bubble Sorting exercise
*/
Public static void main (String [] args ){
Int [] values = {3, 1, 6, 2, 9, 0, 7, 4, 5 };

Sort (values );

For (int I = 0; I <values. length; I ++) {// print the elements in the array after sorting

System. out. println ("Index:" + I + "value:" + values [I]);

}
}

/**
* @ Param int [] values
* @ Deprecated sorting
* @ Author wangliang
*
**/
Public static void sort (int [] values ){
Int temp;
For (int I = 0; I <values. length; I ++) {// Number of partitions

For (int j = 0; j <values. length-I-1; j ++) {// number of comparisons
If (values [j]> values [j + 1]) {

Temp = values [j];

Values [j] = values [j + 1];

Values [j + 1] = temp;
Principle: a divides the array into two parts.
Compare with each of the previous parts. If the current element is small
One point to be compared.
B. Locate the proper position and insert it.


}
}
}
}
}

Insert sort

Principle: a divides the array into two parts.
Compare with each of the previous parts. If the current element is small
One point to be compared.
B. Locate the proper position and insert it.


Sample Code

Public class InsertSort {

/**
* @ Param args
* @ Author wangliang
* @ Deprecated insert sorting exercise
*/
Public static void main (String [] args ){
Int [] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
InsertSort is = new InsertSort ();

Is. sort (arr );

For (int I = 0; I <arr. length; I ++ ){
System. out. print (arr [I] + "");
}
}

/**
* @ Author wangliang
* @ Deprecated sort comparison
* @ Param arr
* []
*
**/
Public static void sort (int [] arr ){
For (int I = 1; I <arr. length; I ++ ){
Int insertVal = arr [I];
// Compare insertValue preparation with the previous number
Int index = I-1;

While (index> = 0 & insertVal <arr [index]) {
// Move arr [index] backward
Arr [index + 1] = arr [index];
// Move the index one byte forward
Index --;
}

// Insert insertValue to an appropriate position
Arr [index + 1] = insertVal;
}
}

}


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.