Question
need a main.cpp , SelectionAlgotihm.cpp and TestBed.cpp In this assignment, you will write a C++ program that finds the k th largest number among a
need a main.cpp , SelectionAlgotihm.cpp and TestBed.cpp
In this assignment, you will write a C++ program that finds the kth largest number among a set of N numbers. However, this time, you will use a variation of the quicksort algorithm to implement the program. The details can be found below.
1) The Input and Expected Output
The program will take the type of algorithm to be applied, k (a number less than or equal to N). Then it will take N followed by a list of N numbers. As output, it will print out the kth largest number and the total elapsed time for the completion of the algorithm.
2) The Algorithm: quickselect
You will implement a variation of the quicksort algorithm to find the kth largest number among a set of numbers S in O(N) time on average, where N is the total amount of numbers, i.e., |S|. The outline of the algorithm, quickselect is as follows.
- If N , sort all the numbers (using insertion sort) and return the kth number.
- Else, pick a pivot number, v S.
- Partition S {v} into S1 and S2, as done in quicksort.
- If k 1|, then the kth largest number must be in S1. Return quickselect(S1, k).
- If k = 1 + | S1|, then the pivot, v is the kth largest number. Return v.
- Otherwise, the kth largest number lies in |S2|, and it is the (k |S1| 1)st largest number in S2. Return quickselect(S2, k |S1| 1).
main.cpp main / modification/extension if(type == 4) algorithm = new Algorithm SortQuick (k); cu ses TestBed / -* algorithm: Selection Algorithm +execute():void +set Algorithmink: int, in type : int) : void +TestBed() + TestBed) ! Selection Algorithm #k: int +select(): int +Selection Algorithm (ink: int) AgorithmSortal AlgorithmSortk +select(): int +AlgorithmsortAllink: int) +select(): int +AlgorithmSortKink: int) extension AlgorithmSortQuick I select(): int +quickselect/in numbers * : int, in left : int, in right : int, in k : int): int I AlgorithmsortQuick(ink: int)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started