+ 3

C++ generate and sort numbers from lowest to highest.

Hello guys, i was trying to create a program that will generate and sort numbers from lowest to highest... But my skills probably sucks... so I am asking for a help now. My problem wasn't to generate, but to sort the numbers. I tried to use arrays but it didn't work as i expected. :/ Thanks for any help you'll provide! :)

16th Mar 2018, 9:01 PM
Tomáš Staroň
Tomáš Staroň - avatar
4 Respuestas
+ 3
Sorry but this is not what i expected Jayden LeCorps. My Program makes this: It asks you for the count of random numbers and the highest possible number. Then it generates it and sort it (lowest to highest). But the sorting is the problem. :/ Have you ever tried anything like that?
16th Mar 2018, 9:23 PM
Tomáš Staroň
Tomáš Staroň - avatar
+ 3
There are quite a number of sorting algorithms, but it sounds like you're new to this so I'll highlight one of the simplest to implement. What you want to do is to firstly compare the first 2 elements in your array, then swap them if the 1st element is bigger. Move on to the next two elements and repeat until you get to the end. You'll have to go through the array multiple times until the whole array is sorted. That is the algorithm, I'll leave the details for you to think about (don't wanna take all the fun away), for instance how you determine when the array is fully sorted.
16th Mar 2018, 10:17 PM
Jiren The Grey
Jiren The Grey - avatar
+ 3
Writing sorting algorithms is cool, but for practice, I'd use std::sort of <algorithm>. Have much fun!
16th Mar 2018, 11:17 PM
Timon Paßlick
+ 2
Alright. Thank you very much! :) And btw yea i am new in programming.
16th Mar 2018, 10:19 PM
Tomáš Staroň
Tomáš Staroň - avatar