我在编写算法时遇到了一些问题......下面将介绍一些分配的细节;现在在你开始说我应该尝试之前,解决这个问题。作业尚未评分,我对实施的结论或解决方案不满意。我对编程很陌生,而且越来越明显,因为我遇到了一个问题;我想我最大的问题是正确使用功能。我离题了,我很乐意提供实际的代码,这将遵循下面的分配说明。感谢
在C ++中,编码搜索算法,搜索特定歌曲的字符串列表。搜索算法将有两个输入:播放列表,它是一个字符串数组,包含按字母顺序排列的歌曲列表;和一首特定的歌曲,这是一个字符串。如果在列表中找到歌曲,算法将返回歌曲的索引,否则返回-1。
这种搜索算法采用类似于二分搜索的分治方法,但略有不同。在二进制搜索中,列表在每个步骤中分为2个子列表;但是,对于您的作业,您将构建和算法,在每个步骤中将列表拆分为3个子列表。
INPUTS:播放列表,是一个字符串数组,包含按字母顺序排列的歌曲列表;和一首特别的歌曲,这是一个字符串。
条件:
如果在列表中找到歌曲,算法将返回歌曲的索引,并返回-1
ELSE:这种搜索算法采用类似于二分搜索的分治方法,但略有不同。在二进制搜索中,列表在每个步骤中分为2个子列表;
输出:但是,对于您的作业,您将构建和算法,在每个步骤中将列表拆分为3个子列表。
|解释和解决方案的实施
第1部分
在C ++中,编码搜索算法,搜索特定歌曲的字符串列表。搜索算法将有两个输入:播放列表,它是一个字符串数组,包含按字母顺序排列的歌曲列表;和一首特定的歌曲,这是一个字符串。如果在列表中找到歌曲,算法将返回歌曲的索引,否则将返回-1。 这种搜索算法采用类似于二分搜索的分治方法,但略有不同。在二进制搜索中,列表在每个步骤中分为2个子列表;但是,对于您的作业,您将构建和算法,在每个步骤中将列表拆分为3个子列表。
INPUTS:播放列表,是一个字符串数组,包含按字母顺序排列的歌曲列表;和一首特定的歌曲,这是一个字符串。 条件:
如果在列表中找到歌曲,算法将返回歌曲的索引,它将会 返回-1
ELSE:这种搜索算法采用类似于二分搜索的分治方法,但略有不同。在二进制搜索中,列表在每个步骤中分为2个子列表;
输出:但是,对于您的作业,您将构建和算法,在每个步骤中将列表拆分为3个子列表。
第2部分
算法相对于播放列表大小的时间复杂度(以Big-O表示法)是多少? 这个时间复杂度与二进制搜索的时间复杂度(就Big-O而言)相比如何? * /
include <iostream>
include <algorithm>
using namespace std;
void bindarySearchPly(string playlist[], int first, int mid, int scdmid2, int last, int y){
int index;
}
using namespace std;
int main() {
void *bindarySearchPly(int y);
index = &bindarySearchPly;
string song;
//inialization of array and array index
string playlst[250] = {"Welcome to JamRock","Jucy", "A G Cook feat. Hannah Diamond – Keri Baby", "Airhead – Believe", "Allie X – BITCH: XHIBIT 1","Alvvays – Archie, Marry Me", "Andy – Violence","Angel Olsen – Forgiven","Aphex Twin – minipops 67 [120.2][source field mix]", "Arca – Thievery", "Ariana Grande feat. The Weeknd – Love Me Harder",
"Azealia Banks – Ice Princess","Banks (prod Shlohmo) – Brain", "Baauer feat. Rae Sremmurd and AlunaGeorge – One Touch", "Beyonce feat. Nicki Minaj – ***Flawless (remix)",
"Bobby Shmurda – Hot Nigga","Bok Bok feat. Kelela – Melba’s Call" ,"Boothroyd – NYC", "Caribou – Can’t Do Without You", "Chance the Rapper & The Social Experiment – No Better Blues",
"Charli XCX – Boom Clap", "Danity Kane – Lemonade", "Danny L Harle – In My Dreams", "Dej Loaf – Try Me","DELS – RGB","Drake – 0-100/The Catch-Up", "Dutch E Germ – Black Sea", "Elliphant feat. MO – One More",
"Evian Christ – Salt Carousel","Father feat. iLoveMakonnen & Key! – Look At Wrist", "Felicita – Doves","FKA Twigs – Two Weeks", "Flying Lotus feat. Kendrick Lamar – Never Catch Me", "Future feat. Pharrell Williams, Pusha T – Move That Dope","GEoRGiA – Hard Lie This", "GFOTY – Bobby","Holly Herndon – Chorus", "iLoveMakonnen feat. Drake – Tuesday","Jamie xx – All Under One Roof Raving","Jelani Blackman – Twenty Three",
"Jessie Ware – Tough Love","Juce – Call You Out","Juicy J feat Nicki Minaj, Lil Bibby, Young Thug – Low","K9 – Untouchable","Kindness – This Is Not About Us","Kendrick Lamar – i",
"Kero Kero Bonito – Sick Beat","Keyshia Cole feat. Milla J, K Michelle, Lil Mo & Da Brat – Loyal (remix)","Kwamie Liv – Follow You","La Roux – Uptight Downtown","Lana Del Rey – Brooklyn Baby",
"Le1f – Boom","LV and Josh Idehen – Imminent","Lykke Li – Gunshot","Mapei – Don’t Wait","Mark Ronson feat. Bruno Mars – Uptown Funk","Meridian Dan – German Whip","Migos – Hit Em","Mozart’s Sister – Good Thing Bad Thing",
"Mr Mitch – Don’t Leave","Mumdance & Novelist – Take Time","Nicki Minaj – Anaconda","Ought – Today More Than Any Other Day","Palmistry – Protector SE5","PARTYNEXTDOOR feat. Drake – Recognize",
"Perfume Genius – Queen","Popcaan – Everything Nice","Rae Sremmurd – No Type","Raury – God’s Whisper","Rich Gang – Lifestyle","Rick Ross feat. Kanye West and Big Sean – Sanctified","Run The Jewels feat BOOTS – Early”,"
"Ryn Weaver – OctaHate","Schoolboy Q – Studio", "SD Laika – Meshes", "Shamir – If It Wasn’t True", "Shura – Touch", "Sia – Chandelier","Skepta – That’s Not Me”","Slackk – Ancient Dolphin","SOPHIE – Hard",
"Spooky Black – Without You","Sun Kil Moon – Ben’s My Friend","SZA – Sobriety","Tei Shi – Bassically","Tiga – Bugatti","Tinashe feat. Schoolboy Q – 2 On",
"Tink feat Jeremih – Don’t Tell Nobody","Tirzah – No Romance","TOPS – Change Of Heart", "QT – Hey QT","Vic Mensa – Down On My Luck","Vessel – Red Sex", "War On Drugs – Red Eyes","Wet – Don't Wanna Be Your Girl",
"Wiley – “On A Level", "Years & Years – Take Shelter", "YG feat. Lil Wayne, Rich Homie Quan, Meek Mill", "Nicki Minaj – My Nigga (remix)", "Young Fathers – War", "Young Thug – Danny Glover",
"Young Thug feat. A$AP Ferg & Freddie Gibbs – Old English","Zara Larsson – Rooftop"};
cout << "Welcome to Choose that song Musicbox!";
cout << "please specify the song you would like to listen too:";
cin >>song;
cin.ignore();
return 0;
}
int bindarySearchPly(string index, string playlist[], int first, int mid, int scdmid2, int last, string y){
//int mid = first/2;
//int last = mid+first/2;
//int scdMid = last+mid+first/2;
string y = song;
for (int i = 0; i < 150; i++) {
mid = last + (first - last) / 2;
scdmid2 = first(first - mid) / 2;
string x;
if(index(y, playlist[mid])) { //potential error due to type conversion int and string && strcomp maybe considered as an not a function
index = mid;
cout << "Thank you, your selection has been identified located within index:" << playlist[i] << endl;
} else if (index(playlist[last]), y) {
index = last = mid + 1;
cout << "Thank you, your selection has been identified located within index:" << playlist[i] << endl;
} else if (index(playlist[scdmid2], y) {
index = mid = last - 1;
cout << "Thank you, your selection has been identified located within index:" << playlist[i] << endl;
} else {
cout << "unable to find music seleciton"<< endl;
}
return -1;
}
}