Dateien nach „BSP_1_E“ hochladen

This commit is contained in:
Georg Thomas Mandl 2025-10-22 23:16:05 +02:00
commit 56192999bd
4 changed files with 252 additions and 0 deletions

42
BSP_1_E/bsp_1_e.cbp Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="bsp_1_e" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/bsp_1_e" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/bsp_1_e" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="bsp_1_e.cpp" />
<Unit filename="bsp_1_e.h" />
<Unit filename="main.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

107
BSP_1_E/bsp_1_e.cpp Normal file
View file

@ -0,0 +1,107 @@
#include "bsp_1_e.h"
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <ctime>
using namespace std;
vector<int> sortvec(const vector<int> &x, const vector<int> &y)
{
vector<int> result = x;
for(size_t k = 0; k<y.size(); ++k)
{
auto pos = lower_bound(result.begin(), result.end(), y[k]);
result.insert(pos, y[k]);
}
// check if sorted
if(is_sorted(result.begin(), result.end()) == 0)
{
cout << "FEHLER - nicht sortiert" << endl;
}
return result;
}
vector<int> genrandvec(const int &n)
{
vector<int> v;
srand((unsigned) time(NULL));
for(int k=0; k<n; ++k)
{
int random = 1 + (rand() % n);
v.push_back(random);
}
return v;
}
vector<int> gensortvec(const int & n)
{
vector<int> v;
for(int k = 0; k<n; ++k)
{
v.push_back(k+1);
}
return v;
}
list<int> sortlist(const list<int> &x, const list<int> &y)
{
list<int> result = x;
for(auto it = y.begin(); it != y.end(); ++it)
{
auto pos = lower_bound(result.begin(), result.end(), *it);
result.insert(pos, *it);
}
// check if sorted
if(is_sorted(result.begin(), result.end()) == 0)
{
cout << "FEHLER - nicht sortiert" << endl;
}
return result;
}
list<int> genrandlist(const int &n)
{
list<int> v;
srand((unsigned) time(NULL));
for(int k=0; k<n; ++k)
{
int random = 1 + (rand() % n);
v.push_back(random);
}
return v;
}
list<int> gensortlist(const int & n)
{
list<int> v;
for(int k = 0; k<n; ++k)
{
v.push_back(k+1);
}
return v;
}
list<int> genlistfromvec(const vector<int> &v)
{
list<int> a;
for(size_t k=0; k<v.size(); ++k)
{
a.push_back(v[k]);
}
return a;
}

67
BSP_1_E/bsp_1_e.h Normal file
View file

@ -0,0 +1,67 @@
#ifndef BSP_1_E_H_INCLUDED
#define BSP_1_E_H_INCLUDED
#include <vector>
#include <list>
/** \brief Ein zufällig generierter Vektor wird in einen aufsteigend sortierten Vektor eingegliedert,
* sodass der Ergebnisvektor wieder aufsteigend sortiert ist
*
* \param[in] x aufsteigend sortierter Vektor
* \param[in] y Zufallsvektor
* \return Vereinigung beider Vektoren, aufsteigend sortiert
*
*/
std::vector<int> sortvec(const std::vector<int> &x, const std::vector<int> &y);
/** \brief Generiert einen Zufallsvektor der Länge n mit Werten aus [1,n]
*
* \param[in] n Länge des gewünschten Vektors
* \return Zufallsvektor der Länge n
*
*/
std::vector<int> genrandvec(const int &n);
/** \brief Erstellt einen Vektor der Länge n mit den aufsteigen sortierten Werten 1,2,3,....,n
*
* \param[in] n Länge des Vektors
* \return Vektor [1,2,3,...,n]
*
*/
std::vector<int> gensortvec(const int & n);
/** \brief Eine zufällig generierte Liste wird in eine aufsteigend sortierte Liste eingegliedert,
* sodass die Ergebnisliste wieder aufsteigend sortiert ist
*
* \param[in] x aufsteigend sortierte Liste
* \param[in] y Zufallsliste
* \return Vereinigung beider Listen aufsteigend sortiert
*
*/
std::list<int> sortlist(const std::list<int> &x, const std::list<int> &y);
/** \brief Generiert eine Zufallsliste der Länge n mit Werten aus [1,n]
*
* \param[in] n Länge der gewünschten Liste
* \return Zufallsliste der Länge n
*
*/
std::list<int> genrandlist(const int &n);
/** \brief Erstellt eine Liste der Länge n mit den aufsteigen sortierten Werten 1,2,3,....,n
*
* \param[in] n Länge der Liste
* \return Liste [1,2,3,...,n]
*
*/
std::list<int> gensortlist(const int & n);
/** \brief Erstellt aus einem gegebenen Vektor eine Liste mit denselben Einträgen
*
* \param[in] v Vektor
* \return Liste
*
*/
std::list<int> genlistfromvec(const std::vector<int> &v);
#endif // BSP_1_E_H_INCLUDED

36
BSP_1_E/main.cpp Normal file
View file

@ -0,0 +1,36 @@
#include "bsp_1_e.h"
#include <iostream>
#include <chrono>
// BSP 1_E
using namespace std;
int main()
{
int n = 1e4;
// verwende nicht 1e6 und höher - braucht 100s und aufwärts (Vektor)
// für 1e5 benötigt die Liste recht lang, einige Minuten
// Vektoren
vector<int> x = gensortvec(n);
vector<int> y = genrandvec(n);
auto time1start = chrono::system_clock::now();
vector<int> s1 = sortvec(x,y);
auto time1end = chrono::system_clock::now();
auto time1 = chrono::duration_cast<chrono::milliseconds>(time1end - time1start);
cout << "Vektor, n = " << n << endl;
cout << "time needed: " << time1.count() << " ms" << endl << endl;
// Listen
list<int> a = gensortlist(n);
list<int> b = genlistfromvec(y);
auto time2start = chrono::system_clock::now();
list<int> s2 = sortlist(a,b);
auto time2end = chrono::system_clock::now();
auto time2 = chrono::duration_cast<chrono::milliseconds>(time2end - time2start);
cout << "Liste, n = " << n << endl;
cout << "time needed: " << time2.count() << " ms" << endl << endl;
return 0;
}