Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // jd.cpp : Defines the entry point for the console application.
- //
- #include "pch.h"
- #include <iostream>
- #include <conio.h>
- #include <time.h>
- #include <cstdlib>
- #include <chrono>
- int table_size;
- void minmax1(int A[], int &j, int &k)
- {
- int i;
- int n = table_size;
- j = 0;
- for (i = 1; i < n; i++)
- {
- if (A[i] > A[j])
- {
- j = i;
- }
- }
- k = 0;
- for (i = 1; i < n; i++)
- {
- if (A[i] < A[k])
- {
- k = i;
- }
- }
- }
- void minmax2(int A[], int &j, int &k)
- {
- int n = table_size;
- int i;
- j = 0; k = 0;
- for (i = 1; i < n; i++)
- if (A[i] > A[j])
- j = i;
- else if (A[i] < A[k])
- k = i;
- }
- void minmax3(int A[], int &j, int &k)
- {
- int i, min, max;
- int n = table_size;
- if (A[0] > A[1])
- {
- k = 1;
- j = 0;
- }
- else
- {
- k = 0;
- j = 1;
- }
- i = 2;
- while (i < n - 1)
- {
- if (A[i] < A[i + 1])
- {
- min = i;
- max = i + 1;
- }
- else
- {
- min = i + 1;
- max = i;
- }
- if (A[min] < A[k])
- {
- k = min;
- }
- if (A[max] > A[j])
- {
- j = max;
- }
- i += 2;
- }
- if (i == n - 1)
- {
- if (A[n - 1] < A[k])
- {
- k = n - 1;
- }
- else
- {
- if (A[n - 1] < A[k])
- {
- j = n - 1;
- }
- }
- }
- }
- int pot(int x, int y)
- {
- int potega = 1;
- while (y != 0)
- {
- if (y % 2 == 1)
- {
- potega *= x;
- }
- y /= 2;
- x *= x;
- }
- return potega;
- }
- void wybieranie(int A[], int size)
- {
- int k, x;
- int n = table_size;
- for (int i = 0; i < n - 1; i++)
- {
- k = i;
- x = A[i];
- for (int j = i + 1; j < n; j++)
- {
- if (A[j] < x)
- {
- k = j;
- x = A[j];
- }
- A[k] = A[i];
- A[i] = x;
- }
- }
- }
- void wstawianie(int A[], int size)
- {
- int j, x;
- int n = table_size;
- for (int i = 1; i < n; i++)
- {
- x = A[i];
- j = i - 1;
- while (j >= 0 && x < A[j])
- {
- A[j + 1] = A[j];
- j--;
- }
- A[j + 1] = x;
- }
- }
- void zamiana(int &x, int &y)
- {
- int bufor;
- bufor = x;
- x = y;
- y = bufor;
- }
- void babelkowe(int A[], int size)
- {
- int n = table_size;
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = 0; j < n - i - 1; j++)
- {
- if (A[j] > A[j + 1])
- {
- zamiana(A[j], A[j + 1]);
- }
- }
- }
- }
- void dod(int **A, int size)
- {
- int i, j, k;
- int n = table_size;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < n; j++)
- {
- for (k = j; k < n; k++)
- {
- A[i][j] += A[i][k];
- }
- }
- }
- }
- using namespace std;
- int main()
- {
- int j = 0;
- int k = 0;
- double duration;
- srand(time(NULL));
- table_size = 10;
- int *A = new int[table_size];
- for (int i = 0; i < table_size; i++)
- {
- A[i] = rand() % 10000;
- }
- cout << "time\t\tminmax1\t\tminmax2\t\tminmax3" << endl;
- // N = 10
- cout << "A[10]";
- auto start = chrono::high_resolution_clock::now();
- minmax1(A, j, k);
- auto stop = chrono::high_resolution_clock::now();
- cout << "\t\t" <<chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax2(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax3(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- cout << endl;
- // N = 100
- table_size = 100;
- A = new int[table_size];
- for (int i = 0; i < table_size; i++)
- {
- A[i] = rand() % 10000;
- }
- cout << "A[100]";
- start = chrono::high_resolution_clock::now();
- minmax1(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax2(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax3(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- cout << endl;
- // N = 1000
- table_size = 1000;
- A = new int[table_size];
- for (int i = 0; i < table_size; i++)
- {
- A[i] = rand() % 10000;
- }
- cout << "A[1000]";
- start = chrono::high_resolution_clock::now();
- minmax1(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax2(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax3(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- cout << endl;
- // N = 10000
- table_size = 10000;
- A = new int[table_size];
- for (int i = 0; i < table_size; i++)
- {
- A[i] = rand() % 10000;
- }
- cout << "A[10000]";
- start = chrono::high_resolution_clock::now();
- minmax1(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax2(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- start = chrono::high_resolution_clock::now();
- minmax3(A, j, k);
- stop = chrono::high_resolution_clock::now();
- cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
- cout << endl;
- _getch();
- delete[] A;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement