Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

[pull] master from begeekmyfriend:master #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions 0031_next_permutation/next_permutation.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ 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]) {
j--;
}
swap(nums + i, nums + j);
}

// reverse the subsequence into increasing one.
reverse(nums + i + 1, numsSize - i - 1);
}

Expand Down
5 changes: 5 additions & 0 deletions 0031_next_permutation/next_permutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ using namespace std;
class Solution {
public:
void nextPermutation(vector<int>& 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]) {
Expand All @@ -18,6 +22,7 @@ class Solution {
swap(nums[i], nums[j]);
}

// reverse the subsequence into increasing one.
reverse(nums.begin() + i + 1, nums.end());
}
};
7 changes: 6 additions & 1 deletion 0215_kth_largest_element_in_an_array/kth_elem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions 0912_sort_an_array/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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]);
}
}

Expand Down