Insertion sort c++ vectors -
void insertion_sort(int *data, unsigned int n) { (unsigned int uns = 1; uns < n; ++uns ) { int next = data[uns]; unsigned int idx; (idx = uns; idx > 0 && data[idx - 1] > next; --idx) { data[idx] = data[idx - 1]; } data[idx] = next; } } int main() { vector<person> crew= ucitaj_osobe("osobe.txt"); /*this reads file osobe.tx , stores in vector crew,this works */ person o; insertion_sort(polje, 100); // ??? ispisi_osobe(popis); /* prints out vector,this works too*/ return 0; }
how send vector insertion sort,and sort it? please help,the code insertion sort implemented source
your function insertion_sort
implemented sort arrays of int
, function not work sorting of vector of person
objects.
if want sort vector of person
objects suggest use std::sort
standard library. use must implement <
operator person
objects.
example:
// demonstrate. struct person { std::string name; int age; }; // implement according needs. bool operator< (const person& lhs, const person& rhs) { return lhs.name < rhs.name; }
int main() { vector<person> crew = ucitaj_osobe("osobe.txt"); std::sort(begin(crew), end(crew)); ispisi_osobe(popis); // return 0; unnecessary in main function. }
live example: http://ideone.com/yel7iv
note std::sort
not guaranteed use insertion sort.
Comments
Post a Comment