std::sort() for arrays

closed account (iAk3T05o)
Could you use std::sort() for arrays. I've only used it when i was trying to learn vectors but didn't understand vectors. And i don't understand how to sort arrays using bubblesort etc.

It would be so much easier to use std::sort() for arrays.
Could you demonstrate it by sorting an array with 10 values?
Thanks.
Last edited on
Here are links to articles that you should read:
http://www.cplusplus.com/faq/sequences/sequencing/sort-stuff/
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/

But to give you the answer that you asked for:

1
2
3
4
5
#include <algorithm>

// ...

std::sort(array, array + 10);

Could you use std::sort() for arrays.
Not directly, because std::sort is an STL algorithm and requires iterators, which are not features of C++ arrays.

I've only used it when i was trying to learn vectors but didn't understand vectors.
The example here: http://www.cplusplus.com/reference/algorithm/sort/ shows the use of std::sort using a vector initialized from an array.
> Could you demonstrate it by sorting an array with 10 values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>

template < typename RA_SEQUENCE > void sort( RA_SEQUENCE& seq )
{
    std::cout << "unordered: " ;
    for( const auto& v : seq ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    std::sort( std::begin(seq), std::end(seq) ) ;

    std::cout << "   sorted: " ;
    for( const auto& v : seq ) std::cout << v << ' ' ;
    std::cout << "\n\n" ;
}


int main()
{
    int a[] = { 12, 45, 89, 23, 45, 65, 10, 98, 12, 34 } ;
    sort(a) ;

    std::string str = "managing a menagerie" ;
    sort(str) ;

    std::vector<std::string> vec = { "¿", "Could", "you", "use", "std::sort()", "for", "arrays", "?" } ;
    sort(vec) ;
}

http://coliru.stacked-crooked.com/a/8a5e5f545beabb6d
closed account (NyqLy60M)
It might also be worth mentioning that there's an array container -- though, it's important to note that the array is going to be a constant size.

1
2
3
4
5
6
7
8
9
10
11
#include <array>

...

std::array<int, 10> a = {12, 45, 89, 23, 45, 65, 10, 98, 12, 34};
std::array<int, 10>::iterator itr1 = a.begin();
std::array<int, 10>::iterator itr2 = a.end();

std::sort(itr1, itr2);

...
Last edited on
Another thing to point out (which I think is what the OP is not understanding) is that iterators are also pointers. If you take a look at the way iterators are defined for most STL containers, you come to see that they are just fancy pointers.

So with this knowledge, you can see that if you are able to sort any container which supports random access (random_access_iterators) by providing a "pointer" to the start of the object and a "pointer" to the past_the_end element of the container.

This is demonstrated by JLBorge's answer
closed account (iAk3T05o)
The only example i understood was that of catfish666 and maybe vemc's even though i don't know how the looks very different.
closed account (iAk3T05o)
How would you find the smallest and largest values in an array sorted with std::sort (array, array+num).
I've tried it but it doesn't work
Check the first and the last elements.
closed account (iAk3T05o)
@zhuge: could you show an example
Do you know how to get elements of an array?
closed account (iAk3T05o)
1
2
3
4
5
int arrai[] = {1, 2, 3, 5, 8, 9, 7, 6, 4, 0}
for (int i = 0; i < 10; ++i)
{
std::cout << arrai[i];
}

is it something like that?
closed account (iAk3T05o)
I thought you wanted to help me
How would you find the smallest and largest values in an array sorted with std::sort (array, array+num).


Let's say you have an array, containing {2, 1, 90, -3}.

After you sort this array, it will contain {-3, 1, 2, 90} which is the same elements but in lowest-to-greatest order.

Now obviously, in order to find the smallest and largest value all you need to do is take the first and last elements: specifically -3 and 90 and generally arr[0] and arr[arr_size - 1].
closed account (iAk3T05o)
Could you show an example
Here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>

int main()
{
    int array[10] = {7,2,6,8,10,3,1,9,5,4};
    std::sort(array, array+10);
    // Or, if you have a C++11 compiler:
    //std::sort(std::begin(array), std::end(array)); // Also #include <iterator>
    
    std::cout << "Min: " << array[0]; // First element
    std::cout << "\nMax: " << array[9]; // Last element
}
closed account (iAk3T05o)
@long double main:
Thanks.
It was so easy and i didn't think of that.
Using this method of sorting is so much easier than insertion sort and has so much less code.
Well, insertion sort doesn't exactly take up very much space anyways...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <utility>

void sort(int array[10])
{
    for (int i = 1; i < 10; ++i)
        for (int j = i; j > 0 && array[j] < array[j-1]; --j)
            std::swap(array[j], array[j-1]);
}

int main()
{
    int array[10] = {7,2,6,8,10,3,1,9,5,4};
    sort(array);
    
    std::cout << "Sorted array:\n";
    for (int i = 0; i < 10; ++i)
        std::cout << array[i] << ' ';
}
Sorted array:
1 2 3 4 5 6 7 8 9 10

Although std::sort is probably going to be faster than just about anything you can write yourself....
Last edited on
closed account (iAk3T05o)
That's definitely not the insertion sort code that's in the jumping into c++ book.
It has over 70 lines consisting of very confusing functions and function names.
You're right, Nathan2222, I have the same book. I used it as the basis for some of simpler sorting, shuffling and swapping functions I created myself. This here does look like it would be more efficient and probably faster too, though.
Topic archived. No new replies allowed.