Random Numbers won't sorted correctly

Hello guys,

thats my program source code. The program works with random numbers.

You have 3 variables, types of int, and those variable have the function for "lo" (low) - lowest number
"hi" (high) - hightest number
"elem" (elements) - count of random numbers which will be printed in the end

In my case the program won't be sorted correctly.
If i enter for "lo" 10 and for "high" 100 and for "elem" 5, i often get values likes: 5 21 78 51 58

But it should be: 5 21 51 58 78

What's wrong in the code?




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
76
77
78
79
80
  #include <iostream>
#include <functional>
#include <random>
#include <vector>
#include <chrono> //Zeitmessung 

using namespace std;

using chrono::high_resolution_clock; //Zeitmessung  
using std::chrono::milliseconds;
using std::chrono::duration_cast;

/*
 * 
 */


void swap_if(int& a, int& b) {

    if (b < a) swap(a, b);

}

void insSort_v0(vector<int>& vi, const unsigned int& ui, const unsigned int& oi) {

    for (unsigned int i{ui + 1U}; i <= oi; ++i)

        for (unsigned int j{i}; ui < j; --j)

            swap_if(vi.at(j - 1U), vi.at(j));
}

/*void check(vector<int>& z) 
{
        for (unsigned int a{0u};a < z.size() ;++a )
          {
        if (z.at(0) > z.at(a))
        {
            throw runtime_error ("Daten wurden falsch sortiert!");           
        }      
    }   
}*/

int main() {


    vector <int> z{};
    int lo{}; //kleinster Wert
    int hi{}; //höchster Wert
    int elem{};


    cout << "Geben Sie einen kleinen Wert und einen großen Wert an: " << endl;
    cin >> lo >> hi;

    auto ri = std::bind(std::uniform_int_distribution<int>{lo, hi}, std::default_random_engine{});
    
    cout << "Geben Sie die Anzahl der Werte an, die Sie als Elemente haben moechten: " << endl;
    cin >> elem;
    
    for (unsigned i{0U}; i < elem; ++i) {

        z.push_back(ri(lo, hi));
    }

    insSort_v0(z, z.at(0), z.size() - 1);

    for (unsigned int i{0u}; i < z.size(); ++i) {

        cout << z.at(i) << " ";
    }







    return 0;
};
You know you're not being charged for every letter you use, right? If you want people to be able to read your code (which you do), use variable names that help people understand what you're trying to do.

Anyway, your function insSort_v0 doesn't make much sense. The second parameter, ui . What's that for? It looks like you don't sort the whole vector. You only sort some of it, based on the value of ui. If ui is big, you sort nothing at all.

Sorry, but my prof set this quellcode for us students and i don't know how to going on.
You didn't write any of this?
just a few sentences
*deleted
Last edited on
Thanks a lot @nuderobmonkey!

I'll deal with the code to understand how it works. Thank you!
Topic archived. No new replies allowed.