zoukankan      html  css  js  c++  java
  • js实现的快速排序

    快速排序是一种平均性能非常优秀的排序算法,在很多场合都会应用到他。
    了解快速排序于对开放高效率的软件有很重要的作用。

    但是有不少的书本讲得并不是很清楚,而且不同的教材的实现方式也不尽相同,
    我这里将最简单的快速排序的思路写出来供大家参考。

    希望不管是使用什么语言都能从这个简单的代码里很方便的掌握快排思路与编写方式

     1     function quick_sort(list, start, end) {  
     2       if (start < end) {  
     3         var pivotpos = partition(list, start, end);   //找出快排的基数  
     4         quick_sort(list, start, pivotpos - 1);        //将左边的快排一次  
     5         quick_sort(list, pivotpos + 1, end);          //将右边的快排一次  
     6       }  
     7     }  
     8       
     9       
    10     //将一个序列调整成以基数为分割的两个区域,一边全都不小于基数,一边全都不大于基数  
    11     function partition(list, start, end) {  
    12       var pivotpos = start;  
    13       var pivot = list[start];  
    14       var tmp;  
    15       for(var i = start + 1; i <= end; i ++) {  
    16         if (list[i] < pivot) {  
    17           tmp = list[i];  
    18           pivotpos += 1;  
    19           list[i] = list[pivotpos];  
    20           list[pivotpos] = tmp;  
    21         }  
    22       }  
    23       
    24       tmp = list[start];  
    25       list[start] = list[pivotpos];  
    26       list[pivotpos] = tmp;  
    27       return pivotpos;  
    28     }  
    29       
    30       
    31     var list = [8,2,4,65,2,4,7,1,9,0,2,34,12];  
    32       
    33     quick_sort(list, 0, list.length);  
  • 相关阅读:
    Font Awesome 中文网
    mobileselect学习
    JavaScript模块化
    webpack基本使用
    MVVM架构方式
    http-server开启测试服务器
    json-server模拟服务器API
    vue-router
    git的使用
    Vue生命周期
  • 原文地址:https://www.cnblogs.com/lhgstudio/p/3461178.html
Copyright © 2011-2022 走看看