zoukankan      html  css  js  c++  java
  • 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]))
    

      

  • 相关阅读:
    win服务大全 (转)
    给大家贴一点好东东 喜欢电影的朋友请看
    滚动字幕的制作 marquee
    嵌入式软件工程师读书计划总纲 转
    优秀男人的十五条标准[转]
    寒冷的冬天到了,我们开始画饼充饥
    如何用.NET创建Windows服务 [转]
    请教:如何进行存储过程的调试
    人际交往(转)
    用户控件 与 Response.Redirect 转向的问题
  • 原文地址:https://www.cnblogs.com/dearxinli/p/8881406.html
Copyright © 2011-2022 走看看