2017-04-02 15:59:33 +02:00
|
|
|
#include<iostream>
|
|
|
|
#include<conio.h>
|
|
|
|
#include<string>
|
|
|
|
#include<sstream>
|
2017-04-09 12:43:54 +02:00
|
|
|
#include<cstdlib>
|
|
|
|
#include<ctime>
|
2017-04-02 15:59:33 +02:00
|
|
|
|
2017-04-10 14:57:05 +02:00
|
|
|
void SelectionSort(long double *array, unsigned short amount);
|
|
|
|
void SelectionSort(int *array, unsigned short amount);
|
|
|
|
|
2017-04-02 15:59:33 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
std::string input;
|
2017-04-08 17:51:54 +02:00
|
|
|
unsigned short amount = 1;
|
2017-04-02 15:59:33 +02:00
|
|
|
|
2017-04-08 17:51:54 +02:00
|
|
|
std::cout << "Gib die Anzahl der Zahlen ein: ";
|
2017-04-02 15:59:33 +02:00
|
|
|
getline(std::cin, input);
|
|
|
|
std::stringstream(input) >> amount;
|
|
|
|
|
2017-04-09 12:43:54 +02:00
|
|
|
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
|
|
|
|
getline(std::cin, input);
|
|
|
|
if(input == "y" || input == "Y")
|
2017-04-02 15:59:33 +02:00
|
|
|
{
|
2017-04-10 14:57:05 +02:00
|
|
|
int *array = new int[amount];
|
2017-04-09 12:43:54 +02:00
|
|
|
srand(time(0));
|
2017-04-10 16:31:26 +02:00
|
|
|
for(unsigned short idx = 0; idx < amount; array[idx++] = rand()%1000);
|
2017-04-10 14:57:05 +02:00
|
|
|
SelectionSort(array, amount);
|
|
|
|
delete[] array;
|
2017-04-09 12:43:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-10 14:57:05 +02:00
|
|
|
long double *array = new long double[amount];
|
2017-04-10 16:31:26 +02:00
|
|
|
for(unsigned short idx = 0; idx < amount; std::stringstream(input) >> array[idx++])
|
2017-04-09 12:43:54 +02:00
|
|
|
{
|
|
|
|
std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
|
|
|
|
getline(std::cin,input);
|
|
|
|
}
|
2017-04-10 14:57:05 +02:00
|
|
|
SelectionSort(array, amount);
|
|
|
|
delete[] array;
|
2017-04-02 15:59:33 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 14:57:05 +02:00
|
|
|
getch();
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionSort(long double *array, unsigned short amount)
|
|
|
|
{
|
2017-04-02 15:59:33 +02:00
|
|
|
std::cout << "\n";
|
|
|
|
|
|
|
|
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
|
2017-04-05 13:12:55 +02:00
|
|
|
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
|
|
for(unsigned short idx = 0; idx<amount-1; idx++)
|
|
|
|
{
|
|
|
|
unsigned short indexMin = idx;
|
2017-04-05 13:12:55 +02:00
|
|
|
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
2017-04-02 15:59:33 +02:00
|
|
|
|
|
|
|
if(indexMin!=idx)
|
|
|
|
{
|
|
|
|
long double temp = array[idx];
|
2017-04-10 16:31:26 +02:00
|
|
|
array[idx] = array[indexMin], array[indexMin] = temp;
|
2017-04-02 15:59:33 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:31:26 +02:00
|
|
|
for(unsigned short idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
2017-04-05 13:12:55 +02:00
|
|
|
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
2017-04-02 15:59:33 +02:00
|
|
|
}
|
2017-04-10 14:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionSort(int *array, unsigned short amount)
|
|
|
|
{
|
|
|
|
std::cout << "\n";
|
2017-04-02 15:59:33 +02:00
|
|
|
|
2017-04-10 14:57:05 +02:00
|
|
|
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 = 0; idx<amount-1; idx++)
|
|
|
|
{
|
|
|
|
unsigned short indexMin = idx;
|
|
|
|
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
|
|
|
|
|
|
|
|
if(indexMin!=idx)
|
|
|
|
{
|
|
|
|
long double temp = array[idx];
|
2017-04-10 16:31:26 +02:00
|
|
|
array[idx] = array[indexMin], array[indexMin] = temp;
|
2017-04-10 14:57:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:31:26 +02:00
|
|
|
for(unsigned short idx2 = 0; idx2<amount-1; idx2++) std::cout << array[idx2] << ", ";
|
2017-04-10 14:57:05 +02:00
|
|
|
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
|
|
|
|
}
|
2017-04-02 15:59:33 +02:00
|
|
|
}
|