c++ - Arranging words in alphabetical order -
this implementation , have txt file animals randomly assign. want order them , insert list.
void sortedlist::insert(std::string x){ int insertpoint=0; if(top==n){ n = 2 * n; string* temp = arr; arr = new string[n]; (int = 0; < top; i++){ arr[i] = temp[i]; } delete[] temp; } arr[top]=x; linearordering(); top++; }
and
void sortedlist::linearordering(){ for(int i=0; < top ; i++){ if (arr[i] > arr[ + 1]) { swap (arr[i], arr[i+1]); } } }
this result
aardvark baboon cougar gorilla lion mouse ocelot gerbil orangutan hamster panther elephant rat rhinoceros tiger hippopotamus zebra
what wrong code make partially ordered.
it looks you're trying bubble-sort list. have loop more once make work.
this answer assumes you're doing educational purposes. otherwise, stilescrisis suggests, use std::sort()
, might prefer std::vector<std::string>
instead of string*
.
Comments
Post a Comment