From 0e4fa45e8e34511d8a9b8c4dea503d2cbe6f4e88 Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Tue, 13 May 2025 09:13:12 +0800 Subject: [PATCH 1/2] Improvement Signed-off-by: begeekmyfriend --- 0031_next_permutation/next_permutation.c | 8 ++++---- 0031_next_permutation/next_permutation.cc | 5 +++++ 0912_sort_an_array/sort.c | 12 ++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/0031_next_permutation/next_permutation.c b/0031_next_permutation/next_permutation.c index 49ddbe5..8cd3024 100644 --- a/0031_next_permutation/next_permutation.c +++ b/0031_next_permutation/next_permutation.c @@ -21,15 +21,13 @@ static void reverse(int *a, int size) static void nextPermutation(int* nums, int numsSize) { - if (numsSize <= 1) { - return; - } - + // find the first smaller element in decreasing sequence from back to forth. int i = numsSize - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } + // if found, find the first bigger element from back to forth and swap them. if (i >= 0) { int j = numsSize - 1; while (j >= 0 && nums[j] <= nums[i]) { @@ -37,6 +35,8 @@ static void nextPermutation(int* nums, int numsSize) } swap(nums + i, nums + j); } + + // reverse the subsequence into increasing one. reverse(nums + i + 1, numsSize - i - 1); } diff --git a/0031_next_permutation/next_permutation.cc b/0031_next_permutation/next_permutation.cc index 5b8256c..f12077d 100644 --- a/0031_next_permutation/next_permutation.cc +++ b/0031_next_permutation/next_permutation.cc @@ -5,11 +5,15 @@ using namespace std; class Solution { public: void nextPermutation(vector& nums) { + // find the first smaller element in decreasing sequence from back to + // forth. int i = nums.size() - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } + // if found, find the first bigger element from back to forth and swap + // them. if (i >= 0) { int j = nums.size() - 1; while (j >= 0 && nums[i] >= nums[j]) { @@ -18,6 +22,7 @@ class Solution { swap(nums[i], nums[j]); } + // reverse the subsequence into increasing one. reverse(nums.begin() + i + 1, nums.end()); } }; diff --git a/0912_sort_an_array/sort.c b/0912_sort_an_array/sort.c index 107783a..304ffaf 100644 --- a/0912_sort_an_array/sort.c +++ b/0912_sort_an_array/sort.c @@ -26,7 +26,7 @@ static void quick_sort(int *nums, int lo, int hi) return; } - /* shuffle the pivot */ + /* shuffle the pivot as it is a must for performance */ mid = lo + (hi - lo) / 2; swap(&nums[mid], &nums[hi]); @@ -38,11 +38,15 @@ static void quick_sort(int *nums, int lo, int hi) * shall make the partition in the middle of the array as far as * possible. If the partition is located in the head or tail, the * performance might well be very bad for it. + * + * Note: Do NOT use nums[++i] <= pivot or nums[--j] >= pivot as the + * loop condition because it leads to redundant operations in each + * recusive iteration when there are many duplicate elements. */ - while (i < hi && nums[++i] < pivot) {} - while (j > lo && nums[--j] > pivot) {} + while (i < j && nums[++i] < pivot) {} + while (i < j && nums[--j] > pivot) {} if (i < j) { - swap(&nums[i], &nums[j]); + swap(&nums[i], &nums[j]); } } From 02b17696de11195db74e6c1fde809185b58e8240 Mon Sep 17 00:00:00 2001 From: begeekmyfriend Date: Tue, 13 May 2025 09:33:18 +0800 Subject: [PATCH 2/2] Improvement Signed-off-by: begeekmyfriend --- 0215_kth_largest_element_in_an_array/kth_elem.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/0215_kth_largest_element_in_an_array/kth_elem.c b/0215_kth_largest_element_in_an_array/kth_elem.c index b14d278..dd049b5 100644 --- a/0215_kth_largest_element_in_an_array/kth_elem.c +++ b/0215_kth_largest_element_in_an_array/kth_elem.c @@ -62,6 +62,10 @@ static void quick_select(int *nums, int lo, int hi, int k) * shall make the partition in the middle of the array as far as * possible. If the partition is located in the head or tail, the * performance might well be very bad for it. + * + * Note: Do NOT use nums[++i] <= pivot or nums[--j] >= pivot as the + * loop condition because it leads to redundant operations in each + * recusive iteration when there are many duplicate elements. */ while (i < hi && nums[++i] > pivot) {} while (j > lo && nums[--j] < pivot) {} @@ -72,7 +76,8 @@ static void quick_select(int *nums, int lo, int hi, int k) /* invariant: i == j + 1 or i == j */ swap(&nums[i], &nums[hi]); - if (i + 1 >= k) { + /* compare index [i] with [k - 1] to locate the kth element */ + if (i > k - 1) { quick_select(nums, lo, i - 1, k); } else { quick_select(nums, i + 1, hi, k);