### Ascending Order

I have created this program that creates a random array of 20 numbers and then shuffles them. The next thing I would like to implement is to be able to reprint the shuffled numbers into ascending order. This is the code I have so far, any ideas on how to do it?

using namespace std;

#include <iostream>

const int sizeOfArray = 20;
int randomArray[sizeOfArray];

void intialise();
void display(string message);
void shuffle();

int main()
{

intialise();

display("Intial Randomised numbers ");

shuffle();
display("Randomised numbers have been shuffled ");

return 0;

}

void intialise()
{
int max;

srand(time(0));

//cout << "Please select the min number you would like the random numbers to be between:";
//cin >> min;

cout << "Please select the max number you would like the random numbers to be between:";
cin >> max;

for (int loop = 0; loop < sizeOfArray; loop++)
{

randomArray[loop] = (rand() % max );

}

}

void display(string message)
{

cout << message << endl;

for (int loop = 0; loop < sizeOfArray; loop++)
{
cout << randomArray[loop] << " ";
}

cout << endl;

}

void shuffle()
{

int first, second, temp, temp2, i, o;

for (int loop = 0; loop < 20; loop++)
{
first = rand() % sizeOfArray;
second = rand() % sizeOfArray;

temp = randomArray[first];
randomArray[first] = randomArray[second];
randomArray[second] = temp;
}

}
https://www.cplusplus.com/articles/jEywvCM9/
you sort the data.
c++ contains a sort, std::sort() which will just do it for you and is usually the best choice.

c++ also contains std::swap(). You wrote that, and it would be smart to move it to its own function as you will need it in most sorting algorithms, esp the simple ones.

you can learn about some sorts here:
https://www.geeksforgeeks.org/sorting-algorithms/

my list of must-know sorts:
- bubble (getting started, its the simplest, and slowest, way to sort. You can skip it, but everyone feels compelled to teach it)
- insertion (slightly better than bubble, and equally easy to understand, and below)
- shellshort (very good, slightly modified insertion sort, easy to code, difficult to understand)
- counting sort (fastest but only works for specific problems. you can often force your data to fit the specific problem, though, with some creativity).
- introsort (current best approach in general: std::sort uses this one. It is advanced, hard to code correctly, hard to understand, uses recursion)

The rest you can poke around at if you are interested.
Last edited on
 PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/ HINT: you can edit your post and add code tags. Some formatting & indentation would not hurt either

You prompt the user to enter min and max values for your random numbers, but you don't use the min value when generating the sequence.

Shuffling randomly created numbers is a bit of overkill. Shuffling a container of sequential numbers ( 1, 2, 3, 4, 5, etc. ) makes more sense.

C++ has an algorithm for shuffling a container. It had two, but one (std::random_shuffle) was deprecated in C++14 and removed in C++17.

std::shuffle requires a C++ random engine.
https://en.cppreference.com/w/cpp/algorithm/random_shuffle

When writing new C++ code it is recommended you use the C++ facilities to generate random numbers. The C library has problems.

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Possibly:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; constexpr size_t sizeOfArray {20}; int randomArray[sizeOfArray] {}; void intialise(); void display(const string& message); std::mt19937 rng(std::random_device {}()); int main() { intialise(); display("\nInitial Random numbers"); std::shuffle(std::begin(randomArray), std::end(randomArray), rng); display("\nShuffled numbers"); std::sort(std::begin(randomArray), std::end(randomArray)); display("\nSorted numbers"); } void intialise() { size_t max { }; cout << "Please select the max number you would like the random numbers to be between: "; cin >> max; std::uniform_int_distribution distrib(0, max); for (auto& r : randomArray) r = distrib(rng); } void display(const string& message) { cout << message << '\n'; for (const auto& r : randomArray) cout << r << ' '; cout << '\n'; }``````

 ``` Please select the max number you would like the random numbers to be between: 100 Initial Random numbers 86 67 80 99 62 72 26 46 29 83 84 30 98 86 62 22 5 58 57 35 Shuffled numbers 5 26 84 98 22 62 57 99 72 29 83 80 46 67 62 35 86 58 86 30 Sorted numbers 5 22 26 29 30 35 46 57 58 62 62 67 72 80 83 84 86 86 98 99 ```

If'n we are going to use a C++ random engine and C++ functions why not use a C++ container instead of a regular array? A std::vector for instance.

Another C++ container, std::set or std::multiset, automatically sorts the elements so no need to do a separate sort on the vector. A set can't contain multiple elements that are the same value, a multiset can.

Another feature of C++ is being able to overload the stream operators so displaying a container is as easy as displaying Plain Old Data like a string or int.

Using reference parameters in functions eliminates the need for globals.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include #include #include std::vector Initialize(size_t, std::default_random_engine&); std::ostream& operator<<(std::ostream&, const std::vector&); std::ostream& operator<<(std::ostream&, const std::multiset&); int main() { constexpr size_t SIZE { 30 }; std::default_random_engine rng(std::random_device {}()); std::vector vec { Initialize(SIZE, rng) }; std::cout << "Original container:\n" << vec << "\n\n"; std::multiset v_set(vec.begin(), vec.end()); std::cout << "Sorted set container:\n" << v_set << "\n\n"; std::cout << "Original container again:\n" << vec << "\n\n"; std::shuffle(vec.begin(), vec.end(), rng); std::cout << "Shuffled container:\n" << vec << "\n\n"; std::sort(vec.begin(), vec.end()); std::cout << "Sorted container:\n" << vec << '\n'; } std::vector Initialize(size_t req_size, std::default_random_engine& rng) { std::vector vec(req_size); std::cout << "Minimum value for random numbers: "; int min; std::cin >> min; std::cout << "Maximum value for random numbers: "; int max; std::cin >> max; std::cout << '\n'; std::uniform_int_distribution dist(min, max); for (auto& itr : vec) { itr = dist(rng); } return vec; } std::ostream& operator<<(std::ostream& os, const std::vector& vec) { for (const auto& itr : vec) { std::cout << itr << ' '; } return os; } std::ostream& operator<<(std::ostream& os, const std::multiset& vec) { for (const auto& itr : vec) { std::cout << itr << ' '; } return os; }``````

 ```Minimum value for random numbers: 5 Maximum value for random numbers: 125 Original container: 18 64 120 27 67 8 29 95 15 111 72 18 16 68 33 92 34 82 28 37 95 54 68 32 84 112 125 105 94 89 Sorted set container: 8 15 16 18 18 27 28 29 32 33 34 37 54 64 67 68 68 72 82 84 89 92 94 95 95 105 111 112 120 125 Original container again: 18 64 120 27 67 8 29 95 15 111 72 18 16 68 33 92 34 82 28 37 95 54 68 32 84 112 125 105 94 89 Shuffled container: 68 29 33 89 34 105 54 32 27 72 16 67 92 18 84 95 125 120 28 111 82 64 18 94 68 15 37 95 8 112 Sorted container: 8 15 16 18 18 27 28 29 32 33 34 37 54 64 67 68 68 72 82 84 89 92 94 95 95 105 111 112 120 125```