Java implements several common sorting methods (I)

Source: Internet
Author: User

Common sorting methods in daily operations include Bubble sorting, fast sorting, selection sorting, insert sorting, and Hill sorting, there are also base sorting, cocktail sorting, bucket sorting, Pigeon nest sorting, and merge sorting.

Bubble Sorting is a simple sort.Algorithm. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.

/** <br/> * bubble sort <br/> * <ul> <br/> * <li> compares adjacent elements. If the first is bigger than the second, exchange the two of them. </LI> <br/> * <li> performs the same operation on each adjacent element, from the first pair to the last one. At this point, the final element should be the largest number. </LI> <br/> * <li> repeat the preceding steps for all elements except the last one. </LI> <br/> * <li> continue to repeat the above steps for fewer and fewer elements each time until no comparison is required. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> * integer array to be sorted <br/> */<br/> Public static void bubblesort (INT [] numbers) {<br/> int temp; // record the temporary median value <br/> int size = numbers. length; // array size <br/> for (INT I = 0; I <size-1; I ++) {<br/> for (Int J = I + 1; j <size; j ++) {<br/> If (numbers [I] <numbers [J]) {// switch the positions of two numbers <br/> temp = numbers [I]; <br/> numbers [I] = numbers [J]; <br/> numbers [J] = temp; <br/>}

 

 

Quick sorting uses the divide and conquer method to divide a sequence into two subsequences.

/** <Br/> * fast sorting <br/> * <ul> <br/> * <li> pick an element from the sequence, called "benchmark" </LI> <br/> * <li> resorts the sequence. All elements smaller than the benchmark value are placed before the benchmark, all elements are placed behind the benchmark (the same number can reach any side ). After this split, <br/> * the benchmark is its last position. This is called a partition operation. </LI> <br/> * <li> recursively sorts subsequences smaller than the reference value element and subsequences greater than the reference value element. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> * @ Param start <br/> *@ param end <br/> */<br/> Public static void quicksort (INT [] numbers, int start, int end) {<br/> If (start <End) {<br/> int base = numbers [start]; // selected benchmark value (the first value serves as the benchmark value) <br/> int temp; // records the temporary median value <br/> int I = start, j = end; <br/> do {<br/> while (numbers [I] <base) & (I <End) <br/> I ++; <br/> while (numbers [J]> base) & (j> Start) <br/> j --; <br/> if (I <= J) {<br/> temp = numbers [I]; <br/> numbers [I] = numbers [J]; <br/> numbers [J] = temp; <br/> I ++; <br/> j --; <br/>}< br/>}while (I <= J ); <br/> If (start <j) <br/> quicksort (numbers, start, J); <br/> If (end> I) <br/> quicksort (numbers, I, end); <br/>}< br/>}

 

Selecting 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 of the sequence.

/** <Br/> * select sorting <br/> * <ul> <br/> * <li> find the smallest element in the unordered sequence, store it to the starting position of the sorting sequence </LI> <br/> * <li> continue to find the minimum element from the remaining unordered elements and put it at the end of the sorting sequence. </LI> <br/> * <li> and so on until all elements are sorted. </LI> <br/> * </ul> <br/> * @ Param numbers <br/> */<br/> Public static void selectsort (INT [] numbers) {<br/> int size = numbers. length, temp; <br/> for (INT I = 0; I <size; I ++) {<br/> int K = I; <br/> for (Int J = size-1; j> I; j --) {<br/> If (numbers [J] <numbers [k]) k = J; <br/>}< br/> temp = numbers [I]; <br/> numbers [I] = numbers [k]; <br/> numbers [k] = temp; <br/>}< br/>}

 

 

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.