Skip to content

Commit 0e4fa45

Browse files
Improvement
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 15d6c5f commit 0e4fa45

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

0031_next_permutation/next_permutation.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ static void reverse(int *a, int size)
2121

2222
static void nextPermutation(int* nums, int numsSize)
2323
{
24-
if (numsSize <= 1) {
25-
return;
26-
}
27-
24+
// find the first smaller element in decreasing sequence from back to forth.
2825
int i = numsSize - 2;
2926
while (i >= 0 && nums[i] >= nums[i + 1]) {
3027
i--;
3128
}
3229

30+
// if found, find the first bigger element from back to forth and swap them.
3331
if (i >= 0) {
3432
int j = numsSize - 1;
3533
while (j >= 0 && nums[j] <= nums[i]) {
3634
j--;
3735
}
3836
swap(nums + i, nums + j);
3937
}
38+
39+
// reverse the subsequence into increasing one.
4040
reverse(nums + i + 1, numsSize - i - 1);
4141
}
4242

0031_next_permutation/next_permutation.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ using namespace std;
55
class Solution {
66
public:
77
void nextPermutation(vector<int>& nums) {
8+
// find the first smaller element in decreasing sequence from back to
9+
// forth.
810
int i = nums.size() - 2;
911
while (i >= 0 && nums[i] >= nums[i + 1]) {
1012
i--;
1113
}
1214

15+
// if found, find the first bigger element from back to forth and swap
16+
// them.
1317
if (i >= 0) {
1418
int j = nums.size() - 1;
1519
while (j >= 0 && nums[i] >= nums[j]) {
@@ -18,6 +22,7 @@ class Solution {
1822
swap(nums[i], nums[j]);
1923
}
2024

25+
// reverse the subsequence into increasing one.
2126
reverse(nums.begin() + i + 1, nums.end());
2227
}
2328
};

0912_sort_an_array/sort.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void quick_sort(int *nums, int lo, int hi)
2626
return;
2727
}
2828

29-
/* shuffle the pivot */
29+
/* shuffle the pivot as it is a must for performance */
3030
mid = lo + (hi - lo) / 2;
3131
swap(&nums[mid], &nums[hi]);
3232

@@ -38,11 +38,15 @@ static void quick_sort(int *nums, int lo, int hi)
3838
* shall make the partition in the middle of the array as far as
3939
* possible. If the partition is located in the head or tail, the
4040
* performance might well be very bad for it.
41+
*
42+
* Note: Do NOT use nums[++i] <= pivot or nums[--j] >= pivot as the
43+
* loop condition because it leads to redundant operations in each
44+
* recusive iteration when there are many duplicate elements.
4145
*/
42-
while (i < hi && nums[++i] < pivot) {}
43-
while (j > lo && nums[--j] > pivot) {}
46+
while (i < j && nums[++i] < pivot) {}
47+
while (i < j && nums[--j] > pivot) {}
4448
if (i < j) {
45-
swap(&nums[i], &nums[j]);
49+
swap(&nums[i], &nums[j]);
4650
}
4751
}
4852

0 commit comments

Comments
 (0)