In C++ Part A: Selection Sort verses Bubble Sort Benchmark This program will rea
ID: 3805665 • Letter: I
Question
In C++
Part A: Selection Sort verses Bubble Sort Benchmark
This program will read data from file unsortedData.txt into two identical arrays.
Name these arrays selectionArray and bubbleArray.
Create a function called selectionSort that sorts the selectionArray data using the selection sort approach. The function should keep track of the number of exchanges made during the selection sort. Write contents of the sorted selection Array to a file named sortedSelectionData.txt and report the number of exchanges done
Next the program should sort the bubbleArray using a function named bubbleSort. Again, keep track of the number of exchanges that the bubble sort requires. Write the output of the sorted bubbleArray to a file named sorted sortedBubbleDatatxt.
Sample console output:
Reading selectionData.txt and copying into selectionArray and bubbleArray
Sorting selectionArray using linear sort
Number of exchanges was XX
Sorting bubbleArray using bubble sort
Number of exchanges was X
Writing bubbleArray to file sortedBubbleData.txt
The results of the PartA will be a two sorted data files and a screen capture. The data files will contain a data file using the selection sort method and a data file sorted using the bubble sort method. (Note, both files should be identical). Name the screen capture file screenCapturePartA. Upload the files to catalyst
Part B: Linear Search verses Binary Search Benchmark:
Write a program compares the number of steps needed by a Linear search verses steps needed for a binary search.
Program algorithm:
Reads the sortedBubbleData.txt created in PartA into an array name searchArray.
Call a function named linearSearch which uses the linear search method
Use the values 26, 50 and 92 for linear searches and report the number of steps needed
Call a function called binarySearch which uses the binary search method.
Use the values 26, 50 and 92 for do a binary searches and report the number of steps needed
Sample screen output:
Linear search for 26 was x steps. Value was found or not found
Linear search for 92 was y steps. Value was found or not found
Linear search for 50 was z steps. Value was found or not found
Binary search for 26 was x steps. Value was found or not found
Binary search for 92 was y steps. Value was found or not found
Binary search for 50 was z steps. Value was found or not found
Note that the searchArray size should be size 20 and the each search should report if the value being searched for is not found.
Part C: Car Sales Program using Vectors
Create a car sales report.
Use the following values for your data vectors
Salespersons:
"Aleshia", "Evan", "Frank", "Ulysses", "Tyisha", "Eric", "Marge"
CarsSold:
100, 200, 250, 175, 125, 150, 60
Sample of the report to create:
Sales Person Cars Sold % of Sales
Jane 60 5%
Bill 40 2%
James 150 8%
. . .
TopSalesPerson: Jane
Upload your source program file to carSales.cpp and screen capture of your carsReport to Catalyst
UnsortedData.txt
84
74
65
26
37
91
94
44
52
50
50
47
34
61
70
12
59
94
61
32
Explanation / Answer
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int selectionSort(int array[], int size)
{
int count=0;
for(int i=0; i<size; i++)
{
int min = array[i];
int index = i;
for(int j=i+1; j<size; j++)
{
if(array[j]<min)
{
min = array[j];
index=j;
}
}
if(index!=i)
{
int tmp = array[i];
array[i] = array[index];
array[index] = tmp;
count++;
}
}
return count;
}
int bubbleSort(int array[], int size)
{
int count=0;
for(int i=0; i<size; i++)
{
for(int j=0; j<size-1-i; j++)
{
if(array[j]>array[j+1])
{
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
count++;
}
}
}
return count;
}
void linearSearch(int array[], int size, int num)
{
int count=0, i=0;
for(i=0; i<size; i++)
{
count++;
if(array[i]==num)
break;
}
if(i==size)
cout << "Linear search for " << num << " was " << count << " steps. Value was not found" << endl;
else
cout << "Linear search for " << num << " was " << count << " steps. Value was found" << endl;
}
void binarySearch(int array[], int size, int num)
{
int count=0;
int beg = 0;
int end = size-1;
while(beg<=end)
{
int mid = (beg+end)/2;
count++;
if(num==array[mid])
break;
else if (num > array[mid])
beg = mid+1;
else
end = mid - 1;
}
if(beg>end)
cout << "Binary search for " << num << " was " << count << " steps. Value was not found" << endl;
else
cout << "Binary search for " << num << " was " << count << " steps. Value was found" << endl;
}
int main()
{
ifstream fin;
ofstream fout;
fin.open("unsortedData.txt");
int selectionArray[100], bubbleArray[100];
int size=0;
cout << "Reading selectionData.txt and copying into selectionArray and bubbleArray" << endl;
while (!fin.eof())
{
int tmp;
fin >> tmp;
bubbleArray[size] = tmp;
selectionArray[size] = tmp;
size++;
}
fin.close();
//Selection Sort
cout << "Sorting selectionArray using linear sort" << endl;
cout << "Number of exchanges was " << selectionSort(selectionArray, size) << endl;
fout.open("sortedSelectionData.txt");
cout << "Writing selectionArray to file sortedSelectionData.txt" << endl;
for(int i=0; i<size; i++)
fout << selectionArray[i] << " ";
fout.close();
//Bubble Sort
cout << "Sorting bubbleArray using bubble sort" << endl;
cout << "Number of exchanges was " << bubbleSort(bubbleArray, size) << endl;
fout.open("sortedBubbleData.txt");
cout << "Writing bubbleArray to file sortedBubbleData.txt" << endl;
for(int i=0; i<size; i++)
fout << bubbleArray[i] << " ";
fout.close();
//Linear Search
fin.open("sortedBubbleData.txt");
size=0;
int searchArray[100];
while (!fin.eof())
{
int tmp;
fin >> tmp;
searchArray[size] = tmp;
size++;
}
fin.close();
linearSearch(searchArray, size, 26);
linearSearch(searchArray, size, 92);
linearSearch(searchArray, size, 50);
//Binary Search
binarySearch(searchArray, size, 26);
binarySearch(searchArray, size, 92);
binarySearch(searchArray, size, 50);
//Part C
vector <char*> salesPersons;
vector <int> carsSold;
salesPersons.push_back("Aleshia");
salesPersons.push_back("Evan");
salesPersons.push_back( "Frank");
salesPersons.push_back("Ulysses");
salesPersons.push_back("Tyisha");
salesPersons.push_back("Eric");
salesPersons.push_back("Marge");
carsSold.push_back(100);
carsSold.push_back(200);
carsSold.push_back(250);
carsSold.push_back(175);
carsSold.push_back(125);
carsSold.push_back(150);
carsSold.push_back(60);
int sum=0;
for(int i=0;i<7;i++)
sum+=carsSold[i];
cout<<"Sales Person Cars Sold % of Sales ";
int maxindex = 0;
for(int i=0;i<7;i++)
{
if(carsSold[i] > carsSold[maxindex])
maxindex = i;
cout<<salesPersons[i]<<" "<<carsSold[i]<<" "<<(carsSold[i]*100)/sum<<"% ";
}
cout << "TopSalesPerson: " << salesPersons[maxindex] << endl;
return 0;
}