码迷,mamicode.com
首页 > 编程语言 > 详细

js:数据结构笔记12--排序算法(2)

时间:2014-10-20 09:55:52      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   java   for   strong   sp   数据   

高级排序算法:(处理大数据:百万以上)

  • 希尔排序:是插入排序的优化版;
  • 首先设置间隔数组,然后按照每个间隔,分别进行排序;
  • 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0],a[1]进行插入排序,直到最后一个;
  • 然后换下一个间隔值,直到所有间隔值排序完(当间隔值为1时,就是一般的插入排序);
  • 效果:

bubuko.com,布布扣

首先在类中添加间隔数组:
this.gaps = [5,3,1];
然后添加函数:
function shellsort() {
  for(var g = 0; g < this.gaps.length; ++g) {
    for(var i = this.gaps[g]; i < this.dataStore.length; ++i) {
      var temp = this.dataStore[i];
      for(var j = i; j >= this.gaps[g] && this.dataStore[j - this.gaps[g]] > temp; j -= this.gaps[g]) {
        this.dataStore[j] = this.dataStore[j - this.gaps[g]];
      }
      this.dataStore[j] = temp;
    }
  }
 }

demo: 

 

js:数据结构笔记12--排序算法(2)

标签:blog   http   io   ar   java   for   strong   sp   数据   

原文地址:http://www.cnblogs.com/jinkspeng/p/4036438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!