• js快速排序算法


    真正的快速排序算法一:
    
    function quickSort(array){
    	function sort(prev, numsize){
    		var nonius = prev;
    		var j = numsize -1;
    		var flag = array[prev];
    		if ((numsize - prev) > 1) {
    			while(nonius < j){
    				for(; nonius < j; j--){
    					if (array[j] < flag) {
    						array[nonius++] = array[j]; //a[i] = a[j]; i += 1;
    						break;
    					};
    				}
    				for( ; nonius < j; nonius++){
    					if (array[nonius] > flag){
    						array[j--] = array[nonius];
    						break;
    					}
    				}
    			}
    			array[nonius] = flag;
    			sort(0, nonius);
    			sort(nonius + 1, numsize);
    		}
    	}
    	sort(0, array.length);
    	return array;
    }
    console.log(quickSort([23,11,89,45,67,76,56,99]))
    
    快速排序算法二:
    
    function swap(items, firstIndex, secondIndex){
        var temp = items[firstIndex];
        items[firstIndex] = items[secondIndex];
        items[secondIndex] = temp;
    }
    
    function partition(items, left, right) {
    
        var pivot   = items[Math.floor((right + left) / 2)],
            i       = left,
            j       = right;
    
    
        while (i <= j) {
    
            while (items[i] < pivot) {
                i++;
            }
    
            while (items[j] > pivot) {
                j--;
            }
    
            if (i <= j) {
                swap(items, i, j);
                i++;
                j--;
            }
        }
    
        return i;
    }
    
    function quickSort(items, left, right) {
    
        var index;
    
        if (items.length > 1) {
    
            left = typeof left != "number" ? 0 : left;
            right = typeof right != "number" ? items.length - 1 : right;
    
            index = partition(items, left, right);
    
            if (left < index - 1) {
                quickSort(items, left, index - 1);
            }
    
            if (index < right) {
                quickSort(items, index, right);
            }
    
        }
    
        return items;
    }
    
    var items = [4, 2, 6, 5, 3, 9];
    // first call
    var result = quickSort(items);
    var result2 = quickSort(items, 0, items.length - 1);
    
    快速排序算法三:
    
    "快速排序"的思想很简单,整个排序过程只需要三步:
          (1)在数据集之中,选择一个元素作为"基准"(pivot)。
    
      (2)所有小于"基准"的元素,都移到"基准"的左边;所有大于"基准"的元素,都移到"基准"的右边。
    
      (3)对"基准"左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
    
    var quickSort = function(arr) {
    
      if (arr.length <= 1) { return arr; }
    
      var pivotIndex = Math.floor(arr.length / 2);
    
      var pivot = arr.splice(pivotIndex, 1)[0];
    
      var left = [];
    
      var right = [];
    
      for (var i = 0; i < arr.length; i++){
    
        if (arr[i] < pivot) {
    
          left.push(arr[i]);
    
        } else {
    
          right.push(arr[i]);
    
        }
    
      }
    
      return quickSort(left).concat([pivot], quickSort(right));
    
    };
    console.log(quickSort([23,11,89,45,67,76,56,99]))
    

      

  • 相关阅读:
    [USACO07DEC]观光奶牛Sightseeing Cows
    洛谷 U3348 A2-回文数
    LOJ #2037. 「SHOI2015」脑洞治疗仪
    1441 士兵的数字游戏
    BZOJ 1108: [POI2007]天然气管道Gaz
    P3047 [USACO12FEB]附近的牛Nearby Cows
    POJ 3061 Subsequence
    Hdu 5776 sum
    1052 最大M子段和
    1288 埃及分数
  • 原文地址:https://www.cnblogs.com/dearxinli/p/8881406.html
Copyright © 2020-2023  润新知