First, bubble sort (bubblesort)
1. Basic ideas:
Sets the sort table to N, compares the values of adjacent elements from backward forward or backward 22, and if the relative order of the two is not correct (a[i-1] > A[i]), then swaps them,
The result is that the smallest element is swapped to the first position of the sequence to be sorted, and we call it a trip bubbling. The next time bubbling, the smallest element determined by the previous trip
No longer participating in the comparison, the ordered sequence is reduced by one element, and each bubbling result places the smallest element of the sequence in the "front" of the sequence.
2. Algorithm implementation
Package bubble sort;/** * Adjacent Data 22 comparison, large row above, small row below the first can be discharged the smallest value * Second secondary discharge of the second small value * The third time to discharge the third small value * and so on the order of expulsion * Optimal time complexity: O (n) (indicates that the traversal of a discovery without any Change the element, sort the end. The worst time complexity: O (N2) Stability: Stable * @author Administrator * */public class Bubblesort {//Primary version (compared from left to right)/** * No. 0 order ==0546897231 1th order = = 0156897432 2nd Order ==0126897543 3rd Order ==0123897654 4th Order ==0123498765 5th Sort ==0123459876 6th Order ==0123456987 7th order = = 0123456798 8th Order ==0123456789 9th Order ==0123456789 Final Sort ==0123456789 * optimal time complexity: O (n) (indicates that traversal of a discovery does not have any elements that can be exchanged, the sort ends.) Worst time complexity: O (N2) Stability: stable */public static void Sort1 (int[] num) {int i,j,temp;for (i=0;i<num.length;i++) {//First i= 0, discharge the smallest value 0//second I=1, drain the second small value 1for (j=i+1;j<num.length;j++) {if (Num[i]>num[j]) {Temp=num[i];num[i]=num[j];num[j] =temp;}}}} Intermediate version (compare right to left) public static void Sort2 (int[] num) {int i,j,temp;for (i=0;i<num.length;i++) {for (j=num.length-1;j> i;j--) {if (Num[j-1]>num[j]) {temp=num[j-1];num[j-1]=num[j];num[j]=temp;}} String str= ""; for (int k:num) {str+=k;} System.out.println ("+i+" = "+str");} String str2= ""; for (int k:num) {str2+=k;} SyStem.out.println ("final sort = =" +str2);} Ultimate public static void Sort3 (int[] num) {int I,j,temp;boolean flag=true;for (i=0;i<num.length&&flag;i++) { Flag=false;for (j=num.length-1;j>i;j--) {//From right to left two-phase comparison, greater than the description can exchange values, less than using Flag=false directly skip if (Num[j-1]>num[j]) {Temp =num[j-1];num[j-1]=num[j];num[j]=temp;flag=true;}}}} public static void Main (String args[]) {int[] num={5,2,4,6,8,9,7,1,3,0};//sort1 (num);//sort2 (num); sort3 (num);}}
Second, choose the sort
1. Basic ideas:
The first of the never-lined parts is the smallest (maximum), and then the remaining comparison, if there is a smaller (larger) one, note the subscript,
With this as the new minimum (maximum) value, the comparison is continued and the minimum value can be obtained after the end of the trip.
For example: Initial sequence: {49 27 65 97 76 12 38}
1th Trip: 12 and 49 Exchange:{27 65 97 76 49 38} (select the first number compared to the remaining 22, the interaction position, the first minimum value)
2nd trip: 27 Do not move: {65 97 76 49 38} (the first number is already lined up, select the second number compared to the remaining 22, the interaction position, the second minimum value)
3rd trip: 65 and 38 Exchange: 97 76 49 65} (the second number is already in the row, select the third number compared to the remaining 22, the interaction position, the third minimum value)
4th trip: 97 and 49 Swap: 12 27 38 49{76 97 65} and so on
5th trip: 76 and 65 swap: 12 27 38 49 65{97 76}
6th trip: 97 and 76 Exchange: 12 27 38 49 65 76 97 Complete
2. Algorithm implementation:
Package simple selection sorting;/** * Select a min to do the benchmark and other data compared to each other, if the comparison of the number of large then the current number of the assignment to the min * and so on * @author Administrator * */public class Selectsort {//Simple select Sort, select a min to do benchmark and other data compare to each other/** * Optimal time complexity: O (N2) Worst time complexity: O (N2) Stability: unstable (consider ascending each time to select the maximum case) * @param num */public static void Sort (int[] num) {int i,j,min,temp;for (i=0;i<num.length;i++) {min=i;//defines the current subscript as the lowest value subscript for (j=i+1;j<num.length;j++ {if (Num[min]>num[j]) {min=j;//If there is a keyword less than the current minimum value, assign the subscript of this keyword to min}}if (i!=min) {//If min is not equal to I, the min is changed, that is, the above compares true to each other, There is a minimum value, Exchange temp=num[i];num[i]=num[min];num[min]=temp;} String str= ""; for (int k:num) {str+=k;} System.out.println ("+i+" = "+str");} String str2= ""; for (int k:num) {str2+=k;} System.out.println ("final sort = =" +str2);} public static void Main (String args[]) {int[] num={5,2,4,6,8,9,7,1,3,0};sort (num);}}
Three, insert sort (insertion sort)
1. Basic ideas:
There is an ordered sequence of data that requires a number to be inserted into the sorted data sequence, but this data sequence is still ordered after insertion,
It's time to use a new sort method--insert sort, the basic operation of inserting a sort is to insert a data into the ordered data that is already sorted,
In order to obtain a new, number plus one ordered data, the algorithm is suitable for the ordering of a small amount of data, the time complexity of O (n^2). is a stable sorting method.
Each time a data element to be sorted is inserted into the appropriate position in the previously sorted sequence, the sequence remains orderly until the data element to be sorted is fully inserted.
"Example":
[Initial key Words] [+]---(select)
j=2 (38) [38 49] 65 97 76 13 27 49 (select 38 Insert to 49 front)
j=3 [+](select 65 Insert in front of 49)
J=4 (+ +)(select 97 Insert to 65 front)
J=5 (76) [38 49 65 76 97] 13 27 49 (etc.)
J=6 (13) [13 38 49 65 76 97] 27 49
J=7 (27) [13 27 38 49 65 76 97] 49
J=8 (49) [13 27 38 49 49 65 76 97]
2. Algorithm implementation:
Package direct insert sort; public class Insertsort {/** * Insert sort * * @paramarr * @return */public static void Insert (int[] num) { for (int i=1;i<num.length;i++) //n-1 This scan, inserting the n-1 element { int temp=num[i] forward, in turn; Each trip inserts num[i] into the preceding sort sub-sequence int J; for (j=i-1;j>=0&&temp<num[j];j--) { num[j+1]=num[j]; Move the previous larger element backward } num[j+1]=temp; The temp value reaches the insertion position, String str= ""; for (int k:num) {str+=k;} System.out.println ("+i+" = "+str"); } String str2= ""; for (int k:num) {str2+=k;} System.out.println ("final sort = =" +str2); } public static void Main (string[] args) {//TODO auto-generated method stub int[] num = {5, 2, 4, 6, 8, 9, 7, 1, 3, 0};insert (num);}}
The thought complexity of various sorting algorithms and its Java program implementation