Sort a double array

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,

Maybe: std::sort

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

1 Like

Hello,

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

#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

1 Like

it works

many thanks,