Skip to content

Instantly share code, notes, and snippets.

@DevinXian
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save DevinXian/a9e9d4d054bad33a93ea to your computer and use it in GitHub Desktop.

Select an option

Save DevinXian/a9e9d4d054bad33a93ea to your computer and use it in GitHub Desktop.

Revisions

  1. DevinXian revised this gist Jul 31, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion QuickSort
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * 快速排序--降序
    * 思路:每次选定一基准元素,将数组划分成大于和小于基准元素的两部分,再分别对子序列进行排序,直到子序列长度为1
    * 效率:O(nlog2 n)
    * 效率:O(nlog n)
    */
    function quick_sort(arr, start, end) {
    if (!arr instanceof Array) {
  2. DevinXian revised this gist Jul 30, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions QuickSort
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    /**
    * 快速排序--降序
    * 思路:每次选定一基准元素,将数组划分成大于和小于基准元素的两部分,再分别对子序列进行排序,直到子序列长度为1
    * 效率:O(nlog2 n)
    */
    function quick_sort(arr, start, end) {
  3. DevinXian revised this gist Jul 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion QuickSort
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ function quick_sort(arr, start, end) {
    while (!(arr[start] >= arr[i] || i == end)); //arr[i]比基准元素大,则++i
    do --j;
    while (!(arr[start] <= arr[j] || j == start)); //arr[j]比基准元素小,则--j
    if (i < j) { //arr[i]>基准元素,arr[j]<基准元素
    if (i < j) { //arr[i]<基准元素,arr[j]>基准元素
    swap(arr, i, j);
    } else {
    break; //break while(true)
  4. DevinXian revised this gist Jul 30, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion QuickSort
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    /**
    * 快速排序--降序
    * 效率:O(nlog2 n)
    * @Returns 排序结果
    */
    function quick_sort(arr, start, end) {
    if (!arr instanceof Array) {
  5. DevinXian revised this gist Jul 30, 2014. 2 changed files with 38 additions and 6 deletions.
    37 changes: 31 additions & 6 deletions QuickSort
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,37 @@
    /**
    * 快速排序
    * 快速排序--降序
    * 效率:O(nlog2 n)
    * @Returns 排序结果
    */
    function quick_sort(arr, start, end){
    if(!arr instanceof Array || start < 0 || end >= arr.length){
    console.log('invalid arguments');
    function quick_sort(arr, start, end) {
    if (!arr instanceof Array) {
    console.log('invalid array');
    return;
    }
    //TODO
    }
    var i = start, j = end + 1;
    if (start < end) { //边界条件
    while (true) { //第一次划分基准元素选择arr[start]
    do ++i;
    while (!(arr[start] >= arr[i] || i == end)); //arr[i]比基准元素大,则++i
    do --j;
    while (!(arr[start] <= arr[j] || j == start)); //arr[j]比基准元素小,则--j
    if (i < j) { //arr[i]>基准元素,arr[j]<基准元素
    swap(arr, i, j);
    } else {
    break; //break while(true)
    }
    }
    swap(arr, start, j); //交换基准元素和arr[j],完成一次划分
    console.log('完成一次划分:' + arr);
    quick_sort(arr, start, j - 1);
    quick_sort(arr, j + 1, end);
    }
    }

    function swap(arr, i, j) {
    console.log('exchange: ' + arr[i] + ' <----> ' + arr[j]);
    var tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    }

    7 changes: 7 additions & 0 deletions Run QuickSort
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Example:
    var arr = [2, 10, 1, 3, 5, 100, 8, 100000];
    quick_sort(arr, 0, arr.length - 2);
    console.log(arr);

    Result:
    [ 100, 10, 8, 5, 3, 2, 1, 100000 ]
  6. DevinXian created this gist Jul 30, 2014.
    12 changes: 12 additions & 0 deletions QuickSort
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /**
    * 快速排序
    * 效率:O(nlog2 n)
    * @Returns 排序结果
    */
    function quick_sort(arr, start, end){
    if(!arr instanceof Array || start < 0 || end >= arr.length){
    console.log('invalid arguments');
    return;
    }
    //TODO
    }