Question
What is the time complexity (in Big-O notation) of thealgorithm with respect to the size of the playlist? How does this time complexity compare to
What is the time complexity (in Big-O notation) of thealgorithm with respect to the size of the playlist?
How does this time complexity compare to the time complexity of binary search (in terms of Big-O)?
//Coded Algorithm that will break the program into 3 sublists
// Encorperates a binary search in order to search through an array
// performs a binary search on an array.
#include
#include
using namespace std;
// Declaring the functions that will be called in the main function
// Can be declared and wrote immediately but doesnt matter
void sortStr(string [], int);
int binary_Search(string [], string);
int len;
int main()
{
// Array with songs not in a particular order and will be ordered
// alphabetically in the program
string songs[] = {"My Curse","Holy Diver","Heartbeat","Hurt","Bat Country",
"Fall Apart","Congradulations","End of Heartache","Hold My Hand","I Remember You","Really Really",
"Believe","Rain"};
// Global Variables that will be utilized.
string songName;
int res;
len=sizeof(songs)/sizeof(songs[0]);
//Send the Array through the sorting function
sortStr(songs, len);
cout<<"Songs after going through Ordering Function"< //List out the songs in alphabetical order for(int i=0;i cout<<"songs["< //Allow user to search for a song in the array and it will display the element cout << "Please enter a song name: "; getline(cin, songName); // Search for name res = binary_Search(songs, songName); // If results contains -1 the name was not found. if (res == -1) cout << "That song does not exist in the playlist. "; else { // If not returned -1 then output the song and its location cout << "That song is found at element " << res; cout << " in the play list. "; } return 0; } void sortStr(string songs[], int num) { int start, minin; string minVal; for (start = 0; start < (num - 1); start++) { minin = start; minVal = songs[start]; for(int in = start + 1; in < num; in++) { if (songs[in] < minVal) //Actual Swapping section when parameter passed { minVal = songs[in]; minin = in; } } songs[minin] = songs[start]; //Else swap a different way songs[start] = minVal; } } int binary_Search(string songs[], string val) { int start = 0, // start song array element end = len - 1, // end of song array element mid1, mid2, // Mid point of search position = -1; // Position of search value bool found = false; // Setting a flag to determine whether the while loop will r while (!found && start <= end) { mid1 = start+ ( end - start) / 3; // Calculate mid1 point mid2 = start + 2 * ( end - start) / 3 ; // Calculate mid2 point if (songs[mid1] == val) // If value is found at mid1 { found = true; position = mid1; } else if (songs[mid2] == val) // If value is found at mid2 { found = true; position = mid2; } else if (songs[mid1] > val ) // If value is in lower 1/3 end = mid1 - 1; else if(songs[mid2] < val) start = mid2 + 1; // If value is in 2/3 rd places else { start = mid1; end = mid2; } } return position; }
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