Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ need help with partition function. may not change any of the code in main #include using namespace std; // You may not change my

c++ need help with partition function. may not change any of the code in main

#include using namespace std;

// 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

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

=+ 5. Beleaguered State Bank (BSB) holds $250 million

Answered: 1 week ago