I want to sort the array items using the vector.

Example:
int buff[4] = {15, 6, 55, 34};

std::vector<int> myVector(buff, buff+4);

now i will pass the array in to sort the buff data in ascending order.

mySort(myVector.begin(),myVector.end());

How to do this ?
Kindly suggest.

Recommended Answers

All 4 Replies

Start by picking a sorting algorithm, then implement it. If you have any trouble with the coding, post your code and explain what isn't working how you'd expect.

void mySort(myVector.begin(),myVector.end())
{
   //Logic inside here
   ...
   ...
   ?
}

Actually i don't have an option to include the <algorithm> library.
I want to do the same functionality as sort() does in mySort() function.

As im new in this, please suggest something (logic) to execute inside the mySort() function.

Thanks well in Adwance.

As nmaillet said, pick a sorting algorithm and try to implement it. The wiki page has a pretty complete list. You should probably start with one of the simpler methods (the O(n^2) algorithms) like Bubble Sort, Insertion Sort, or Selection Sort. As for reproducing the behavior of the standard sort algorithm, it uses an intro-sort algorithm, which requires that you also implement quick-sort and heap-sort. Merge-sort is another interesting alternative.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.