var qurckSort = function(array){ function qsort(array, begin, end){ if(end>begin){ //找到枢纽 var index = partition(array, begin, end); qsort(array, begin, index-1); qsort(array, index+1, end); } } //定义三个内部函数 function partition(array, begin, end){ //随意定位枢纽的位置 var index=begin+Math.floor(Math.random()*(end-begin)); //获取枢纽的值 var pivot=array[index]; //获取枢纽后, 将其置放到数组的最后一个位置 swap(array,index,end); //之后循环数组, 与枢纽进行比较, 相当于重新排数组 for(var i=index=begin;i<end;i++){ //如果当前元素小于枢纽,则交换位置 if(array[i]<pivot){ swap(array, index++, i); } } swap(array, index, end); return index; } function swap(array,a,b){ var tmp = array[a] array[a] = array[b] array[b] = tmp ; } qsort(array, 0, array.length-1); }
24,8,1,44,13,34,11,64,23,98,43,25 25,8,1,44,13,34,11,64,23,98,43,24 v = 24, 8,25,1,44,13,34,11,64,23,98,43,24 0 8,1,25,44,13,34,11,64,23,98,43,24 1 8,1,13,44,25,34,11,64,23,98,43,24 2 8,1,13,11,25,34,44,64,23,98,43,24 3 8,1,13,11,23,34,44,64,25,98,43,24 4 8,1,13,11,23,24,44,64,25,98,42,34 5 [8,1,13,11,23],24,[64,25,98,42,34]//枢纽分别为8,64 [23,1,13,11,8],24,[34,25,98,42,64] 调换 [23,1,13,11,8],24,[34,25,98,42,64] i = 0,v = 0 [1,23,13,11,8],24,[25,34,98,42,64] i = 1,v = 0 [1,23,13,11,8],24,[25,34,42,98,64] i = 2,v= 1 [1,8,13,11,23],24,[25,34,42,64,98] [1],8,[13,11,23],24,[25,34,42],64,98 [1],8,[23,11,13] [1],8,[11,23,13] [1],8,[11],[13],[23]
一些有用的链接
http://www.cnblogs.com/isun/archive/2009/04/25/1443603.html