GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — programming/problems/sorting/QuickSort.cpp at master · rp-code9/programming Expand file tree/ Copy pathQuickSort.cppMore file actions 73 lines (63 loc) · 1.55 KB / Copy pathQuickSort.cppFile metadata and controls73 lines (63 loc) · 1.55 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // Techie Delight https://www.techiedelight.com/quicksort/ #include <iostream> #include <vector> #include <cstdlib> #include <algorithm> #include <ctime> void print(const std::vector<int>& nums) { for (auto n : nums) { std::cout << n << " "; } } std::vector<int> generateRandomNumbers(const int length, const int min, const int max) { std::srand(std::time(nullptr)); std::vector<int> nums; for (int i = 0; i < length; ++i) { int x = min + (rand() % max - min + 1); nums.push_back(x); } return nums; } int partition(std::vector<int>& nums, int low, int high) { int pivot = nums[high]; int index = low; for (int i = low; i <= high; ++i) { if (nums[i] < pivot) { std::swap(nums[index++], nums[i]); } } std::swap(nums[index], nums[high]); return index; } void quicksort(std::vector<int>& nums, int low, int high) { if (low >= high) { return; } int pivot = partition(nums, low, high); quicksort(nums, low, pivot - 1); quicksort(nums, pivot + 1, high); } void quicksort(std::vector<int>& nums) { quicksort(nums, 0, nums.size() - 1); } void test(std::vector<int>& nums) { std::cout << "Numbers before sorting: "; print(nums); quicksort(nums); std::cout << "\nNumbers after sorting: "; print(nums); std::cout << "\nIs sorted: " << std::boolalpha << std::is_sorted(nums.begin(), nums.end()); std::cout << "\n"; } int main() { std::vector<int> nums = generateRandomNumbers(20, -1000, 1000); test(nums); return 0; } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |