# Sort a double array

**URL:** <https://root-forum.cern.ch/t/sort-a-double-array/44821>\
**Category:** Newbie\
**Created:** [May 11, 2021, 7:24pm UTC](https://root-forum.cern.ch/t/sort-a-double-array/44821 "2021-05-11T19:24:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keyvan\_Tabaei](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/keyvan_tabaei/32/25908_2.png) [@Keyvan\_Tabaei](https://root-forum.cern.ch/u/Keyvan_Tabaei)\
**Post date:** [May 11, 2021, 7:24pm UTC](https://root-forum.cern.ch/t/sort-a-double-array/44821/1 "2021-05-11T19:24:24Z")

</div>

hi  
i have a double array with 1000000 elements(size).

how can i sort it in increasing order ?

i tried Bubble Sort algorithm,it crashed.  
thanks,

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 11, 2021, 7:58pm UTC](https://root-forum.cern.ch/t/sort-a-double-array/44821/2 "2021-05-11T19:58:44Z")

</div>

Maybe: [std::sort](http://www.cplusplus.com/reference/algorithm/sort/)

Assuming you have some ordinary “`double table[]`” with “`n = 1000000`” elements, you can simply use:  
`std::sort(table, table + n);`

---

<div class="post-metadata">

**Author:** ![maxo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@maxo](https://root-forum.cern.ch/u/maxo)\
**Post date:** [May 11, 2021, 8:03pm UTC](https://root-forum.cern.ch/t/sort-a-double-array/44821/3 "2021-05-11T20:03:00Z")

</div>

Hello,

If you are using C++, you can use `std::sort` from the `<algorithm>` header.

```cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main() { 
    std::vector<int> data;
    int size = 1'000'000;
    // reserve space for `size` elements
    data.reserve(size);
    // fill vector in decreasing order for example
    for (int i = 0; i < size; i++) { 
        data.push_back(size - i);
    }
    // before sorting, the first element is size
    // and the last element is 1
    std::cout << "-- before sorting:\n";
    std::cout << "first element: " << data.front() << "\n";
    std::cout << "last element: " << data.back() << "\n"; 
    std::sort(data.begin(), data.end());
    // after sorting, the first element is 1 and the last
    // element is size
    std::cout << "-- after sorting:\n";
    std::cout << "first element: " << data.front() << "\n";
    std::cout << "last element: " << data.back() << "\n"; 
}

```

You can try this example here: [Compiler Explorer](https://godbolt.org/z/4c6TTWEWr)

---

<div class="post-metadata">

**Author:** ![Keyvan\_Tabaei](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/keyvan_tabaei/32/25908_2.png) [@Keyvan\_Tabaei](https://root-forum.cern.ch/u/Keyvan_Tabaei)\
**Post date:** [May 11, 2021, 8:30pm UTC](https://root-forum.cern.ch/t/sort-a-double-array/44821/4 "2021-05-11T20:30:15Z")

</div>

it works

many thanks,
