Question

In: Computer Science

(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...

(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it.

quickSort.cpp

#include <iostream>

using namespace std;

  

// A helper function to facilitate swapping two int elements

// You might want to use this function in the function partition().

void swap(int& a, int& b)

{

}

  

// This function partitions sub-array A[p..r]. It takes last element in the sub-array, i.e., A[r], as pivot,

// places the pivot element at its correct position in sorted array,

// places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot,

// and returns the index of the pivot element (i.e. it's correct position in sorted array)

// You might want to use this function in the function quickSort().

int partition (int A[], int p, int r)

{

}

  

// using quickSort to sort sub-array A[p..r]  

// p is for left index and r is right index of the

// sub-array of A[] to be sorted

void quickSort(int A[], int p, int r)

{

}

  

int main()

{

cout << "Please enter the length (number of elements) of the input array: ";

int n;

cin >> n;

if(n <= 0) {

cout << "Illegal input array length!" << endl;

return 0;

}

int* A = new int [n];

cout << "Please enter each element in the array" << endl;

cout << "(each element must be an integer within the range of int type)." << endl;

for(int i=0; i<n; i++) {

cout << "A[" << i << "] = ";

cin >> A[i];

}

  

cout << "Given array A[] is: ";

for(int i=0; i<n-1; i++)

cout << A[i] << ",";

cout << A[n-1] << endl;

  

quickSort(A, 0, n-1);

  

cout << "After quickSort, sorted array A[] is: ";

for(int i=0; i<n-1; i++)

cout << A[i] << ",";

cout << A[n-1] << endl;

delete [] A;

return 0;

}

Solutions

Expert Solution

//quickSort.cpp

#include <iostream>

using namespace std;

// A helper function to facilitate swapping two int elements

// You might want to use this function in the function partition().

void swap(int& a, int& b)

{

int temp =a;

a=b;

b=temp;

}

int partition(int A[],int p,int r){

int pivot=A[r];

int i=0,j=-1;

while(i<r){

if(A[i]<pivot){

j++;

swap(A[i],A[j]);

}

i++;

}

A[i]=A[++j];

A[j]=pivot;

return j;

}

void quickSort(int A[],int p,int r){

int pivot;

if(p<r){

pivot=partition(A,p,r);

quickSort(A,p,pivot-1);

quickSort(A,pivot+1,r);

}

}

  

int main()

{

cout << "Please enter the length (number of elements) of the input array: ";

int n;

cin >> n;

if(n <= 0) {

cout << "Illegal input array length!" << endl;

return 0;

}

int* A = new int [n];

cout << "Please enter each element in the array" << endl;

cout << "(each element must be an integer within the range of int type)." << endl;

for(int i=0; i<n; i++) {

cout << "A[" << i << "] = ";

cin >> A[i];

}

  

cout << "Given array A[] is: ";

for(int i=0; i<n-1; i++)

cout << A[i] << ",";

cout << A[n-1] << endl;

  

quickSort(A, 0, n-1);

  

cout << "After quickSort, sorted array A[] is: ";

for(int i=0; i<n-1; i++)

cout << A[i] << ",";

cout << A[n-1] << endl;

delete [] A;

return 0;

}

//sample output


Related Solutions

4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a...
Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a better running time and implement in Java one of the ways you provide (add few comments to explain your code)
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN) B. Evaluate the following postfix expression. 2 3 4 + * C. In an array representation of heap, what are the parent node of a node a[10]? a[9] a[11] a[5] a[20] There is no easy way to access parent node in such representation. D. In an array representation of heap, what are the children nodes (if any) of a node a[10]? a[11] and a[12]...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
Develop the code for this sort algorithm: Shaker shakerSort is a variation of the bubbleSort where...
Develop the code for this sort algorithm: Shaker shakerSort is a variation of the bubbleSort where we alternately go up and then down switching out-of-order pairs until done. In it have code that counts the number of moves and number of compares.
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT