1 /*Insert Sort--is the basic sort of the performance is better (more suitable for basic ordered sort)2 * Sort ideas: A sequence, the front is ordered, the back is unordered, take out an element to insert into the already ordered3 * Part (here involves moving the operation)--Find the position first, then move4 * Features: A trip to sort down the position of each element may not be fixed5 * Complexity of Time: O (N2)6 * sequencing Stability: Stable7 * Usage Scenario: The data is basically ordered in the case8 * */9 Public classTestinsertsort {Ten One Public Static voidMain (string[] args) { A int[] List1 = {10,8,9,7}; - Insertsort (list1); - for(inti = 0; i < list1.length; i++){ theSystem.out.print (List1[i] + ""); - } - - } + - Public Static voidInsertsort (int[] list) { + for(inti = 1; I <list.length; i++){ A //record values that are now being compared at inttemp =List[i]; - intj =i; - //compare to the previous ordered section find location insert "satisfy >0 and smaller than previous element" - while(J > 0 && list[j-1] >temp) { -LIST[J] = list[j-1];//move to the right . -j--; in } -LIST[J] =temp; to } + } - the}
1 /*Bubble Sort--a simple sort2 * Thought: Is the comparison of each of the two adjacent elements-the interchange until the end of the sequencing, to determine the largest element3 * Specific implementation: Nested Loops required4 * Outer loop: Control sort pass number--n-15 * Inner Loop: Control the number of comparisons per trip n-1/n-2 ....6 * Time complexity of bubble sort: O (N2)--compare + swap7 * Sort Stability: Refers to elements that have the same value when the relative position remains unchanged after sorting8 * The essence is that the condition is greater than and not greater than or equal to9 * */Ten Public classTestbubblesort { One A Public Static voidMain (string[] args) { - int[] list = {10, 8,7,9,2,3,1,0}; - int[] List1 =Bubblesort (list); the for(inti = 0; i < list1.length; i++){ -System.out.print (List1[i] + ""); - } - } + - Public Static int[] Bubblesort (int[] list) { + //number of times the external loop control is sorted A for(inti = 1; i < list.length; i++){ at //internally compare the number of times per trip - for(intj = 0; J <list.length-i; J + + ){ - if(List[j] > List[j+1]) {//Guaranteed sequencing Stability - inttemp =List[j]; -LIST[J] = list[j+1]; -LIST[J+1] =temp; in - } to } + } - returnlist; the } * $ Panax Notoginseng -}
1 /*Select Sort--a certain improvement in bubble sort, which reduces the number of good times2 * Basic idea: Compare the first element with all subsequent elements to find the smallest element with the first interchange3 * Complete the order once, then find the second small, etc.4 * Specific implementation: Nested Loops (outer control sort pass number, internal control exchange number)5 * Select time complexity for sorting: O (N2)6 * Stability of sequencing: stable7 * Usage Scenario: Small amount of data8 * */9 Public classTestselectsort {Ten One Public Static voidMain (string[] args) { A int[] List1 = {10,8,0,7,9,7,6,5,4}; - Selectsort (list1); - for(inti = 0; i < list1.length; i++){ theSystem.out.print (List1[i] + ""); - } - } - + //and orderly not participating in the comparison "reduce the number of good times" - Public Static voidSelectsort (int[] list) { + for(inti = 0; i < list.length-1; i++){ A for(intj = i + 1; J < List.length; J + +){ at if(List[i] >List[j]) { - inttemp =List[i]; -List[i] =List[j]; -LIST[J] =temp; - } - } in } - } to +}
Data structure--sort--simple sort