Common sorting methods in daily operations are: bubble sort, quick sort, select sort, insert sort, hill sort, etc.
Bubble sort is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.
* Bubble Method Sort * Compare adjacent elements. If the first one is bigger than the second one, swap them both. * For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number. * Repeat the above steps for all elements except the last one. * Continue to repeat the above steps each time for fewer elements until there are no pairs of numbers to compare. * *@param numbers*an array of integers that need to be sorted*/ Public Static voidBubblesort (int[] numbers) { intTemp//Record interim intermediate values intsize = Numbers.length;//Array Size for(inti = 0; i < size-1; i++) { for(intj = i + 1; J < size; J + +) { if(Numbers[i] < numbers[j]) {//Exchange two number of positionstemp =Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; } } } }
Quick sorting is the use of a divide-and-conquer strategy to divide a sequence into two sub-sequences.
*Quick Sort*pick an element from the sequence called the "benchmark"*Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). After this split,*The benchmark is its last position. This is called a split (partition) operation. *recursively sorts sub-columns that are less than the base value element and sub-columns that are larger than the base value element. * *@param numbers*@param start*@param end*/ Public Static voidQuickSort (int[] numbers,intStartintend) { if(Start <end) { intbase = Numbers[start];//Selected Datum value (first value as Datum) intTemp//Record interim intermediate values inti = start, j =end; Do { while((Numbers[i] < base) && (I <end)) I++; while((Numbers[j] > Base) && (J >start)) J--; if(I <=j) {Temp=Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; I++; J--; } } while(I <=j); if(Start <j) QuickSort (Numbers, start, j); if(End >i) QuickSort (numbers, I, end); } }
Selecting sort is a simple and straightforward way to sort the smallest values in a sequence, and then place them at the very end.
*Select Sort*finds the smallest element in an unordered sequence, holding it to the starting position of the sort sequence*Then continue looking for the smallest element from the remaining unsorted elements and place it at the end of the sort sequence. *and so on until all elements are sorted. * *@param numbers*/ Public Static voidSelectsort (int[] numbers) { intSize =Numbers.length, temp; for(inti = 0; i < size; i++) { intK =i; for(intj = size-1; J >i; j--) { if(Numbers[j] < numbers[k]) K =J; } temp=Numbers[i]; Numbers[i]=Numbers[k]; NUMBERS[K]=temp; } }
Several common sorting algorithms in Java