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
#include <iostream>
#include <functional>
#include <random>
#include <vector>
#include <chrono> //Zeitmessung
usingnamespace 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, constunsignedint& ui, constunsignedint& oi) {
for (unsignedint i{ui + 1U}; i <= oi; ++i)
for (unsignedint 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 (unsignedint 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.