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 = 10;
- // int arr[arrSize];
- int deli(int *arr[], int dno, int vrh);
- void izpisiZaporedje(int *arr[], int velikost);
- void hitroUredi(int *arr[], int dno, int vrh);
- void generirajNakljucno(int *arr[], int velikost);
- void generirajNarascajoce(int *arr[], int velikost);
- void generirajPadajoce(int *arr[], int velikost);
- void preveri(int *arr[], int velikost);
- int main()
- {
- clock_t start, finish;
- double duration;
- srand(time(NULL));
- int size = 0;
- cout << "Vnesi velikost polja: " << endl;
- cin >> size;
- int *arrStevil = new int[size];
- 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;
- switch (izbira) {
- case 1:
- generirajNakljucno(&arrStevil, size);
- break;
- case 2:
- generirajNarascajoce(&arrStevil, size);
- break;
- case 3:
- generirajPadajoce(&arrStevil, size);
- break;
- case 4:
- izpisiZaporedje(&arrStevil, size);
- break;
- case 5:
- start = clock();
- hitroUredi(&arrStevil, 0, size - 1);
- finish = clock();
- duration = (double)(finish - start);
- cout << "Cas izvajanja: " << duration << endl;
- preveri(&arrStevil, size);
- break;
- case 6:
- return 1;
- default:
- cout << "Vpisi ponovno: ";
- }
- }
- }
- void preveri(int *arr[], int velikost) {
- bool pravilnoUrejeno = true;
- for (int i = 0; i < velikost - 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;
- else cout << "Zaporedje ni pravilno urejeno" << endl;
- }
- void generirajNakljucno(int *arr[], int velikost) {
- for (int i = 0; i < velikost; i++)
- *arr[i] = (rand() % 50000);
- }
- void generirajNarascajoce(int *arr[], int velikost) {
- for (int i = 0; i < velikost; i++)
- *arr[i] = i;
- }
- void generirajPadajoce(int *arr[], int velikost) {
- for (int i = velikost; i >= 0; i--)
- *arr[i] = velikost - i;
- }
- void hitroUredi(int *arr[], int dno, int vrh) {
- if (dno < vrh) {
- int j = deli(arr, dno, vrh);
- hitroUredi(arr, dno, j - 1);
- hitroUredi(arr, j + 1, vrh);
- }
- }
- int deli(int *arr[], 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(int *arr[], int velikost) {
- for (int i = 0; i < velikost; i++) {
- cout << arr[i] << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement