Sorting

How do you sort? I searched the internet and see so complicated ones. Can anyne give me a simplest way to sort? I am familiar with up to pointers.
By calling sort on an iterator range:
http://www.cplusplus.com/reference/algorithm/sort/
That one is complicated. Any other?
Is that supposed to be a joke?
How can it get any easier than calling a function?

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
  int myints[] = {32,71,12,45,26,80,53,33};
  sort(myints,myints+8);
}


There, it's sorted.

The only way to make it any easier is to store your values in a std::set. Then you'll have to do literally nothing to get your values sorted as std::set already takes care of that.
Last edited on
ok. SO there is already a function. I thought you had to use for loops for it to work
That one is complicated. Any other?


The C++ reference pages are an invaluable resource, but...to those unaccustomed to the formal formatting of them, they can make things look a lot more complicated than they really are.

As Athar indicated above, the sort() function really is just as simple as including <algorithm>, and calling sort with a beginning and ending location.
alright i will try that. But people(my friends) told me that i need to use for loop
That would be if you made your own sort function and had to iterate over all the elements to be sorted. Still... I don't see how a for loop makes things complicated.
Yea but how much I try i cannot get it to work.
Don't be shy...post a code snippet with your attempt at calling sort() and we'll look at it.
There is it. And it actually works. thanks!! But I thought it was harder then this as my friends said.
I dont understand
sort (array, array +5);
what does +5 part do?



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
#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    int array[5];
    cout << "Enter 5 numbers" <<  endl;
    for (int m=0 ; m < 5 ; m++)
    {
        
        cin >> array[m];
    }
    sort (array, array +5);
    for (int m=0; m < 5 ; m++)
    {
        cout << array[m];
    
    }
    
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Topic archived. No new replies allowed.