### sorting numbers

Hello all,

Is there any way to sort five inputed numbers by user in ascending/descending order, not using five differenf for loops and if statements for each number?

I did my coding in my native language, so i am not sure if it would make any sense.

Of course.

No matter how many numbers you have, this implementation
of selection sort requires two for loops and two if statements:

 ``12345678910111213141516171819202122232425`` ``````/* a[0] to a[n-1] is the array to sort */ int i,j; int iMin; /* advance the position through the entire array */ /* (could do j < n-1 because single element is also min element) */ for (j = 0; j < n-1; j++) { /* find the min element in the unsorted a[j .. n-1] */ /* assume the min is the first element */ iMin = j; /* test against elements after j to find the smallest */ for ( i = j+1; i < n; i++) { /* if this element is less, then it is the new minimum */ if (a[i] < a[iMin]) { /* found new minimum; remember its index */ iMin = i; } } if(iMin != j) { swap(a[j], a[iMin]); } }``````

https://en.wikipedia.org/wiki/Selection_sort#Implementation
Last edited on
 ``123456789101112131415161718192021`` ``````#include #include #include int main() { int array[5] = {1, 3, 5, 4, 2}; //By default sort sorts in ascending order std::sort(std::begin(array), std::end(array)); for(int i : array) std::cout << i << ' '; std::cout << '\n'; //Providing a comparator to sort in descending order std::sort(std::begin(array), std::end(array), std::greater<>()); for(int i : array) std::cout << i << ' '; std::cout << '\n'; }``````
thanks for the answer, i've ended up with my version and I have few questions about it's logic

What does line for (int i = 1; i < pan_array - 1; i++) do? I see that it generates numbers from 1 to 10 in the loop, but i cant see the purpose of it and what value it gets? (is it just how many times will loop run?)

Where does a value of the lowest_num is storred when for loop is ran once?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include using namespace std; int main() { const int pan_array = 11; int elements[pan_array] = {0}; int lowest_num; cout << "iveskite kiek blynu pusryciams suvalge kiekvienas is 10 zmoniu \n" << endl; for (int array_index = 1; array_index < pan_array; array_index++) { cout << array_index << " - zmogus suvalge" << endl; cin >> elements[array_index]; } for (int i = 1; i < pan_array - 1; i++) { for (int j = 1; j < pan_array; j++) { if(elements[j] < elements[j + 1]) { lowest_num = elements[j]; elements[j] = elements[j + 1]; elements[j + 1] = lowest_num; } } } cout << "mazejancia tvarka \n" << endl; for (int i = 1; i < pan_array; i++) { cout << elements[i] << endl; } return 0; }``````
Last edited on
I see you decided to use bubble sort instead. ( https://en.wikipedia.org/wiki/Bubble_sort )

 What does line for (int i = 1; i < pan_array - 1; i++) do?

It makes sure that the inner loop (let's call it a pass, following Wikipedia's conventions) executes
enough times for the array to be sorted. A better approach is to additionally check whether a pass
changes the array and break prematurely if it doesn't (see the Wikipedia implementations).

 Where does a value of the lowest_num is storred when for loop is ran once?

I don't understand what you're asking here.

BTW, while it looks conceptually correct, there are unusual conventions
(array indexing starts at 1 instead of 0) as well as off-by-one errors
(e.g. elements[j + 1] when j == pan_array - 1) in your code.
Topic archived. No new replies allowed.