#include <bits/stdc++.h> using namespace std; vector<string> split_string(string
ID: 3891741 • Letter: #
Question
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
// Complete the quickSort function below.
vector<int> quickSort(vector<int> arr) {
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), ' ');
string arr_temp_temp;
getline(cin, arr_temp_temp);
vector<string> arr_temp = split_string(arr_temp_temp);
vector<int> arr(n);
for (int i = 0; i < n; i++) {
int arr_item = stoi(arr_temp[i]);
arr[i] = arr_item;
}
vector<int> result = quickSort(arr);
for (int i = 0; i < result.size(); i++) {
fout << result[i];
if (i != result.size() - 1) {
fout << " ";
}
}
fout << " ";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}
Explanation / Answer
Hello there I am providing answer for Step 1 and Implementation of QuicSort():
[1] Solution for step 1: Divide==>
#include<iostream>
#include<vector>
using namespace std;
vector<int> divide(vector<int> &v,int piv)
{
int temp;
vector<int> L;
vector<int> R;
int equal = piv;
for(int i = 0; i<v.size();++i)
{
if(v[i]<piv)
{
L.push_back(v[i]);
}
if(v[i]>piv)
{
R.push_back(v[i]);
}
}
for(int i=0;i<L.size();++i)
{
v[i] = L[i];
}
v[L.size()] = equal;
for(int i=0;i<R.size();++i)
{
v[L.size()+i+1] = R[i];
}
return v;
}
int main() {
vector<int> v;
int n;
cin >> n;
int temp;
for(int i=0;i<n;++i)
{
cin >> temp;
v.push_back(temp);
}
v=divide(v,v[0]); // You can pass any pivote value
for(auto i=v.begin();i!=v.end();++i)
{
cout << *i << endl;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[2] Implementation of QuickSort():
Here I used two function more to implement QuickSort,
vector<int> swap(vector<int> &v, int x, int y) {
int temp = v[x];
v[x] = v[y];
v[y] = temp;
return v;
}
vector<int> quickSort1(vector<int> &vec, int Left, int Right) {
int i, j, mid, piv;
i = Left;
j = Right;
mid = Left + (Right - Left) / 2;
piv = vec[mid];
while (i<Right || j>Left) {
while (vec[i] < piv)
i++;
while (vec[j] > piv)
j--;
if (i <= j) {
swap(vec, i, j);
i++;
j--;
}
else {
if (i < Right)
quickSort1(vec, i, Right);
if (j > Left)
quickSort1(vec, Left, j);
return vec;
}
}
}
// Here is your quickSort
vector<int> quickSort(vector<int> &arr)
{
arr= quickSort1(arr,0,arr.size());
return arr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Feel free to ask queries regarding above solution, I am always here to help you!!!
Thank You!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.