Basic idea: Let the number of sorted sequences be N, i take 1, 2, 3 ... n-1, find the record with the smallest sorting code from N-i + 1 records (Ri, Ri + 1 .... Rn) , Exchange with the i-th record, complete the sequence sorting after performing N-1 times.
//选择排序privatestaticvoidselectSort(int[] arr,int n){for(int i =0; i < n -1;++i){for(int j = i +1; j < n;++j){if(arr[i]> arr[j]){int t = arr[i];
arr[i]= arr[j];
arr[j]= t;}}}}
Bubble Sort
The basic idea: compare adjacent elements in pairs, and exchange them in reverse order. Every time, the largest or smallest element will be "floated up", and finally reach order.
//冒泡排序privatestaticvoidbubbleSort(in[] arr, into n){for(int i =0; i < n -1;++i){for(int j =0; j < n -1- i;++j){if(arr[j]> arr[j +1]){int t = arr[j];
arr[j]= arr[j +1];
arr[j +1]= t;}}}}
Merge sort
Combine several ordered sequences in pairs until the records to be sorted are all in an ordered sequence
Basic idea: Split the data to be sorted into two independent parts by one-pass sorting, and all the data in one part is smaller than all the data in the other part, and then quickly sort the two parts according to this method.