Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <conio.h>
- #include <cstdlib>
- using namespace std;
- #define N 100000
- int randomazer (int min, int max) {
- float random;
- random = rand();
- random = (random / RAND_MAX) * (max - min) + min;
- return((int)random);
- }
- void insertion(int* A) {
- int j, i, ii;
- for (j = 0; j <= N; j++) {
- i = 1;
- while (i < N) {
- if (A[i] >= A[i - 1]) {
- i++;
- }
- else {
- while (A[i] < A[i - 1]) {
- ii = A[i - 1];
- A[i - 1] = A[i];
- A[i] = ii;
- i--;
- }
- i = N;
- }
- }
- }
- }
- void swap(int* a1, int* a2) {
- int tmp = *a1;
- *a1 = *a2;
- *a2 = tmp;
- }
- void Max_Heapify(int* A, int i, int n) {
- int l, r, largest = i;
- l = 2 * i + 1;
- r = 2 * i + 2;
- if ((l < n) && (A[l] > A[largest])) {
- largest = l;
- }
- else {
- largest = i;
- }
- if ((r < n) && (A[r] > A[largest])) {
- largest = r;
- }
- if (largest != i) {
- swap(&A[i], &A[largest]);
- Max_Heapify(A, largest, n);
- }
- }
- void Build_Max_Heap(int* A, int n) {
- int i;
- for (i = N / 2 - 1; i >= 0; i--) {
- Max_Heapify(A, i, n);
- }
- }
- void Heapsort(int* A, int n) {
- int i;
- Build_Max_Heap(A, n);
- for (i = N - 1; i >= 0; i--) {
- swap(&A[0], &A[i]);
- Max_Heapify(A, 0, i);
- }
- }
- void input_console (int* A) {
- int i;
- if (N < 100) {
- for (i = 0; i < N; i++) {
- cout << A[i] << " ";
- }
- cout << "\n";
- }
- }
- int main() {
- int key1, key2;
- int i;
- int A[N+1];
- int B[N+1]; //because we change array A in 1 sort
- cout << "Please input 0 if you want input numbers by yourself, and smth else number if not \n";
- cin >> key1;
- if (key1 == 0) {
- cout << "input numbers: ";
- for (i = 0; i < N; i++) {
- cin >> A[i];
- B[i] = A[i];
- }
- } else {
- for (i = 0; i < N; i++) {
- A[i] = randomazer(1, 10);
- B[i] = A[i];
- }
- }
- input_console(A);
- int marker = 1;
- while (marker) {
- cout << "Which algoritm do you want?\n";
- cout << "1.Insertion sorting\n";
- cout << "2.Pyramidal sorting\n";
- cout << "0.Exit\n";
- cout << "Please choose number: ";
- cin >> key2;
- clock_t start = clock();
- if (key2 == 0) {
- marker = 0;
- }
- if (key2 == 1) {
- insertion(A);
- }
- else {
- Heapsort(B, N);
- }
- if (marker != 0){
- clock_t end = clock();
- double total_time = (double(end) - double(start)) / CLOCKS_PER_SEC;
- input_console(A);
- cout << "Time of work: ";
- cout << total_time << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement