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/
Please edit and format your code for readability.
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:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <algorithm>
#include <random>

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<size_t> 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

Topic archived. No new replies allowed.