Kernul wrote: |
---|
Where do I find those sorts? (Bubble sort, insertion sort and selection sort) |
You have to write the code yourself if you want to use any of those sorting methods I mentioned:
http://en.wikipedia.org/wiki/Bubble_sort
http://en.wikipedia.org/wiki/Insertion_sort
http://en.wikipedia.org/wiki/Selection_sort
There is also a sorting method in the algorithms library
http://www.cplusplus.com/reference/algorithm/sort/
This sort is already written for you, but you should practice writing the other sorts I mentioned as this is a good way to boost your learning.
To use the sort algorithm (implemented as introsort
http://en.wikipedia.org/wiki/Introsort), here is one of the many possible ways of doing it (This can also be applied to any sorting algorithm):
- First build a list numbers numbered 0-9 (indices)
- Then sort this list of numbers by using value contained in your pancake array as the condition
- So something like this:
1 2 3 4 5
|
...
std::array<int> nums = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::sort(nums.begin(), nums.end(), [&](int a, int b) -> bool {
return persons[a] > persons[b];
});
|
Then to print the values
1 2
|
for (int v : nums)
std::cout << "Person " << v << " ate " << persons[v] << " pancakes\n";
|
And there you have it. All the features used are part of c++11 standard
std::array is found in the
<array> library
[&](int , int ) -> bool is a lambda function that takes 2 integers as parameter and returns bool
for (int v : nums) This is a range based for-loop
Like I said, it is one way of doing it, and there are other ways