sorting

How to sort and array with integers in c++/
LETS SAY int arr[5]={1,2,3,4,5};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>
#include <iostream>

int main() {
    const int size = 5;
    int arr[size] = {1, 3, 2, 5, 4};

    std::sort(arr, arr + size);

    for (int i = 0; i < size; ++i) 
        std::cout << arr[i] << ' ';

    return 0;
}

Last edited on
Easiest way to sort something in C++ is to use std::sort.

1
2
3
4
5
6
7
8
#include <algorithm>
#include <iterator>

int main()
{
	int arr[5]={1,2,3,4,5};
	std::sort(std::begin(arr), std::end(arr));
}

http://en.cppreference.com/w/cpp/algorithm/sort
http://en.cppreference.com/w/cpp/iterator/begin
Topic archived. No new replies allowed.