Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Quick sort problem Attached is my code but it doesn't pass the test cas es . Please modify my code to make it pass all

Quick sort problem
Attached is my code but it doesn't pass the test cas
es. Please modify my code to make it pass all test cases. In C++ please.
#include
using namespace std;
int swaps =0;
int* ReadNums(int& size){
cin >> size;
int* numbers = new int[size];
for (int i =0; i size; i++){
cin >> numbers[i];
}
return numbers;
}
void PrintNums(int* numbers, int left, int right){
for (int i = left; i = right; i++){
cout numbers[i];
if (i != right){
cout "";
}
}
cout endl;
}
int Partition(int* numbers, int left, int right){
int pivot = numbers[right];
int i = left -1;
for (int j = left; j right; j++){
if (numbers[j] pivot){
i++;
swap(numbers[i], numbers[j]);
swaps++;
}
}
swap(numbers[i +1], numbers[right]);
swaps++;
return i +1;
}
void QuickSort(int* numbers, int left, int right){
if (left right){
int pivotIndex = Partition(numbers, left, right);
cout "Pivot: " numbers[pivotIndex] endl;
//cout "Pivoted Array: ";
PrintNums(numbers, left, right);
QuickSort(numbers, left, pivotIndex -1);
QuickSort(numbers, pivotIndex +1, right);
} else {
cout "Left Side: ";
PrintNums(numbers, left, right);
cout "Right Side: " endl;
}
}
int main(){
int size =0;
int* numbers = ReadNums(size);
cout "Unsorted:" endl;
PrintNums(numbers,0, size -1);
QuickSort(numbers,0, size -1);
cout "Sorted:" endl;
PrintNums(numbers,0, size -1);
cout "Swaps: " swaps endl;
delete[] numbers;
return 0;
}
For input : 6321598
6 indicates how many numbers are needed to sorted(in 321598,6 number needed to sorted)
the output will be:
Unsorted:
321598
Pivot: 8
321589
Pivot: 5
3215
Pivot: 1
123
Left Side:
Pivot: 3
23
Left Side: 2
Right Side:
Right Side: 23
Left Side: 123
Right Side:
Left Side: 1235
Right Side: 9
Sorted:
123589
Swaps: 2
other input will be
8123456789
897654321
823456791
821436597
output for other input
Unsorted:
9,7,6,5,4,3,2,1
Pivot: 1
1,7,6,5,4,3,2,9
Left Side:
Pivot: 9
7,6,5,4,3,2,9
Pivot: 2
2,6,5,4,3,7
Left side:
Pivot: 7
6,5,4,3,7
Pivot: 3
3,5,4,6
Left side:
Pivot: 6
Expected output
546
Pivot: 4
45
Left Side:
Right side: 5
Left Side: 45
Right side:
Right Side: 456
Left Side: 3,4,5
Right side:
Right side: 3,4,5,67
Left Side: 2,3,4,5,6,7
Right side:
Right side: 2345674
Sorted:
1,2,3,4,5,6,7,9
Swaps: 4Unsorted:
2,3,4,5,6,7,9,1
Pivot: 1
1,3,4,5,6,7,9,2
Left side:
Pivot: 2
2,4,5,6,7,9,3
Left side:
Pivot: 3
3,5,6,7,9,4
Left side:
Pivot: 4
4,6,7,9,5
Left Side:
Pivot: 5
5,7,9,6
Expected output Left Side:
Pivot: 64
6,97
Left Side:
Pivot: 7
7,9
Left Side:
Right side: 9
Right Side: 79
Right Side: 679
Right Side: 5,67,9
Right side: 4,5,6,7,9
Right side: 3,4,5,6,7,9
Right Side: 2,3,4,5,6,7,9
Sorted:
1,2,3,4,5,6,7,9
Swaps: 7
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions

Question

What are the three types of beta that can be used in the CAPM?

Answered: 1 week ago

Question

What is Accounting?

Answered: 1 week ago

Question

Define organisation chart

Answered: 1 week ago

Question

What are the advantages of planning ?

Answered: 1 week ago

Question

Explain the factors that determine the degree of decentralisation

Answered: 1 week ago

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

7. Identify four antecedents that influence intercultural contact.

Answered: 1 week ago