Common methods of sorting in daily operations are: bubble sort, quick sort, sort by selection, insert sort, hill sort, even base sort, cocktail sort, bucket sort, pigeon nest sort, merge sort etc.
First, bubble sort
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 smaller 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 smallest 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. * * @paramnumbers * Array of integers that need to be sorted*/ Public Static voidBUBBLESORT01 (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 positions
temp =Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; } } } }
Note: The above does not know what sort, compares the elements of the datum position with the following elements, if the datum position value is smaller than the following element, then the swap position, the swapped element is the new datum element. The following is the real bubble sort.
Public Static voidBubblesort (int[] a) {inttemp; intSize =a.length; for(intI=1; i<size; i++) { for(intj=0; j<size-i; J + +) { if(A[j] < a[j+1]) {temp=A[j]; A[J]=a[j+1]; A[j+1]=temp; } } for(intaa:a) System.out.print (AA+","); System.out.println (); }}
Second, quick sort
Quick sort use the divide-and-conquer strategy to divide a sequence into two sub-sequences.
/*** Quick Sort * * Select an element from the series, called the "datum" * Reorder the sequence, 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 at the back of 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. * * @paramnumbers *@paramStart *@paramEnd*/ 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); } }
Iii. Choice of sorting
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 * Find the smallest element in the unordered sequence, and place it at the start of the sort sequence * then continue looking for the smallest element from the remaining unsorted elements and place it at the start of the sort sequence. * And so on until all elements are sorted. * * @paramnumbers*/ Public Static voidSelectsort (int[] numbers) { intSize =numbers.length; inttemp; 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; } }
Iv. Insert Sort
The insertion sort works by constructing an ordered sequence, for unsorted data, from a backward forward scan in a sorted sequence, to find the appropriate location and insert it. See the code and comments for specific steps.
/*** Insert Sort * * Starting with the first element, the element can be thought to have been sorted * Take out the next element, scan from backward forward in sequence of ordered elements * if the element (sorted) is greater than the new element, move the element to the next position * Repeat step 3, straight To locate a sorted element that is less than or equal to the position of the new element * insert a new element into the position * Repeat step 2 *@paramnumbers*/ Public Static voidInsertsort (int[] numbers) { intSize =Numbers.length, temp, J; for(intI=1; i<size; i++) {Temp=Numbers[i]; for(j = i; j > 0 && Temp < numbers[j-1]; j--) Numbers[j]= Numbers[j-1]; NUMBERS[J]=temp; } }
Five, merge sort
Merge sort is an efficient sort algorithm based on merging operations, which is the operation of merging two sorted sequences into a sequence.
/*** Merge Sort * * Apply space to size two of the sum of the sorted sequence, the space used to hold the merged sequence * Set two pointers, the initial position is the starting position of two sorted sequences * compare two pointers to the element, select a relatively small element into the Merge space and move the pointer to the next position * Repeat step 3 until a pointer reaches the end of the sequence * Copy all remaining elements of another sequence directly to the end of the merge sequence * *@paramnumbers*/ Public Static voidMergeSort (int[] numbers,intLeftintRight ) { intt = 1;//number of elements per group intSize = Right-left + 1; while(T <size) { ints = t;//number of elements per group in this cyclet = 2 *s; inti =Left ; while(i + (t-1) <size) {Merge (numbers, I, I+ (s-1), i + (t-1)); I+=T; } if(i + (s-1) <Right ) Merge (numbers, I, I+ (s-1), right); } }
/*** Merge Algorithm implementation * *@paramData *@paramp *@paramQ *@paramR*/ Private Static voidMergeint[] Data,intPintQintr) {int[] B =New int[Data.length]; ints =p; intt = q + 1; intK =p; while(s <= q && t <=r) {if(Data[s] <=Data[t]) {B[k]=Data[s]; S++; } Else{B[k]=Data[t]; T++; } k++; } if(s = = q + 1) B[k+ +] = data[t++]; Elseb[k+ +] = data[s++]; for(inti = P; I <= R; i++) Data[i]=B[i]; }
Several common sorting methods for Java implementation