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

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.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <random>
#include <vector>
#include <set>
#include <algorithm>

std::vector<int> Initialize(size_t, std::default_random_engine&);
std::ostream& operator<<(std::ostream&, const std::vector<int>&);
std::ostream& operator<<(std::ostream&, const std::multiset<int>&);

int main()
{
   constexpr size_t SIZE { 30 };

   std::default_random_engine rng(std::random_device {}());

   std::vector<int> vec { Initialize(SIZE, rng) };

   std::cout << "Original container:\n" << vec << "\n\n";

   std::multiset<int> 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<int> Initialize(size_t req_size, std::default_random_engine& rng)
{
   std::vector<int> 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<int> dist(min, max);

   for (auto& itr : vec)
   {
      itr = dist(rng);
   }

   return vec;
}

std::ostream& operator<<(std::ostream& os, const std::vector<int>& vec)
{
   for (const auto& itr : vec)
   {
      std::cout << itr << ' ';
   }
   return os;
}

std::ostream& operator<<(std::ostream& os, const std::multiset<int>& 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
Registered users can post here. Sign in or register to post.