zoukankan      html  css  js  c++  java
  • 排序算法2--插入排序--折半插入排序

    折半插入排序

    折半插入排序(binary insertion sort)是对插入排序算法的一种改进,所谓排序算法过程,就是不断的依次将元素插入前面已排好序的序列中。

    在将一个新元素插入已排好序的数组的过程中,寻找插入点时,将待插入区域的首元素设置为a[low],末元素设置为a[high],则轮比较时将待插入元素与a[m],其中m=(low+high)/2相比较,如果比参考元素小,则选择a[low]到a[m-1]为新的插入区域(即high=m-1),否则选择a[m+1]到a[high]为新的插入区域(即low=m+1),如此直至low<=high不成立,即将此位置之后所有元素后移一位,并将新元素插入a[high+1]。

    java实现:

     1 package 平时常用;
     2 
     3 public class _6折半插入排序 {
     4     public static void main(String[] args) {
     5         int[] a={3,5,6,2,1,4};
     6         zhebanInsert(a);
     7         for (int i : a) {
     8             System.out.print(i);
     9         }
    10     }
    11     public static void zhebanInsert(int[] a){
    12         for (int i = 1; i < a.length; i++) {
    13             int low = 0;
    14             int high = i-1;
    15             int temp = a[i];
    16             while (low<= high) {
    17                 int mid = (low+high)/2;
    18                 if (temp >= a[mid]) {
    19                     low = mid+1;
    20                 }else{
    21                     high = mid-1;
    22                 }
    23             }
    24             //整体后移
    25             for (int j = i; j>=low+1; j--) {
    26                 a[j] = a[j-1];
    27             }
    28             //插入到指定的位置
    29             a[low] = temp;
    30         }
    31     }
    32 }

    js实现:

     1 function zhebanSort(a){
     2     for (var i = 1; i < a.length; i++) {
     3             var low = 0;
     4             var high = i-1;
     5             var temp = a[i];
     6             while (low<= high) {
     7                 var mid = Math.floor((low+high)/2);
     8                 if (temp >= a[mid]) {
     9                     low = mid+1;
    10                 }else{
    11                     high = mid-1;
    12                 }
    13             }
    14             //整体后移
    15             for (var j = i; j>=low+1; j--) {
    16                 a[j] = a[j-1];
    17             }
    18             //插入到指定的位置
    19             a[low] = temp;
    20         }
    21 }
    22 var a = new Array(7,2,6,5,1,4,3);
    23 zhebanSort(a);
    24 document.write("_2折半插入排序"+a+"<br />");

    python实现:

     1 def zhebanInsert_sort(listNums):
     2     for i in range(1,len(listNums)):
     3         low = 0
     4         high = i-1
     5         temp = listNums[i]
     6         #找到要插入的位置
     7         while low <= high:
     8             mid = int((low + high) // 2)
     9             if temp >= listNums[mid]:
    10                 low = mid +1
    11             else:
    12                 high = mid -1
    13         j = i-1
    14         #整体后移
    15         while j >= low:
    16             listNums[j+1] = listNums[j]
    17             j -= 1
    18         #找到插入位置,插入
    19         listNums[low] = temp
    20     return listNums
  • 相关阅读:
    union
    大端和小端
    迭代器
    STL
    动态内存管理2
    动态内存管理
    关于 FlexBox
    CSS的居中问题
    操作符
    JavaScript介绍
  • 原文地址:https://www.cnblogs.com/zhangxue521/p/6748127.html
Copyright © 2011-2022 走看看