Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post almost 2 years ago by yjj‭.

17 / 255
  • K-way parittion quick sort in c++
  • K-way partition quick sort in C++
  • Question: How to write a k-way partition QuickSort that satisfies the test cases shown below?
  • First thing first: I don't know this algorithm and there seems to be no information available about it on the internet (i am familiar with QuickSort and k-way partition dough)
  • Is it just the recursive dual-partition QuickSort with insertion sort when length is less than or equal to 2*k?
  • i found this code:
  • https://github.com/KGene1901/Algorithms-Data-Structures-Yr1/blob/91dc231df2a27a2e306a7599afdb0be38b3935a4/k-way%20QuickSort.py
  • and tried to translate it to c++ but there seems to be a bug with my current code:
  • the resulting array in the main.cpp is shorter than the original array more specifically it zeros at the end.
  • i think it due to the way it's handled in the python code is mixing integer and lists together in the partition array and i tried to convert that by checking the vector sizes.
  • main.cpp:
  • vector<vector<int>> partition(vector<int> A, int k,int* q){
  • vector<int> pivots;
  • std::vector<std::vector<int>>sortedA;
  • int count=0;
  • int P1=1;
  • int P2=2;
  • if (k<=0){
  • return {};
  • }
  • else{
  • for (int i=A.size()-1; i>A.size()-k-1; i--){
  • pivots.push_back(A[i]);
  • A.erase(A.begin()+i);
  • sort(pivots.begin(), pivots.end());
  • }
  • for (int m: pivots){
  • sortedA.push_back({m});
  • }
  • sortedA.insert(sortedA.begin(), {});
  • sortedA.push_back({});
  • while (sortedA[P1].size() && sortedA[P2].size()){
  • sortedA.insert(sortedA.begin()+P2, {{}});
  • P1+=2;
  • P2+=2;
  • }
  • for (int ele: A){
  • count=0;
  • if (ele<pivots[0]){
  • sortedA[0].push_back({ele});
  • }
  • else if (ele>pivots[pivots.size()-1]){
  • sortedA[sortedA.size()-1].push_back({ele});
  • }
  • else{
  • while (ele>pivots[count]){
  • count+=1;
  • }
  • int index = find(sortedA.begin(), sortedA.end(), vector<int>{pivots[count-1]}) - sortedA.begin();
  • sortedA[index+1].push_back(ele);
  • }
  • }
  • }
  • return sortedA;
  • }
  • vector<int> quicksort(vector<int> A, int k, int* q){
  • if (k<=0){
  • return {};
  • }
  • if (A.size()==1){
  • return A;
  • }
  • else if (A.size()<=(2*k)){
  • sort(A.begin(), A.end());
  • return A;
  • }
  • else{
  • vector<vector<int>> sorted = partition(A,k,q);
  • for (auto ele: sorted){
  • if (ele.size()<2){
  • continue;
  • }
  • else{
  • int index = find(sorted.begin(), sorted.end(), ele) - sorted.begin();
  • sorted[index] = quicksort(ele,k,q);
  • }
  • }
  • vector<int> SortedAFinalMatch;
  • for (auto s: sorted){
  • if (s.size()==1){
  • SortedAFinalMatch.push_back(s[0]);
  • }
  • else{
  • for (auto item: s){
  • SortedAFinalMatch.push_back(item);
  • }
  • }
  • }
  • return SortedAFinalMatch;
  • }
  • }
  • int main(){
  • int n = 30;
  • int k = 13;
  • int* q = new int[2*k];
  • int* pivots = new int[k];
  • std::vector<int>a;
  • for (int i = 0; i < 2 * k; i++) pivots[i] = 4 * i + 2;
  • srand(time(NULL));
  • cout<<endl;
  • for (int i = 0; i < n; i++) { a.push_back(rand()% (4 * k)); }
  • for (auto i:a)std::cout << i << " ";
  • cout<<endl;
  • std::vector<int>b = quicksort(a,k,q);
  • for (auto i:b)std::cout << i << " ";
  • cout <<endl;
  • int i = 0;
  • for (int j = 0; j < k; j++) {
  • while (a[i] < pivots[j]){i++;cout<<"*"<<pivots[i];};
  • assert (q[2 * j] == i);
  • cout<<";"<<i<<";";
  • while (a[i] == pivots[j]){i++;cout<<"*"<<pivots[i];};
  • assert (q[2 * j + 1] == i);
  • }
  • }
  • using namespace std;
  • #include <vector>
  • #include <iostream>
  • #include <algorithm>
  • #include "I.h"
  • #pragma once
  • template <class T>
  • class KWayPartition {
  • public:
  • /* A[p .. r]
  • pivots[0 .. (k-1)] an Aay of k ordered values (from smaller to bigger)// k+1 parts with k pivots
  • q[0 .. (2k-1)] output Aay of borders
  • At the end:
  • A[p .. q[0]-1] < pivots[0]
  • A[q[0] .. q[1]-] = pivots[0]
  • pivots[0] < A[q[1] .. q[2]-1] < pivots[1]
  • A[q[2] .. q[3]-1] = pivots[1]
  • ...
  • pivots[i-1] < A[q[2i-1] .. q[2i]-1] < pivots[i] 0 < i < k-1
  • A[q[2i] .. q[2i+1]-1] = pivots[i] 0 < i < k-1
  • ...
  • pivots[k-2] < A[q[2k-3] .. q[2k-2]-1] < pivots[k-1]
  • A[q[2k-2] .. q[2k-1]-1] = pivots[k-1]
  • A[q[2k-1] .. r] > pivots[k-1]
  • */
  • void insertionSort(T* A, int r) {
  • T key;
  • int j=0;
  • for (int i=0;i<r;i++) {
  • key = A[i];
  • j = i - 1;
  • while (j >= 0 && A[j] > key) {
  • A[j+1] = A[j];
  • j--;
  • }
  • A[j+1] = key;
  • }
  • }
  • vector<vector<T>> partition(vector<T> A, int k){
  • vector<T> pivots;
  • std::vector<std::vector<T>>sortedA;
  • int count=0;
  • int P1=1;
  • int P2=2;
  • if (k<=0){
  • return {};
  • }
  • else{
  • for (int i=A.size()-1; i>A.size()-k-1; i--){
  • pivots.push_back(A[i]);
  • A.erase(A.begin()+i);
  • sort(pivots.begin(), pivots.end());
  • }
  • for (int m: pivots){
  • sortedA.push_back({m});
  • }
  • sortedA.insert(sortedA.begin(), {});
  • sortedA.push_back({});
  • while (sortedA[P1].size() && sortedA[P2].size()){
  • sortedA.insert(sortedA.begin()+P2, {});
  • P1+=2;
  • P2+=2;
  • }
  • for (int ele: A){
  • count=0;
  • if (ele<pivots[0]){
  • sortedA[0].push_back(ele);
  • }
  • else if (ele>pivots[pivots.size()-1]){
  • sortedA[sortedA.size()-1].push_back(ele);
  • }
  • else{
  • while (ele>pivots[count]){
  • count+=1;
  • }
  • int index = find(sortedA.begin(), sortedA.end(), vector<T>{pivots[count-1]}) - sortedA.begin();
  • sortedA[index+1].push_back(ele);
  • }
  • }
  • }
  • return sortedA;
  • }
  • vector<T> quicksort(vector<T> A, int k){
  • if (k<=0){
  • return {};
  • }
  • if (A.size()==1){
  • return A;
  • }
  • else if (A.size()<=(2*k)){
  • sort(A.begin(), A.end());
  • return A;
  • }
  • else{
  • vector<vector<T>> sorted = partition(A,k);
  • for (auto ele: sorted){
  • if (ele.size()<2){
  • continue;
  • }
  • else{
  • int index = find(sorted.begin(), sorted.end(), ele) - sorted.begin();
  • sorted[index] = quicksort(ele,k);
  • }
  • }
  • vector<T> SortedAFinalMatch;
  • for (auto s: sorted){
  • if (s.size()==1){
  • SortedAFinalMatch.push_back(s[0]);
  • }
  • else{
  • for (auto item: s){
  • SortedAFinalMatch.push_back(item);
  • }
  • }
  • }
  • return SortedAFinalMatch;
  • }
  • }
  • virtual void Partition (T* A, T* pivots, int* q, int p, int r, int k) {
  • std::vector<T>a;
  • for (int i = 0; i <= r; i++) { a.push_back(A[i]); }
  • std::vector<T>b = quicksort(a,3);
  • cout<<endl;
  • for (auto i:b)std::cout << i << " ";
  • /*
  • * for (int i = 0; i <= r; i++) {
  • * cout<< a[i]<<", ";
  • * }
  • */
  • /*
  • * cout<<endl;
  • * a=quicksort(a,k);
  • * for (int i = 0; i < r+1; i++) {
  • * cout<< a[i]<<", ";
  • * A[i] = a[i];
  • * }
  • */
  • }
  • };
  • How do i write a KWayPartition method such that it satisfies this testcase?
  • ```c++
  • #include <iostream>
  • #include <cassert>
  • #include <time.h>
  • #include "../src/KWayPartition.h"
  • #include "I.h"
  • using namespace std;
  • int main() {
  • int n = 30;
  • int k = 13;
  • I *A = new I[n];
  • I* pivots = new I[k];
  • int* q = new int[2*k];
  • KWayPartition<I>* fwp = new KWayPartition<I>();
  • srand(time(NULL));
  • for (int i = 0; i < 2 * k; i++)
  • pivots[i] = 4 * i + 2;
  • cout << "Initial Array:" << endl;
  • for (int i = 0; i < n; i++) {
  • A[i] = rand() % (4 * k);
  • cout << A[i] << ", ";
  • }
  • fwp->Partition(A, pivots, q, 0, n-1, k);
  • int i = 0;
  • for (int j = 0; j < k; j++) {
  • while (A[i] < pivots[j])
  • i++;
  • assert (q[2 * j] == i);
  • while (A[i] == pivots[j])
  • i++;
  • assert (q[2 * j + 1] == i);
  • }
  • cout << "\nFinal Array:" << endl;
  • for (int i = 0; i < n; i++) {
  • cout << A[i] << ", ";
  • }
  • cout << "q values:" << endl;
  • for (int i = 0; i < k; i++) {
  • cout << "Pivot " << pivots[i] << ", q< is:"<< q[2*i] << ", q= is:" << q[2*i+1] << endl;
  • }
  • int d;
  • cin >> d;
  • }
  • ```
  • What is this test cases about?
  • I don't know this algorithm and there seems to be no information available about it on the internet (I am familiar with quicksort and k-way partition though).
  • I'm not sure if it's just the recursive dual-partition quicksort with insertion sort when length <= 2*k
  • I found [an implementation](https://github.com/KGene1901/Algorithms-Data-Structures-Yr1/blob/91dc231df2a27a2e306a7599afdb0be38b3935a4/k-way%20QuickSort.py) and tried to translate it to C++ but there seems to be a bug with my current code.
  • - [main.cpp](https://paste.rs/qfI)
  • - [main3.cpp](https://paste.rs/pta)
  • - [KWayPartition.h](https://paste.rs/WI0)
  • - [I.h, I.cpp, Test.cpp](https://paste.rs/ZCp)
  • I need to write a KWayPartition method such that it satisfies this testcase:
  • ```cpp
  • int i = 0;
  • for (int j = 0; j < k; j++) {
  • while (A[i] < pivots[j])
  • i++;
  • assert (q[2 * j] == i);
  • while (A[i] == pivots[j])
  • i++;
  • assert (q[2 * j + 1] == i);
  • }
  • ```

Suggested almost 2 years ago by trichoplax‭