Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually sorting vector<int> in C++

I am currently looking into how Vectors work in C++. I have read and understand their functionality pretty well.

I'm looking at different ways of sorting a vector object with 10,000 ints, I've used the std::sort method, and a shell sort.

I noticed that a shell sort for a vector is slower than sorting a simple C style array. I learnt this was because "Fast element insertion or removal in the middle of the container is not supported" (http://www.cppreference.com/wiki/container/vector/start). So obviously a shell sort with lots of random accesses is going to be quite slow.

I was wondering in anyones experience what a better manual sorting method would be for a vector with 10,000 ints? This is for a learning exercise you see! :)

like image 811
Cameron Wilby Avatar asked Apr 10 '11 00:04

Cameron Wilby


People also ask

How do I manually sort a vector?

To sort a vector based on manual position of elements, we can use order function along with the factor function. The factor function will help us to arrange the vector elements in the order we want by defining the levels as vector elements and order function will order them.

How do I sort custom vectors?

You can sort a vector of custom objects using the C++ STL function std::sort. The sort function has an overloaded form that takes as arguments first, last, comparator. The first and last are iterators to first and last elements of the container.

How do you sort a vector without using the sort function?

The vector can use the array notation to access the elements. If you don't want to use the default std::sort , or std::sort with custom comparator, you can use qsort or write your own.

How do you sort an array of vectors?

Sorting a vector in C++ can be done by using std::sort(). It is defined in<algorithm> header. To get a stable sort std::stable_sort is used. It is exactly like sort() but maintains the relative order of equal elements.


2 Answers

For all intents and purposes, vectors are arrays with added niceties. Random access is as fast as the C-style array. Removing/inserting elements in the middle of a vector is slow - but the same applies to C-style arrays, too. Shell sort should be as fast on vectors as it is on arrays. To me, it sounds like you're doing something unorthodox.

Quicksort or introsort (std::sort is one) should be the fastest comparison-based sorts available to you. Mergesort is slightly slower than quicksort, but it does not have quicksort's susceptibility to pathological cases. On average, all of those take O(n lg n) time (with quadratic worst-case for quicksort).

EDITED UPDATE

Code: C-array and Vector based shellsorts. With optimisations or without, sorting 1 million elements takes twice as long for vectors, for a reason unbeknown to me. Looks like STL is doing a lot of error-checking when you access a vector.

like image 144
evgeny Avatar answered Sep 21 '22 18:09

evgeny


Try the quickselect or quicksort algorithms (very similar but depends on what you need). In any case, these are just relatively simple and popular algorithms - more importantly, in practice they are fast (although they do have bad worst cases).

Regards,
Dennis M.

like image 42
RageD Avatar answered Sep 19 '22 18:09

RageD