PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your solution. Readability of the program will be graded based on variable naming, indentation, commenting (not too little and not too much), spacing, consistency, and styling in general including choice of functions. [NOTE: the code at the end of this document does not completely meet requirements as it uses many single letter variables and has no internal documentation. You need to improve it.]
2. Compile and test your program
3. Run experiments and write a report of the performance of your implementation.
4. After you finish writing, testing, and running your experiments, you will need submit the program and your report for grading
PROBLEM Write a C++ program that has the following:
A) i. A class called AssortedSorter that has an array of integers of size at least 20001 and a counter that keeps track of how may numbers are in the array. ii. Member functions representing different sort methods as follows: • BubbleSort: implement the BubbleSort algorithm given in section 8.3 of textbook by Gaddis, Starting out with C++ from Control Structures through Objects. You should pass the array of numbers, how many numbers are in the array, and two reference parameters of type long int one to count loops and another to count swaps. Initialize the reference parameters to 0. First copy the data from the parameter array to the array in the class. The function should sort the array in the class. Put a statement to add one to the loop count just before “if (array[count] > array[count+1])”. Put a statement to add one to the swap count just before “temp = array[index]”.
• SelectionSort: implement the SelectionSort algorithm given in section 8.3 of textbook by Gaddis, Starting out with C++ from Control Structures through Objects. You should pass the array of numbers, how many numbers are in the array, and two reference parameters of type long int one to count loops and another to count swaps. Initialize the reference parameters to 0. First copy the data from the parameter array to the array in the class. The function should sort the array in the class. Put a statement to add one to the loop count just before the line “if (array[index] > minValue)”. Put a statement to add one to the swap count just before line “array[minIndex] = array[startScan]”.
• ShellSort: implement the ShellSort algorithm given at the end of this document. You should pass the array of numbers, a sequence array, how many numbers are in the array, and two reference parameters of type long int one to count loops and another to count swaps. Initialize the reference parameters to 0. First copy the data from the parameter array to the array in the class. The function should sort the array in the class. You will need a sequence array that contains number of elements in segments sorted per pass. An example may be: int sequence[] = { 364, 121, 40, 13, 4, 1, 0} ; Put a statement to add one to the loop count in the while loop. Put a statement to add one to the swap count just before the statement “array[j] = val”. Pass 0 for the variable start and stop is the number of elements minus 1.
• QuickSort: implement the non-recursive QuickSort algorithm given at the end of this document. You should pass the array of numbers, how many numbers are in the array, and two reference parameters of type long int one to count loops and another to count swaps. Initialize the reference parameters to 0. First copy the data from the parameter array to the array in the class. You will pass the count variables further by reference to the partition() function. Put two statements to add one to the loop count in the partition() function in the inner while loops. The first will be just before up++ and the second just before down--. Put two statements to add one to the swap count in the partition() function next to each of the calls to the swap() function. B) main(): In the main() function, declare an array of integers. You may choose any size of the array larger or equal to 20001. DO NOT USE THE C++11 array or STL vector. In the main function, ask a user how many numbers should be generated. Read the number into a variable which you may call UserNumOfNumbers. The number should be between 10 and 20000. The default should be 10000 numbers (that is to say the user should be able to say, “choose a number for me”). That many numbers are then generated and stored in the array using the rand() function found in the cstdlib standard library. You also need the ctime standard library to seed the time. Next, in the main function, create an object of AssortedSorter and call the four sort member functions described above one after another passing the array of numbers and assign the returned count of loop and data movements accordingly. Finally, display the counts that were returned by the different functions as follows: SORT ALGORITHM RUN EFFICIENCY ALGORITHM LOOP COUNT DATA MOVEMENT Bubble xxxxxxxxxx xxxxxxxxxx Selection xxxxxxxxxx xxxxxxxxxx Shell xxxxxxxxxx xxxxxxxxxx Quick xxxxxxxxxx xxxxxxxxxx The random number generator may look like this: srand(time(0)); for (int i = 0; i < UserNumOfNumbers; i++) array[i] = rand()%1000000 + 1;
Screenshot
Program
#include <iostream>
#include<ctime>
using namespace std;
#define MAX_SIZE 20000
//Create a class AssortedSorter
class AssortedSorter {
//Attributes of class
private:
int *intArray;
int elemCnt;
//Member functions
public:
void bubbleSort(int[], int, long int&, long
int&);
void selectionSort(int[], int, long int&, long
int&);
void quickSort(int[], int, long int&, long
int&);
};
//Implementation of class
/*
Function to sort an array using bubble sort
Param In: arr, an integer array
sz, number of elements in the array
loopCnt,
reference variable to count loop
swapCnt,
reference variable to count swaps
Param Out: None
*/
void AssortedSorter::bubbleSort(int arr[], int sz, long int&
loopCnt, long int& swapCnt) {
//Initialize refernce values
loopCnt = 0;
swapCnt = 0;
//Copy array and size
intArray = arr;
elemCnt = sz;
//Loop through the
array
for (int i = 0; i < elemCnt - 1;
i++)
{
//increment loop
count by one
loopCnt++;
if (intArray[i]
> intArray[i + 1])
{
//Increment swap count by 1
swapCnt++;
int temp = intArray[i];
intArray[i] = intArray[i + 1];
intArray[i + 1] = temp;
}
}
}
/*
Function to sort an array using selection sort
Param In: arr, an integer array
sz, number of
elements in the array
loopCnt,
reference variable to count loop
swapCnt,
reference variable to count swaps
Param Out: None
*/
void AssortedSorter::selectionSort(int arr[], int sz, long int&
loopCnt, long int& swapCnt) {
//Initialize refernce values
loopCnt = 0;
swapCnt = 0;
//Copy array and size
intArray = arr;
elemCnt = sz;
//smallest value index
int minIndex;
//Loop for sorting
for (int i = 0; i < elemCnt - 1; i++) {
minIndex = i;
for (int index = i + 1; index
< elemCnt; index++) {
// count
comparison
loopCnt++;
if
(intArray[index] < intArray[minIndex]) {
minIndex = index;
}
}
swapCnt++;
int temp =
intArray[minIndex];
intArray[minIndex] =
intArray[i];
intArray[i] = temp;
}
}
/*
Function to sort an array using quick sort
Param In: arr, an integer array
sz, number of
elements in the array
loopCnt,
reference variable to count loop
swapCnt,
reference variable to count swaps
Param Out: None
*/
void AssortedSorter::quickSort(int arr[], int sz, long int&
loopCnt, long int& swapCnt) {
//Initialize refernce values
loopCnt = 0;
swapCnt = 0;
//Copy array and size
intArray = arr;
int h = sz, l = 0;
elemCnt = elemCnt - 0 + 1;
//Temporary array act as stack
int *stackArray;
stackArray = new int[elemCnt];
// initialize top of stack
int top = -1;
// push initial values of l and h to stack
stackArray[++top] = 0;
stackArray[++top] = elemCnt;
// Keep popping from stack while is not empty
while (top >= 0) {
loopCnt++;
// Pop h and l
h = stackArray[top--];
l = stackArray[top--];
// Set pivot element at its
correct position
// in sorted array
int x = intArray[h];
int i = (l - 1);
for (int j = l; j <= h - 1;
j++) {
if (intArray[j]
<= x) {
i++;
int t = intArray[i];
intArray[i]= intArray[j];
intArray[j] = t;
swapCnt++;
}
}
int t = intArray[i+1];
intArray[i+1] = intArray[h];
intArray[h] = t;
swapCnt++;
int p = (i + 1);
// If there are elements on left
side of pivot,
// then push left side to
stack
if (p - 1 > l) {
stackArray[++top] = l;
stackArray[++top] = p - 1;
}
// If there are elements on right
side of pivot,
// then push right side to
stack
if (p + 1 < h) {
stackArray[++top] = p + 1;
stackArray[++top] = h;
}
}
}
int main()
{
//Delcare an array
int array[MAX_SIZE];
int UserNumOfNumbers;
srand(time(0));
//Prompt for array size
cout << "Enter number betwee 10 to 20000 for the
number of elements in the array: ";
cin >> UserNumOfNumbers;
while (UserNumOfNumbers<10 ||
UserNumOfNumbers>MAX_SIZE) {
cout << "Enter number betwee
10 to 20000 for the number of elements in the array: ";
cin >>
UserNumOfNumbers;
}
//Generate array
for (int i = 0; i < UserNumOfNumbers; i++)
array[i] = rand() % 1000000 +
1;
//Create object of the class
AssortedSorter sorter;
long int lCnt, sCnt;
//Display result
cout << "\nSORT ALGORITHM RUN EFFICIENCY
ALGORITHM LOOP COUNT DATA MOVEMENT \n";
sorter.bubbleSort(array, 1000, lCnt, sCnt);
cout << "Bubble " << lCnt << " "
<< sCnt << endl;
sorter.selectionSort(array, 1000, lCnt, sCnt);
cout << "Selection " << lCnt << " "
<< sCnt << endl;
sorter.quickSort(array, 1000, lCnt, sCnt);
cout << "Quick " << lCnt << " "
<< sCnt << endl;
}
-----------------------------------------------------------------------------------
Output
Enter number betwee 10 to 20000 for the number of elements in the array: 1000
SORT ALGORITHM RUN EFFICIENCY ALGORITHM LOOP COUNT DATA
MOVEMENT
Bubble 999 992
Selection 499500 999
Quick 504 1008
--------------------------------------------
Note:-
Please provide shell sort details.Then only i can implement that part as per your requirement
PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your...
"you will write a program in C++ which will read a list of up to 100 integers from the user, and print them back to the screen in sorted order. The user can indicate that they are done entering numbers by entering any negative value. Your program will store the entered numbers into an array. It will then sort the array, using the selection sort algorithm. After each iteration of the sorting algorithm, it will print the current state of...
Hello I need help with this program. Should programmed in C!
Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...
Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...
Write a complete C++ program that analyzes the following sorts by keeping track of how long they take to sort arrays of longS of size 5000, 10000, and 100000, as well as the number of swaps and comparisons they take in doing so: Insertion sort Selection sort Quick sort shell sort(using gaps of n/2, n/4, n/8, ..., 1) for 20, the gaps would be 10, 5, 2, and 1 shell sort(using gaps based on 2^k + 1, but starting at...
C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...
Practical 5: Write a program that implements several sorting
algorithms, and use it to demonstrate the comparative performance
of the algorithms for a variety of data sets.
Need Help With this Sorting Algorithm task for C++
Base Code for sorting.cpp is given.
The header file is not included in this. Help would be much
appreciated as I have not started on this due to personal
reasons
#include <cstdlib>
#include <iostream>
#include <getopt.h>
using namespace std;
long compares; // for counting...
The program defined in insertionSort.cpp gets a
list of numbers as input from the user and calls the insertion sort
algorithm to sort them. It also displays the list before and after
the sorting.
The insertion sort algorithm should be defined in the
InsertionSort.h file, but this definition has been
omitted and it is your task to provide it. The appropriate
insertion sort function is to be defined in the
InsertionSort.h file.
The InsertionSort.h file performs an include of
Swap.h,...
1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...
THIS IS FOR C++ PROGRAMMING USING VISUAL
STUDIO
THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING
#include "pch.h"
#include
#include
using namespace std;
// Function prototype
void displayMessage(void);
void totalFees(void);
double calculateFees(int);
double calculateFees(int bags) {
return bags * 30.0;
}
void displayMessage(void) {
cout << "This program calculates the total
amount of checked bag fees." << endl;
}
void totalFees() {
double bags = 0;
cout << "Enter the amount of checked bags you
have." << endl;
cout <<...