Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cstdlib>
- #include "time.h"
- using namespace std;
- int arrSize;
- int *arr;
- int deli(int dno, int vrh);
- void izpisiZaporedje();
- void hitroUredi(int dno, int vrh);
- void generirajNakljucno();
- void generirajNarascajoce();
- void generirajPadajoce();
- void preveri(int dno, int vrh);
- void definirajZaporedje();
- int main()
- {
- clock_t start, finish;
- double duration;
- srand(time(NULL));
- while (true) {
- int izbira;
- cout <<
- "Hitro uredi - izbira\n\n" <<
- "1 Generiraj nakljucno zaporedje\n" <<
- "2 Generiraj urejeno narascajoce zaporedje\n" <<
- "3 Generiraj urejeno padajoče zaporedje\n" <<
- "4 Izpis zaporedja\n" <<
- "5 Uredi\n" <<
- "6 Konec\n" <<
- "Vasa izbira: ";
- cin >> izbira;
- cout << endl;
- switch (izbira) {
- case 1:
- definirajZaporedje();
- generirajNakljucno();
- break;
- case 2:
- definirajZaporedje();
- generirajNarascajoce();
- break;
- case 3:
- definirajZaporedje();
- generirajPadajoce();
- break;
- case 4:
- izpisiZaporedje();
- break;
- case 5:
- start = clock();
- hitroUredi(0, arrSize - 1);
- finish = clock();
- duration = (double)(finish - start);
- cout << "Cas izvajanja: " << duration << endl;
- preveri(0, arrSize - 1);
- break;
- case 6:
- return 1;
- default:
- cout << "Vpisi ponovno: ";
- }
- cout << "------------------------" << endl;
- }
- }
- void definirajZaporedje() {
- int size;
- cout << "Vnesi dolzino zaporedja: ";
- cin >> size;
- arrSize = size;
- int a = size;
- arr = new int[a];
- }
- void preveri(int dno, int vrh) {
- bool pravilnoUrejeno = true;
- for (int i = dno; i < vrh - 1; i++) {
- if (arr[i + 1] < arr[i]) {
- cout << arr[i + 1] << " " << arr[i] << endl;
- pravilnoUrejeno = false;
- }
- }
- if (pravilnoUrejeno) cout << "Zaporedje je pravilno urejeno" << endl << endl;
- else cout << "Zaporedje ni pravilno urejeno" << endl << endl;
- }
- void generirajNakljucno() {
- for (int i = 0; i < arrSize; i++)
- arr[i] = (rand() % 50000);
- }
- void generirajNarascajoce() {
- for (int i = 0; i < arrSize; i++)
- arr[i] = i;
- }
- void generirajPadajoce() {
- for (int i = arrSize; i >= 0; i--)
- arr[i] = arrSize - i;
- }
- void hitroUredi(int dno, int vrh) {
- if (dno < vrh) {
- int j = deli(dno, vrh);
- hitroUredi(dno, j - 1);
- hitroUredi(j + 1, vrh);
- }
- }
- int deli(int dno, int vrh) {
- int m = (dno + vrh) / 2;
- int tmpA = arr[dno];
- arr[dno] = arr[m];
- arr[m] = tmpA;
- int pe = arr[dno];
- int levi = dno + 1;
- int desni = vrh;
- while (true) {
- while (arr[levi] < pe && levi <= vrh) levi++;
- while (arr[desni] > pe && desni >= dno) desni--;
- if (desni <= levi) break;
- else {
- int tmpB = arr[levi];
- arr[levi] = arr[desni];
- arr[desni] = tmpB;
- levi++;
- desni--;
- }
- }
- int tmpC = arr[dno];
- arr[dno] = arr[desni];
- arr[desni] = tmpC;
- return desni;
- }
- void izpisiZaporedje() {
- for (int i = 0; i < arrSize; i++) {
- cout << arr[i] << " ";
- }
- cout << endl <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement