Objective:
Thick one of the "algorithm fourth edition", see One-fifth has benefited a lot, and the top one-fifth is about the sort, bubble sort, quick sort, heap sort, direct insertion sort, hill sort and so on, understanding is not particularly difficult, today with you to share one of them--direct insertion sorting algorithm, Here I have implemented JavaScript and Java in two language versions.
Ideas:
In life, what would we do if we were to sort the cards by size?
① first find a card and put it on the table.
② took out the second card, put it above the first one, and put it down before the first big one.
③ take out the third card, than the first small on the top, compared to the first big and the second one, than the second one small put above, than the second grow up and the next contrast ...
④ Repeat steps the final poker sort is done.
From these three steps we can draw some conclusions
① the cards placed on the table are always orderly.
② took the card in contrast to the previous one until it was smaller than that one.
Realize:
Direct Insert sort key: Each step inserts an object to be sorted, by its sort code size, into the appropriate position of the previously ordered set of objects until the object is fully inserted.
To be more clear, put the array elements that are currently sorted into the proper positions of the elements that are already ordered before, and loop to the full order;
JavaScript implementations:
function Insertsort (arr) {for (var i = 1;i<arr.length;i++) { var temp = arr[i]; The element to be sorted var j = I; while (j>0) {if (temp < arr[j-1]) {//If the previous number is larger than its own var a = arr[j-1]; Exchange it and its previous element arr[j-1] = arr[j]; ARR[J] = A; j--;//Exchange The position of J J--}else}}} return arr;
When the input array [6,1,2,7,9,3,4,5,10,8,8,9], the resulting results are [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10]
Then analyze the steps in the while:
function Insertsort (arr) { //arr = [6,1,2,7,9,3,4,5,10,8,8,9] for (var i = 1;i<arr.length;i++) { var temp = Arr[i]; For example i=1 words temp = arr[1]; var j = I; while (j>0) { if (temp < arr[j-1]) {//1 < 6 equals True var a = arr[j-1]; Arr[j-1] = arr[j]; arr[j] = A;//Exchange 1-6 j--;//i--i=0 end of this cycle arr = [1,6,2,7 ...] again find 2 loop}else{j=0 ; }}} return arr;}
Implementation of Java:
public classSortutil {//Direct insert sort algorithm public static voidSort (comparable[] a) {for (int i=1;i<a.length;i++) {Comparable TEM =A[i]; To sort elements Int J =I while (j>0) {if (Less (tem,a[j-1]) {//less comparison element tem is smaller than the previous one Exch (a,j,j-1); j--; }else{j=0; }}}}//* CompareTo Comparison method * @param {comparable} v if V is less than W returns True * @param {comparable} w if V is greater than or equal to W returns false * @return {bool EAN} returns the value of the comparison * * */public static BooleanLess (comparable v,comparable W) {return V.compareto (W) < 0; }/* * CompareTo Interchange Element * @param {comparable[]} a sortable array to swap * @param {int} I exchange 1 subscript * @param {int} J Exchange 2 Subscript * */Public stat IC void Exch (comparable[] A,int i,intj) {Comparable t =A[i]; A[i] =A[J]; A[J] =T } public static voidShow (comparable[] a) {for (int i=0;i<a.length;i++) {Stdout.print (a[i]+ ""); Stdout.println (); }}/* * CompareTo TEST element is ordered * @param {comparable[]} a sortable array * @return {Boolean} is an ordered array * * */public static Boolean I Ssorted (comparable[] a) {for (Int. i=1;i<a.length;i++) {if (less (a[i],a[i-1])) {return false;}} return True}/* * CompareTo convert int[] to integer[] * @param {int[]} a int array * @return {integer[]} converted integer[] * */public s Tatic integer[] Intto (int[] a) {integer[] con = new integer[a.length]; for (int i=0;i<a.length;i++) {Con[i] = new Integer (A[i]);} return con;//main public static void main (string[] args) {integer[] a = Intto (in . readints (Args[0])); Runtime runtime = new runtime (), sort (a), if (!issorted (a)) {Stdout.println ("Sort is error"); System.exit (0);}; Stdout.println (runtime. End ()); }}
Summarize:
① catch the point, each step inserts an object to be sorted, by its sort code size, into the appropriate position of the previously ordered set of objects until the object is fully inserted.
② the step analysis in the learning process, the array is manually sorted in a logical way, the steps of analyzing it, and finally translating into code implementation
The direct insertion sorting algorithm is the basic method of hill sort, the front door of the algorithm always has to go up the ladder, only perseverance, hard study, can ascend and hope, List of mountains small.
"Insert sort algorithm" the sort of beginner algorithm--Direct Insert Sort