C ++, C #, java Algorithm learning diary 04 ---- binary insertion sorting

Source: Internet
Author: User

C ++, C #, java Algorithm learning diary 04 ---- binary insertion sorting

After several articles on the sorting algorithm, we found that the so-called sorting is to determine the position of each element in an array, and then sit on the SN. The process is to find the position of the element. Location determination, the use of the Bipartite method can achieve high efficiency, we apply it to insert sorting, even if it is an optimization of the sorting in the previous article, it can improve efficiency.

Basic Idea:

Similar to the insertion sorting in the previous article, it divides the sorted and unsorted parts, and inserts unsorted elements one by one. However, the insertion process is different and you need to find an intermediate position each time, compare the size of the element with the middle position, and then move the high position to the left or right based on the size, and then compare the middle element until a proper position is found (that is, the insert position is determined by the binary method) and then move some of the elements to make room for the elements to be inserted, and then insert the sequence.

C ++ instance:
# Include
 
  
Using namespace std; void HalfSort (int * array, int Length) {// minimum defined, maximum, median value, data to be inserted, insertion position int low, high, middle, temp, index, I, j; for (I = 1; I
  
   
Temp) // compare with the header and tail. If the range class is not omitted, the loop {while (low <= high) // The binary lookup insert position {middle = (low + high)/2; if (array [middle]> temp) {high = middle-1;} else low = middle + 1;} // Finally, the location indexindex = low ;} if (array [0]> = temp) // if it is smaller than the first element, insert it directly to the first position {index = 0;} if (array [I-1] <= temp) // if it is larger than the last element, insert it directly to the last position {index = I;} // shift backward to free up the inserted space for (j = I-1; j> = index; j --) {array [j + 1] = array [j];} array [index] = temp; // insert} // output for (I = 0; I
   
    

 

C # example:
Using System; using System. collections. generic; using System. linq; using System. text; namespace HalfSort2 {class Sort {public void halfSort (int [] array) {int low, high, middle, temp, index, I; for (I = 1; I <array. length; I ++) {low = 0; high = I-1; temp = array [I]; index = I; if (array [0] <temp & array [I-1]> temp) {while (low <= high) // Binary Search insert position {middle = (low + high)/2; if (array [middle]> temp) {high = middle-1 ;} else low = middle + 1;} index = low;} if (array [0]> = temp) // if it is smaller than the first element, insert it directly to the first position {index = 0;} if (array [I-1] <= temp) // if it is larger than the last element, insert it directly to the last position {index = I;} // shift backward to free up the inserted space for (int j = I-1; j> = index; j --) {array [j + 1] = array [j];} array [index] = temp; // insert} foreach (int j in array) {Console. write (j +);} Console. writeLine ();} static void Main (string [] args) {int [] array = new int [] {63,4, 24,1, 3,15}; Sort sorter = new Sort (); sorter. halfSort (array );}}}


The preceding result is:

Java instance:
package Sort;public class Sort {public void halfsort(int[] array){int low,high,middle,temp,index,i;for(i=1;itemp){         while(low<=high){         middle=(low+high)/2;         if(array[middle]>temp){         high=middle-1;         }         else{         low=middle+1;         }         }         index=low;         }         if(array[0]>=temp){         index=0;         }         if(array[i-1]<=temp){         index=i;         }         for(int j =i-1;j>=index;j--){         array[j+1]=array[j];         }         array[index]=temp;}for(int j:array){         System.out.print(j+  );         }}public static void main(String[] args) {int[] array = new int[]{63,4,24,1,3,15};Sort sorter = new Sort();sorter.halfsort(array);}}


Result:

 

 

As a matter of fact, when I read the data, the Code does not contain if (array [0] Temp), if (array [0]> = temp), if (array [I-1] <= temp) such judgment statement, this is added by myself, if you do not have these judgment statements, the program will still go through some unnecessary loops when the sequence you sort is already an ordered sequence, rather than directly inserting the sorting statement in the previous article.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.