• 0

Locate one defined element inside a vector, in C


Question

I need one algorithym to find a n bigger element inside a vector, without ordering it.

Give n=5 find the 5 bigger element in the vector. Cant order the vector or use a seconde vector

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

create a vector of size n (int *result = new int[n];).

fill it with zeros.

create an int to keep a reference to the smaller number saved in this vector (int min=0). this will tell u at what position is the smaller number, and thus where you should insert your next value.

loop on your initial vector, filling your result vector with the numbers u find that are > result[min]. after each insert, find the smaller number in result and save it's position in min.

though it would be easier to just make a copy of your vector and sort the copy...

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.