Skip to content

Commit

Permalink
Update 6.quickSort.md (hustcc#6)
Browse files Browse the repository at this point in the history
* Update 6.quickSort.md

update 基于严蔚敏版的js快排, C++版.优化掉swap的操作

* Update 6.quickSort.md

* update lagyout

唔,其实我想表达的重点是优化掉swap这个交换,这里(swap)会增加同排序数量级(O(nlgn))的比较次数和交换次数操作,有点浪费:)

* Update 6.quickSort.md
  • Loading branch information
wagnlinzh authored and hustcc committed Feb 8, 2017
1 parent 523336f commit 2939241
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 6.quickSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ function swap(arr, i, j) {
arr[i] = arr[j];
arr[j] = temp;
}
functiion paritition2(arr, low, high) {
let pivot = arr[low];
while (low < high) {
while (low < high && arr[high] > pivot) {
--high;
}
arr[low] = arr[high];
while (low < high && arr[low] <= pivot) {
++low;
}
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}

function quickSort2(arr, low, high) {
if (low < high) {
let pivot = paritition2(arr, low, high);
quickSort2(arr, low, pivot - 1);
quickSort2(arr, pivot + 1, high);
}
return arr;
}

```


Expand Down Expand Up @@ -127,3 +152,34 @@ func swap(arr []int, i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}
```

## 6. C++版


```C++
//严蔚敏《数据结构》标准分割函数
Paritition1(int A[], int low, int high) {
int pivot = A[low];
while (low < high) {
while (low < high && A[high] >= pivot) {
--high;
}
A[low] = A[high];
while (low < high && A[low] <= pivot) {
++low;
}
A[high] = A[low];
}
A[low] = pivot;
return low;
}

void QuickSort(int A[], int low, int high) //快排母函数
{
if (low < high) {
int pivot = Paritition1(A, low, high);
QuickSort(A, low, pivot - 1);
QuickSort(A, pivot + 1, high);
}
}
```

0 comments on commit 2939241

Please sign in to comment.