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

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:...
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,...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
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....
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
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
Create a swap method without a temporary variable. Implement an algorithm to figure out if someone...
Create a swap method without a temporary variable. Implement an algorithm to figure out if someone has won a game of tic-tac-toe. import java.util.Arrays; public class ArrayReview { private int data[]; public ArrayReview(int n) { data = new int[n]; for (int i = 0; i < n; i++) { data[i] = (int) (Math.random() * 100); } } public int[] getData() { return data; } public void setData(int[] data) { this.data = data; } @Override public String toString() { return Arrays.toString(data);...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT