#include <iostream>
#include <vector>
int main() {
int unsorted[] = {5,7,12,890,-5,-4,3,2,1,56,890,-1043};
std::vector<bool> tracker(12, false);
const int n = 5; //we want the top n largest numbers
std::vector<int> topN;
for(int i = 0; i < n; i++) {
//an integer needs to store the maximum in the list.
//but it cannot be one that has already been marked
//as true in tracker.
//so first we will loop until the first un-marked number.
int unmarked_index = 0;
for(; unmarked_index < tracker.size(); unmarked_index++) {
if(!tracker[unmarked_index]) {
//we found one that has not been visited yet
break;
}
}
//the max variable will hold the maximum after the next
//loop finishes. first we must initialize it
//and the loop we just came from gives us the
//index to initialize with.
//also, we must keep track of where the max is found
//so that it can be marked in tracker after the loop
int max = unsorted[unmarked_index];
int max_index = unmarked_index;
for(int j = unmarked_index+1; j < tracker.size(); j++) {
if(!tracker[j] && unsorted[j] > max) {
//if j is not marked and j is > max
max = unsorted[j];
max_index = j;
}
}
//time to mark max as used
tracker[max_index] = true;
//and add max to topN
topN.push_back(max);
}
for(int i = 0; i < topN.size(); i++) {
std::cout << topN[i];
if(i != topN.size() - 1) std::cout << ", ";
}
std::cout << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKSB7CglpbnQgdW5zb3J0ZWRbXSA9IHs1LDcsMTIsODkwLC01LC00LDMsMiwxLDU2LDg5MCwtMTA0M307CglzdGQ6OnZlY3Rvcjxib29sPiB0cmFja2VyKDEyLCBmYWxzZSk7Cgljb25zdCBpbnQgbiA9IDU7IC8vd2Ugd2FudCB0aGUgdG9wIG4gbGFyZ2VzdCBudW1iZXJzCglzdGQ6OnZlY3RvcjxpbnQ+IHRvcE47CgkKCWZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKCQkvL2FuIGludGVnZXIgbmVlZHMgdG8gc3RvcmUgdGhlIG1heGltdW0gaW4gdGhlIGxpc3QuCgkJLy9idXQgaXQgY2Fubm90IGJlIG9uZSB0aGF0IGhhcyBhbHJlYWR5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