dynamischer Speicher
This commit is contained in:
parent
8632dd6db9
commit
e583c9ff83
1 changed files with 50 additions and 23 deletions
|
@ -5,6 +5,9 @@
|
||||||
#include<cstdlib>
|
#include<cstdlib>
|
||||||
#include<ctime>
|
#include<ctime>
|
||||||
|
|
||||||
|
void InsertionSort(long double *ptr, unsigned short amount);
|
||||||
|
void InsertionSort(int *ptr, unsigned short amount);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::string input;
|
std::string input;
|
||||||
|
@ -14,45 +17,69 @@ int main()
|
||||||
getline(std::cin, input);
|
getline(std::cin, input);
|
||||||
std::stringstream(input) >> amount;
|
std::stringstream(input) >> amount;
|
||||||
|
|
||||||
long double array[amount];
|
|
||||||
|
|
||||||
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
||||||
getline(std::cin, input);
|
getline(std::cin, input);
|
||||||
if(input == "y" || input == "Y")
|
if(input == "y" || input == "Y")
|
||||||
{
|
{
|
||||||
|
int *array = new int[amount];
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
for(unsigned short idx = 0; idx < amount; idx++)
|
for(unsigned short idx = 0; idx < amount; idx++) array[idx] = rand()%100;
|
||||||
{
|
InsertionSort(array, amount);
|
||||||
array[idx] = rand()%100;
|
delete[] array;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
long double *array = new long double[amount];
|
||||||
for(unsigned short idx = 0; idx < amount; idx++)
|
for(unsigned short idx = 0; idx < amount; idx++)
|
||||||
{
|
{
|
||||||
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
||||||
getline(std::cin,input);
|
getline(std::cin,input);
|
||||||
std::stringstream(input) >> array[idx];
|
std::stringstream(input) >> array[idx];
|
||||||
}
|
}
|
||||||
}
|
InsertionSort(array, amount);
|
||||||
|
delete[] array;
|
||||||
std::cout << "\n";
|
|
||||||
|
|
||||||
for(unsigned short idx = 0; idx<amount-1; idx++) std::cout << array[idx] <<", ";
|
|
||||||
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
|
||||||
|
|
||||||
for(unsigned short idx = 1; idx < amount; idx++)
|
|
||||||
{
|
|
||||||
int temp = array[idx];
|
|
||||||
int idx2 = idx - 1;
|
|
||||||
|
|
||||||
while(idx2>=0 && array[idx2]>temp) array[idx2+1] = array[idx2--];
|
|
||||||
array[idx2+1] = temp;
|
|
||||||
|
|
||||||
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
|
|
||||||
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getch();
|
getch();
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InsertionSort(long double *ptr, unsigned short amount)
|
||||||
|
{
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
for(unsigned short idx = 0; idx<amount-1; idx++) std::cout << ptr[idx] <<", ";
|
||||||
|
std::cout << ptr[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||||
|
|
||||||
|
for(unsigned short idx = 1; idx < amount; idx++)
|
||||||
|
{
|
||||||
|
int temp = ptr[idx];
|
||||||
|
int idx2 = idx - 1;
|
||||||
|
|
||||||
|
while(idx2>=0 && ptr[idx2]>temp) ptr[idx2+1] = ptr[idx2--];
|
||||||
|
ptr[idx2+1] = temp;
|
||||||
|
|
||||||
|
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << ptr[idx] << ", ";
|
||||||
|
std::cout << ptr[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InsertionSort(int *ptr, unsigned short amount)
|
||||||
|
{
|
||||||
|
std::cout << "\n";
|
||||||
|
|
||||||
|
for(unsigned short idx = 0; idx<amount-1; idx++) std::cout << ptr[idx] <<", ";
|
||||||
|
std::cout << ptr[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
||||||
|
|
||||||
|
for(unsigned short idx = 1; idx < amount; idx++)
|
||||||
|
{
|
||||||
|
int temp = ptr[idx];
|
||||||
|
int idx2 = idx - 1;
|
||||||
|
|
||||||
|
while(idx2>=0 && ptr[idx2]>temp) ptr[idx2+1] = ptr[idx2--];
|
||||||
|
ptr[idx2+1] = temp;
|
||||||
|
|
||||||
|
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << ptr[idx] << ", ";
|
||||||
|
std::cout << ptr[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue