mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
Add the initial EN translation for C++ code (#1346)
This commit is contained in:
6
en/codes/cpp/chapter_sorting/CMakeLists.txt
Normal file
6
en/codes/cpp/chapter_sorting/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
add_executable(selection_sort selection_sort.cpp)
|
||||
add_executable(bubble_sort bubble_sort.cpp)
|
||||
add_executable(insertion_sort insertion_sort.cpp)
|
||||
add_executable(merge_sort merge_sort.cpp)
|
||||
add_executable(quick_sort quick_sort.cpp)
|
||||
add_executable(heap_sort heap_sort.cpp)
|
||||
56
en/codes/cpp/chapter_sorting/bubble_sort.cpp
Normal file
56
en/codes/cpp/chapter_sorting/bubble_sort.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: bubble_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Bubble sort */
|
||||
void bubbleSort(vector<int> &nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
// Here, the std
|
||||
swap(nums[j], nums[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (optimized with flag)*/
|
||||
void bubbleSortWithFlag(vector<int> &nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
bool flag = false; // Initialize flag
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
// Here, the std
|
||||
swap(nums[j], nums[j + 1]);
|
||||
flag = true; // Record swapped elements
|
||||
}
|
||||
}
|
||||
if (!flag)
|
||||
break; // If no elements were swapped in this round of "bubbling", exit
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
bubbleSort(nums);
|
||||
cout << "After bubble sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
vector<int> nums1 = {4, 1, 3, 1, 5, 2};
|
||||
bubbleSortWithFlag(nums1);
|
||||
cout << "After bubble sort, nums1 = ";
|
||||
printVector(nums1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
44
en/codes/cpp/chapter_sorting/bucket_sort.cpp
Normal file
44
en/codes/cpp/chapter_sorting/bucket_sort.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: bucket_sort.cpp
|
||||
* Created Time: 2023-03-30
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Bucket sort */
|
||||
void bucketSort(vector<float> &nums) {
|
||||
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
int k = nums.size() / 2;
|
||||
vector<vector<float>> buckets(k);
|
||||
// 1. Distribute array elements into various buckets
|
||||
for (float num : nums) {
|
||||
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
|
||||
int i = num * k;
|
||||
// Add number to bucket_idx
|
||||
buckets[i].push_back(num);
|
||||
}
|
||||
// 2. Sort each bucket
|
||||
for (vector<float> &bucket : buckets) {
|
||||
// Use built-in sorting function, can also replace with other sorting algorithms
|
||||
sort(bucket.begin(), bucket.end());
|
||||
}
|
||||
// 3. Traverse buckets to merge results
|
||||
int i = 0;
|
||||
for (vector<float> &bucket : buckets) {
|
||||
for (float num : bucket) {
|
||||
nums[i++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// Assume input data is floating point, range [0, 1)
|
||||
vector<float> nums = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f};
|
||||
bucketSort(nums);
|
||||
cout << "After bucket sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
77
en/codes/cpp/chapter_sorting/counting_sort.cpp
Normal file
77
en/codes/cpp/chapter_sorting/counting_sort.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* File: counting_sort.cpp
|
||||
* Created Time: 2023-03-17
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Counting sort */
|
||||
// Simple implementation, cannot be used for sorting objects
|
||||
void countingSortNaive(vector<int> &nums) {
|
||||
// 1. Count the maximum element m in the array
|
||||
int m = 0;
|
||||
for (int num : nums) {
|
||||
m = max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// counter[num] represents the occurrence of num
|
||||
vector<int> counter(m + 1, 0);
|
||||
for (int num : nums) {
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. Traverse counter, filling each element back into the original array nums
|
||||
int i = 0;
|
||||
for (int num = 0; num < m + 1; num++) {
|
||||
for (int j = 0; j < counter[num]; j++, i++) {
|
||||
nums[i] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Counting sort */
|
||||
// Complete implementation, can sort objects and is a stable sort
|
||||
void countingSort(vector<int> &nums) {
|
||||
// 1. Count the maximum element m in the array
|
||||
int m = 0;
|
||||
for (int num : nums) {
|
||||
m = max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// counter[num] represents the occurrence of num
|
||||
vector<int> counter(m + 1, 0);
|
||||
for (int num : nums) {
|
||||
counter[num]++;
|
||||
}
|
||||
// 3. Calculate the prefix sum of counter, converting "occurrence count" to "tail index"
|
||||
// counter[num]-1 is the last index where num appears in res
|
||||
for (int i = 0; i < m; i++) {
|
||||
counter[i + 1] += counter[i];
|
||||
}
|
||||
// 4. Traverse nums in reverse order, placing each element into the result array res
|
||||
// Initialize the array res to record results
|
||||
int n = nums.size();
|
||||
vector<int> res(n);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
int num = nums[i];
|
||||
res[counter[num] - 1] = num; // Place num at the corresponding index
|
||||
counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num
|
||||
}
|
||||
// Use result array res to overwrite the original array nums
|
||||
nums = res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
||||
countingSortNaive(nums);
|
||||
cout << "After count sort (unable to sort objects), nums = ";
|
||||
printVector(nums);
|
||||
|
||||
vector<int> nums1 = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
||||
countingSort(nums1);
|
||||
cout << "After count sort, nums1 = ";
|
||||
printVector(nums1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
54
en/codes/cpp/chapter_sorting/heap_sort.cpp
Normal file
54
en/codes/cpp/chapter_sorting/heap_sort.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: heap_sort.cpp
|
||||
* Created Time: 2023-05-26
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
void siftDown(vector<int> &nums, int n, int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
int l = 2 * i + 1;
|
||||
int r = 2 * i + 2;
|
||||
int ma = i;
|
||||
if (l < n && nums[l] > nums[ma])
|
||||
ma = l;
|
||||
if (r < n && nums[r] > nums[ma])
|
||||
ma = r;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
if (ma == i) {
|
||||
break;
|
||||
}
|
||||
// Swap two nodes
|
||||
swap(nums[i], nums[ma]);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Heap sort */
|
||||
void heapSort(vector<int> &nums) {
|
||||
// Build heap operation: heapify all nodes except leaves
|
||||
for (int i = nums.size() / 2 - 1; i >= 0; --i) {
|
||||
siftDown(nums, nums.size(), i);
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for (int i = nums.size() - 1; i > 0; --i) {
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
swap(nums[0], nums[i]);
|
||||
// Start heapifying the root node, from top to bottom
|
||||
siftDown(nums, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
heapSort(nums);
|
||||
cout << "After heap sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
31
en/codes/cpp/chapter_sorting/insertion_sort.cpp
Normal file
31
en/codes/cpp/chapter_sorting/insertion_sort.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* File: insertion_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Insertion sort */
|
||||
void insertionSort(vector<int> &nums) {
|
||||
// Outer loop: sorted range is [0, i-1]
|
||||
for (int i = 1; i < nums.size(); i++) {
|
||||
int base = nums[i], j = i - 1;
|
||||
// Inner loop: insert base into the correct position within the sorted range [0, i-1]
|
||||
while (j >= 0 && nums[j] > base) {
|
||||
nums[j + 1] = nums[j]; // Move nums[j] to the right by one position
|
||||
j--;
|
||||
}
|
||||
nums[j + 1] = base; // Assign base to the correct position
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
insertionSort(nums);
|
||||
cout << "After insertion sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
58
en/codes/cpp/chapter_sorting/merge_sort.cpp
Normal file
58
en/codes/cpp/chapter_sorting/merge_sort.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* File: merge_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Merge left subarray and right subarray */
|
||||
void merge(vector<int> &nums, int left, int mid, int right) {
|
||||
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
|
||||
// Create a temporary array tmp to store the merged results
|
||||
vector<int> tmp(right - left + 1);
|
||||
// Initialize the start indices of the left and right subarrays
|
||||
int i = left, j = mid + 1, k = 0;
|
||||
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
|
||||
while (i <= mid && j <= right) {
|
||||
if (nums[i] <= nums[j])
|
||||
tmp[k++] = nums[i++];
|
||||
else
|
||||
tmp[k++] = nums[j++];
|
||||
}
|
||||
// Copy the remaining elements of the left and right subarrays into the temporary array
|
||||
while (i <= mid) {
|
||||
tmp[k++] = nums[i++];
|
||||
}
|
||||
while (j <= right) {
|
||||
tmp[k++] = nums[j++];
|
||||
}
|
||||
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
|
||||
for (k = 0; k < tmp.size(); k++) {
|
||||
nums[left + k] = tmp[k];
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge sort */
|
||||
void mergeSort(vector<int> &nums, int left, int right) {
|
||||
// Termination condition
|
||||
if (left >= right)
|
||||
return; // Terminate recursion when subarray length is 1
|
||||
// Partition stage
|
||||
int mid = (left + right) / 2; // Calculate midpoint
|
||||
mergeSort(nums, left, mid); // Recursively process the left subarray
|
||||
mergeSort(nums, mid + 1, right); // Recursively process the right subarray
|
||||
// Merge stage
|
||||
merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Merge sort */
|
||||
vector<int> nums = {7, 3, 2, 6, 0, 1, 5, 4};
|
||||
mergeSort(nums, 0, nums.size() - 1);
|
||||
cout << "After merge sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
166
en/codes/cpp/chapter_sorting/quick_sort.cpp
Normal file
166
en/codes/cpp/chapter_sorting/quick_sort.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* File: quick_sort.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Quick sort class */
|
||||
class QuickSort {
|
||||
private:
|
||||
/* Swap elements */
|
||||
static void swap(vector<int> &nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
static int partition(vector<int> &nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
public:
|
||||
/* Quick sort */
|
||||
static void quickSort(vector<int> &nums, int left, int right) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Partition
|
||||
int pivot = partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
}
|
||||
};
|
||||
|
||||
/* Quick sort class (median pivot optimization) */
|
||||
class QuickSortMedian {
|
||||
private:
|
||||
/* Swap elements */
|
||||
static void swap(vector<int> &nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Select the median of three candidate elements */
|
||||
static int medianThree(vector<int> &nums, int left, int mid, int right) {
|
||||
int l = nums[left], m = nums[mid], r = nums[right];
|
||||
if ((l <= m && m <= r) || (r <= m && m <= l))
|
||||
return mid; // m is between l and r
|
||||
if ((m <= l && l <= r) || (r <= l && l <= m))
|
||||
return left; // l is between m and r
|
||||
return right;
|
||||
}
|
||||
|
||||
/* Partition (median of three) */
|
||||
static int partition(vector<int> &nums, int left, int right) {
|
||||
// Select the median of three candidate elements
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
// Swap the median to the array's leftmost position
|
||||
swap(nums, left, med);
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
public:
|
||||
/* Quick sort */
|
||||
static void quickSort(vector<int> &nums, int left, int right) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right)
|
||||
return;
|
||||
// Partition
|
||||
int pivot = partition(nums, left, right);
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1);
|
||||
quickSort(nums, pivot + 1, right);
|
||||
}
|
||||
};
|
||||
|
||||
/* Quick sort class (tail recursion optimization) */
|
||||
class QuickSortTailCall {
|
||||
private:
|
||||
/* Swap elements */
|
||||
static void swap(vector<int> &nums, int i, int j) {
|
||||
int tmp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
static int partition(vector<int> &nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
while (i < j) {
|
||||
while (i < j && nums[j] >= nums[left])
|
||||
j--; // Search from right to left for the first element smaller than the pivot
|
||||
while (i < j && nums[i] <= nums[left])
|
||||
i++; // Search from left to right for the first element greater than the pivot
|
||||
swap(nums, i, j); // Swap these two elements
|
||||
}
|
||||
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
public:
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
static void quickSort(vector<int> &nums, int left, int right) {
|
||||
// Terminate when subarray length is 1
|
||||
while (left < right) {
|
||||
// Partition operation
|
||||
int pivot = partition(nums, left, right);
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if (pivot - left < right - pivot) {
|
||||
quickSort(nums, left, pivot - 1); // Recursively sort the left subarray
|
||||
left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, right); // Recursively sort the right subarray
|
||||
right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Quick sort */
|
||||
vector<int> nums{2, 4, 1, 0, 3, 5};
|
||||
QuickSort::quickSort(nums, 0, nums.size() - 1);
|
||||
cout << "After quick sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* Quick sort (median pivot optimization) */
|
||||
vector<int> nums1 = {2, 4, 1, 0, 3, 5};
|
||||
QuickSortMedian::quickSort(nums1, 0, nums1.size() - 1);
|
||||
cout << "Quick sort (median pivot optimization) completed, nums = ";
|
||||
printVector(nums1);
|
||||
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
vector<int> nums2 = {2, 4, 1, 0, 3, 5};
|
||||
QuickSortTailCall::quickSort(nums2, 0, nums2.size() - 1);
|
||||
cout << "Quick sort (tail recursion optimization) completed, nums = ";
|
||||
printVector(nums2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
65
en/codes/cpp/chapter_sorting/radix_sort.cpp
Normal file
65
en/codes/cpp/chapter_sorting/radix_sort.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* File: radix_sort.cpp
|
||||
* Created Time: 2023-03-26
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Get the k-th digit of element num, where exp = 10^(k-1) */
|
||||
int digit(int num, int exp) {
|
||||
// Passing exp instead of k can avoid repeated expensive exponentiation here
|
||||
return (num / exp) % 10;
|
||||
}
|
||||
|
||||
/* Counting sort (based on nums k-th digit) */
|
||||
void countingSortDigit(vector<int> &nums, int exp) {
|
||||
// Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
vector<int> counter(10, 0);
|
||||
int n = nums.size();
|
||||
// Count the occurrence of digits 0~9
|
||||
for (int i = 0; i < n; i++) {
|
||||
int d = digit(nums[i], exp); // Get the k-th digit of nums[i], noted as d
|
||||
counter[d]++; // Count the occurrence of digit d
|
||||
}
|
||||
// Calculate prefix sum, converting "occurrence count" into "array index"
|
||||
for (int i = 1; i < 10; i++) {
|
||||
counter[i] += counter[i - 1];
|
||||
}
|
||||
// Traverse in reverse, based on bucket statistics, place each element into res
|
||||
vector<int> res(n, 0);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
int d = digit(nums[i], exp);
|
||||
int j = counter[d] - 1; // Get the index j for d in the array
|
||||
res[j] = nums[i]; // Place the current element at index j
|
||||
counter[d]--; // Decrease the count of d by 1
|
||||
}
|
||||
// Use result to overwrite the original array nums
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = res[i];
|
||||
}
|
||||
|
||||
/* Radix sort */
|
||||
void radixSort(vector<int> &nums) {
|
||||
// Get the maximum element of the array, used to determine the maximum number of digits
|
||||
int m = *max_element(nums.begin(), nums.end());
|
||||
// Traverse from the lowest to the highest digit
|
||||
for (int exp = 1; exp <= m; exp *= 10)
|
||||
// Perform counting sort on the k-th digit of array elements
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// i.e., exp = 10^(k-1)
|
||||
countingSortDigit(nums, exp);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// Radix sort
|
||||
vector<int> nums = {10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996};
|
||||
radixSort(nums);
|
||||
cout << "After radix sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
34
en/codes/cpp/chapter_sorting/selection_sort.cpp
Normal file
34
en/codes/cpp/chapter_sorting/selection_sort.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* File: selection_sort.cpp
|
||||
* Created Time: 2023-05-23
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Selection sort */
|
||||
void selectionSort(vector<int> &nums) {
|
||||
int n = nums.size();
|
||||
// Outer loop: unsorted range is [i, n-1]
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// Inner loop: find the smallest element within the unsorted range
|
||||
int k = i;
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (nums[j] < nums[k])
|
||||
k = j; // Record the index of the smallest element
|
||||
}
|
||||
// Swap the smallest element with the first element of the unsorted range
|
||||
swap(nums[i], nums[k]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
selectionSort(nums);
|
||||
|
||||
cout << "After selection sort, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user