美文网首页
js 排序算法

js 排序算法

作者: 蘑菇酱960903 | 来源:发表于2018-03-23 19:09 被阅读0次
     /*生成一个[min,max)的随机数*/
            function randomNum1(min, max) {
                return Math.floor(Math.random()*(max - min + 1) + min);
            }
      /*生成array数组*/
            function array(min,max,count) {
                var i = 1, arr = [];
                while (i++ <= count) {
                    arr.push(randomNum1(min,max));
                }
                return arr;
            }
    
    
            /*冒泡排序*/
            function bubbleSort(arr) {
                var len = arr.length, temp; 
                for (var i = 0; i < len-1; i++) {
                    for (var j = i+1; j < len; j++){
                        if (arr[i] > arr[j]) {
                            temp = arr[j];
                            arr[j] = arr[i];
                            arr[i] = temp;
                        }
                    }
                }
                return arr;
            }
    
        /*选择排序*/
        function selectSort(arr) {
            var len = arr.length, temp, flag;
            for (var i = 0; i < len-1; i++) {
                flag = i;
                for (var j = i+1; j < len; j++) {
                    if (arr[flag] > arr[j]) {
                        flag = j;
                    }
                }
                if (flag !== i) {
                    temp = arr[flag];
                    arr[flag] = arr[i];
                    arr[i] = temp;
                }
            }
            return arr;
        }
    
            /*插入排序*/
            function insertSort(arr) {
                var len = arr.length, temp;
                for (var i = 1; i < len; i++) {
                    temp = arr[i];
                    for (var j = i; j > 0; j--) {
                        if (temp < arr[j-1]) {
                            arr[j] = arr[j-1];
                            arr[j-1] = temp;
                        }
                    }
                }
                return arr;
            }
    
    
        /*二分排序*/
        function sort(arr) {
            var len = arr.length;
            for (var i = 1; i < len; i++) {
                var low, high, mid;
                low = 0;
                high = i-1;
                var temp = arr[i];
                while (low <= high) {
                    mid = Math.floor((low + high) / 2);
                    if (arr[mid] > temp) {
                        high = mid - 1;
                    } else {
                        low = mid + 1;
                    }
                }
                for (var j = i - 1; j > high; j--) {
                    arr[j + 1] = arr[j];
                }
                arr[high + 1] = temp;
            }
            return arr;
        }
    
            /*快速排序*/
            /*
            利用递归方法
            先选出temp = arr[low],low指向最小值,high指向最大值
            不断让arr[low] arr[high] 与temp比较,将数组分成两个部分,左边为都小于temp的数,右边为都大于temp的数,
            再继续递归,先把左边小于temp再一次操作,选出low,temp,high…… 然后右边操作
            */
            function quickSort(arr, low, high) {
                if (low < high) {               
                    var pre = Partition(arr, low, high);
                    quickSort(arr, low, pre-1);
                    quickSort(arr, pre+1, high);
                }
            }
            function Partition(arr, low, high) {
                var temp = arr[low];
                while (low < high) {
                    while (low < high && arr[high] >= temp) {
                        high--;
                    }
                    if (low < high) {
                        arr[low++] = arr[high];
                    }
                    while (low < high && arr[low] <= temp) {
                        low++;
                    }
                    if (low < high) {
                        arr[high--] = arr[low];
                    }
                }
                arr[low] = temp;
                return low;
            }
    

    相关文章

      网友评论

          本文标题:js 排序算法

          本文链接:https://www.haomeiwen.com/subject/tuexcftx.html