Created
March 20, 2013 21:03
-
-
Save klemenzarn/5208408 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
int stElementov = 0; | |
int vrednosti = 0; | |
void napolniPolje(int polje[], int stElementov, int vrednosti){ | |
srand((unsigned)time(0)); | |
for(int i = 0; i < stElementov; i++){ | |
int randSt = rand()% vrednosti; | |
polje[i] = randSt; | |
} | |
} | |
void initPolje(int polje[], int stElementov){ | |
for(int i = 0; i < stElementov; i++){ | |
polje[i] = 0; | |
} | |
} | |
void izpisiPolje(int polje[], int stElementov){ | |
for(int i = 0; i < stElementov; i++){ | |
cout << polje[i] << " "; | |
} | |
} | |
void komulativno(int polje[], int stElementov){ | |
for(int i = 0; i < stElementov - 1; i++){ | |
polje[i + 1] = polje[i] + polje[i+1]; | |
} | |
} | |
void dobiPoljeStevcev(int poljeVrednosti[], int poljeStevcev[], int stElementov){ | |
initPolje(poljeStevcev, stElementov); | |
for(int i = 0; i < stElementov; i++){ | |
poljeStevcev[poljeVrednosti[i]]++; //Shranjujemo števce | |
} | |
} | |
void countingSort(int poljeStevcev[], int urejenoPolje[], int poljeVrednosti[], int stElementov){ | |
for(int i = stElementov - 1; i >= 0; i--){ | |
int j = poljeVrednosti[i]; | |
poljeStevcev[j] = poljeStevcev[j] - 1; | |
int k = poljeStevcev[j]; | |
urejenoPolje[k] = poljeVrednosti[i]; | |
} | |
} | |
int main(){ | |
int izbira = 0; | |
do{ | |
cout << "Izberi 1 od moznosti" << endl; | |
cout << "1 - Polje velikosti 1000 - Vrednosti 0 - 100" << endl; | |
cout << "2 - Polje velikosti 10000 - Vrednosti 0 - 1000" << endl; | |
cout << "3 - Polje velikosti 100000 - Vrednosti 0 - 10000" << endl; | |
cout << "4 - Zapri" << endl; | |
cout << "izbira: "; | |
cin >>izbira; | |
switch(izbira){ | |
case 1: | |
{ | |
stElementov = 1000; | |
vrednosti = 100; | |
int *poljeVrednosti = new int[stElementov]; | |
int *poljeStevcev = new int[stElementov]; | |
int *urejenoPolje = new int[stElementov]; | |
napolniPolje(poljeVrednosti, stElementov, vrednosti); | |
izpisiPolje(poljeVrednosti, stElementov); | |
cout << endl << "Polje stevcev: "; | |
dobiPoljeStevcev(poljeVrednosti, poljeStevcev, stElementov); | |
izpisiPolje(poljeStevcev, stElementov); | |
cout << endl; | |
komulativno(poljeStevcev, stElementov); | |
//izpisiPolje(poljeStevcev, stElementov); | |
cout << "Urejeno polje: "; | |
countingSort(poljeStevcev, urejenoPolje, poljeVrednosti, stElementov); | |
izpisiPolje(urejenoPolje, stElementov); | |
cout << endl; | |
break; | |
} | |
case 2: | |
{ | |
stElementov = 10000; | |
vrednosti = 1000; | |
int *poljeVrednosti = new int[stElementov]; | |
int *poljeStevcev = new int[stElementov]; | |
int *urejenoPolje = new int[stElementov]; | |
napolniPolje(poljeVrednosti, stElementov, vrednosti); | |
izpisiPolje(poljeVrednosti, stElementov); | |
cout << endl << "Polje stevcev: "; | |
dobiPoljeStevcev(poljeVrednosti, poljeStevcev, stElementov); | |
izpisiPolje(poljeStevcev, stElementov); | |
cout << endl; | |
komulativno(poljeStevcev, stElementov); | |
//izpisiPolje(poljeStevcev, stElementov); | |
cout << "Urejeno polje: "; | |
countingSort(poljeStevcev, urejenoPolje, poljeVrednosti, stElementov); | |
izpisiPolje(urejenoPolje, stElementov); | |
break; | |
} | |
case 3: | |
{ | |
stElementov = 100000; | |
vrednosti = 10000; | |
int *poljeVrednosti = new int[stElementov]; | |
int *poljeStevcev = new int[stElementov]; | |
int *urejenoPolje = new int[stElementov]; | |
napolniPolje(poljeVrednosti, stElementov, vrednosti); | |
izpisiPolje(poljeVrednosti, stElementov); | |
cout << endl << "Polje stevcev: "; | |
dobiPoljeStevcev(poljeVrednosti, poljeStevcev, stElementov); | |
izpisiPolje(poljeStevcev, stElementov); | |
cout << endl; | |
komulativno(poljeStevcev, stElementov); | |
//izpisiPolje(poljeStevcev, stElementov); | |
cout << "Urejeno polje: "; | |
countingSort(poljeStevcev, urejenoPolje, poljeVrednosti, stElementov); | |
izpisiPolje(urejenoPolje, stElementov); | |
break; | |
} | |
} | |
}while(izbira != 4); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment