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

Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort....
Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort. This algorithm chooses the pivot element as the median of the 3 elements namely: A[p], A[r], and A[(p+r)/2].(Java language) Quicksort(A,p,r) 1 if p 2 N = r- p +1 3 m = Median3(A, p, p + N/2 , r) 4 exchange A[m] with A[r] 5 q = Partition (A,p,r) 6 Quicksort(A,p, q-1) 7 Quicksort(A,q+1,r)
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
●In this task, the quick sort algorithm selects the first element in the list as the...
●In this task, the quick sort algorithm selects the first element in the list as the pivot. Revise it by selecting the median among the first, middle, and last elements in the list. ● Write the algorithm for searching for entries using linear probing. ● Write the algorithm for removing entries using linear probing. ● Create a diagram similar to the one above that shows the hash table of size 11 after entries with the keys 34, 29, 53, 44,...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1,...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT