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

    package
    {
     
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     import flash.text.TextField;
     import flash.utils.getTimer;
     
     public class SortTest1 extends Sprite
     {
      private var f:Function = function(a:Object,b:Object):int{return b.y-a.y}
      private var arr:Array = new Array();
      private var tmText:TextField = new TextField();
      
      public function SortTest1()
      {
       var i:int = 0;
       for(i = 0;i < 5000;i++)
       {
        arr.push({"y":0});
       }
       this.addChild(tmText);
       tmText.x = 100;
       tmText.y = 100;
       
       this.stage.addEventListener(MouseEvent.CLICK,onSort);
      }
      
      protected function onSort(event:MouseEvent):void
      {
       var a:int = getTimer();
       var i:int = 0;
       var j:int = 0;
       
       //for(j = 0;j < 24;j++)
       //{
        for(i = 0;i < 5000;i++)
        {
         arr[i].y = (Math.random() * 10000 + 1);
        }
        sortOnNumber(arr,"y",16,arr.length,0);
        //arr.sortOn("y");
       //}
       var b:int = getTimer();
       tmText.text = (b - a).toString();
      }
      
      internal const numVec:Vector.<Number>  = new Vector.<Number>(0xFFFF);
      internal function sortOnNumber(input:Object, name:String, options:uint, length:uint, startIndex:uint):void {
       if (length < 2) return;
       var right:uint = startIndex + length;
       var tempVec:Vector.<Number> = numVec, j:uint = startIndex;
       var p:Number = input[j][name], s:int = 1;
       for (; j < right; ++j) {
        e = input[j];
        t = e[name];
        tempVec[j] = t;
        s &= int(!(t > p));// ! > instead of <= to account for NaN
        p = t;
        if (t !== t) {
         --right;
         input[j] = input[right];
         input[right] = e;
         --j;
        }
       }
       --right;
       if (!s) quickSortOnNumber(tempVec, input, startIndex, right, 0);
       if (options & Array.DESCENDING) {
        j = startIndex;
        options = j + length - 1;
        while (options > j) {
         e = input[j];
         input[j] = input[options];
         input[options] = e;
         ++j;
         --options;
        }
       }
       return;
       var t:Number = 0, e:* = undefined;
      }
      
      internal function quickSortOnNumber(input:Vector.<Number>, sInput:Object, left:int, right:int, d:int):void {
       if (left >= right) return;
       var j:int = right;
       var i:int = left;
       var size:int = right - left;
       var pivotPoint:Number = input[uint((right >>> 1) + (left >>> 1))];
       do {
        if (size < 9) {
         do {
          pivotPoint = input[left];
          do {
           ++left;
           e = input[left];
           if (pivotPoint > e) {
            pivotPoint = e;
            t = sInput[left];
            do {
             q = left--;
             e = input[left];
             input[q] = e;
             sInput[q] = sInput[left];
            } while (int(left > i) & int(pivotPoint < e));
            input[left] = pivotPoint;
            sInput[left] = t;
           }
          } while (left < right);
          ++i;
          left = i;
         } while (i < right);
         return;
        }
        while (left < right) {
         f = input[right];
         while (f > pivotPoint) {
          --right;
          f = input[right];
         }
         e = input[left];
         while (e < pivotPoint) {
          ++left;
          e = input[left];
         }
         if (left < right) {
          input[left] = f;
          input[right] = e;
          t = sInput[left];
          sInput[left] = sInput[right];
          sInput[right] = t;
          ++left, --right;
         }
        }
        if (left === right) {
         e = input[left];
         q = int(e >= pivotPoint);
         q &= int(right > 0);
         right -= q;
         q = int(e <= pivotPoint);
         left += q;
        }
        if (i < right)
         quickSortOnNumber(input, sInput, i, right, d + 1);
        if (j > left) {
         i = left;
         right = j;
         pivotPoint = input[uint((right >>> 1) + (left >>> 1))];
         size = right - left;
         ++d;
        } else break
       } while (true);
       return;
       var e:Number = 0, f:Number = 0, t:* = undefined, q:int = 0;
      }
     }
    }

  • 相关阅读:
    项目开发中的注意点和技巧
    addslashes — 使用反斜线引用字符串
    PHP error_reporting() 错误控制函数功能详解
    零碎收集cocos知识
    LeetCode:二叉树的前序遍历【144】
    SpringBoot学习笔记:SpringBootAdmin
    LeetCode:简化路径【71】
    LeetCode:组合总数II【40】
    LeetCode:全排列II【47】
    LeetCode:全排列【46】
  • 原文地址:https://www.cnblogs.com/autumndawn/p/3419998.html
Copyright © 2011-2022 走看看