Implement the insertion Sorting Algorithm Using the binary method (the binary method is implemented using recursion) and the binary method is recursive.
I recently read this book "Introduction to algorithms" and found a problem in my exercise: the binary search method is used to implement insertion sorting, because the previous content has explained the Recursive Implementation of the binary method, so we can combine them here to solve the problem. If you don't talk much about it, go directly to the Code:
// Algrithms. cpp: defines the entry point of the console application. /// Use the binary method to complete insertion sorting, and use the recursive algorithm to complete the binary method # include "stdafx. h "int Binary_Divide (int A [], int low, int height, int key) {// recursive termination condition is input low> input height, at this time, the location is left or right of low. // you only need to determine the size between the key and A [low] to determine the location where the key should be placed. if (low> height) {if (key> A [low]) {return low + 1; // if the key is larger than A [low, then the key is placed in the next position of A [low]} else {return low; // if the key is smaller than A [low, then the key is placed on the position of A [low] (because the subsequent program will leave this position blank)} if (A [(low + height) /2]> key) {Binary_Divide (A, low, (low + height)/2-1, key);} else {Binary_Divide (A, (low + height) /2 + 1, height, key) ;}} int _ tmain (int argc, _ TCHAR * argv []) {int a [11] = {6, 10, 13, 3, 7, 20, 24,100, 1, 3, 6}; // The sorted array int array_length = sizeof (a)/sizeof (a [0]); // int key of the array size; // keyword // insert A [n] to the first n-1 number in the sorted order for (int I = 1; I <array_length; I ++) {key = a [I]; int j = I-1; int position = Binary_Divide (a, 0, j, key ); // returns the location where the key is located by the binary method. // after the position (including position) is obtained, the position is moved after the number of j. while (j> = 0 & j> = position) {if (a [j]> key) {a [j + 1] = a [j];} j --;} a [position] = key; // place the keyword in the correct position. // print out the array for (int k = 0; k <array_length; k ++) for each element arranged) {printf ("% d", a [k]);} printf ("% d \ n", position); // print the appropriate position} getchar (); return 0 ;}
The algorithm idea is very simple. It is nothing more than replacing the appropriate position of the elements to be sorted in the original linear search with the appropriate position using the binary method, what bothered me a long time ago was the determination of recursive termination. When learning binary search, we know that if a number does not exist in an array, the lower bound of the input array is greater than the upper bound, and Recursion ends, when this algorithm looks for a location, this recursive termination of the binary method will lead us to find a point closest to the "suitable location, therefore, the size judgment between this vertex and the keyword can be transmitted back to the correct position.
The first time I posted a blog, I also asked the experts in the park to give me more advice.