I know how to write the algorithm for Quicksort in a 1-dimensionalarray in C++:
ID: 3612688 • Letter: I
Question
I know how to write the algorithm for Quicksort in a 1-dimensionalarray in C++:void sort(double Data[], int Lo, int Hi)
{
int P;
P = partition(Data, Lo, Hi);
if(Hi > Lo)
{
sort(int Data, P +1, Hi);
sort(int Data, Lo,P - 1);
}
}
int partition(double Data[], int Lo, int Hi)
{
int Small=Lo, Curr, Mid=(Hi+Lo)/2;
swap(Data, Lo, Mid);
for(Curr=Lo+1; Curr<=Hi; Curr++)
{
if(Lo>=Curr)
{
Small++;
swap(Data, Small, Curr);
}
}
swap(Data, Lo, Small);
return Small;
}
void swap(double Data[], int First, int Second)
{
double Tmp=Data[First];
Data[First] = Data[Second];
Data[Second] = Tmp;
}
The question is this: How do you take into consideration a2-dimensional array with the above algorithm?
Explanation / Answer
please rate - thanks here is the 2 dimensional version of your code. yous didn'tcompile syntax errors highlighted (no int needed) void sort(double Data[], int Lo, int Hi) { int P; P = partition(Data, Lo, Hi); if(Hi > Lo) { sort(int Data, P + 1, Hi); sort(int Data, Lo,P - 1); } } I didn't want to change what you had, I just made it 2dimensional-let me know if you need it debugged #include using namespace std; void sort(double Data[][2], int Lo, int Hi,int col); int partition(double Data[][2], int Lo, int Hi,int col); void swap(double Data[][2], int First, int Second,int col); void print(double[][2],int,int); int main() {int i; double array1[][2]={2, 15, 23, 45, 17, 67, 32, 1, 2, 7, 19,1, 11,73, 42, 66, 82, 9, 9, 23, 45, 78, 231, 22, 589, 88}; for (i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.