Question
c++ need help with partition function. may not change any of the code in main Simply add the code where * appears. Required Test Cases:
c++ need help with partition function. may not change any of the code in main Simply add the code where * appears.
Required Test Cases: (Must test in this order)
- 1 2 3 4 5 6 7 only 1 is in the left section
- 8 7 6 5 4 3 2 1 only 8 is in the right section
- 5 1 4 3 6 7 8 2 things less than 5 are in the left section
- 4 3 1 2 7 5 things less than 4 are in the left section
- 2 6 4 1 7 only 1 is in the left section
#include
// You may not change my code in any manner. 0 pts if you do. // Simply add your code for **
//---------------------------------------- //CS311 HW2P2 Partition //Name: //Compiler: g++ //-----------------------------------------
// partitions the array a into smaller than pivot and others. // f is the beginning of the section to be partitioned // r is the end of the section to be partitioned // return the first slot of the Large section int partition(int pivot, int a[], int f, int r) { int left = f; // pointer from the left int right = r; // pointer from the right
// loop for finding out of place pairs and swap them // ** until the left and right cross // if left OK, move left // if right OK, move right // if both are bad, swap
// return the partition point where // those smaller than pivot are before what is returned // ** there is a special cases and a regular case }
int main() { int x; // number of elements int nums[10]; cout << "How many elements? (must be less than 10) "; cin >> x; cout << "Enter elements one per line.." << endl; for (int i = 0; i < x; i++)
{ cin >> nums[i]; }
// the pivot is always the first element cout << "Ok the pivot is " << nums[0] << endl;
int part = partition(nums[0], nums, 0, x-1);
cout << "Results..." << endl; // display up to the partition without endl for (int i = 0; i < part; i++) cout << num[i];
cout << "|"; // display from the partition on.. without endl for (int i = part; i < x; i++) cout << num[i];
cout << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started