Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define e 0.000001
- int comp = 0; //количество сравнений
- int mov = 0; //количество перемещений
- void swap (double *a, double *b) {
- double t = *a;
- *a = *b;
- *b = t;
- }
- double absd (double a) { //модуль от double
- if (a < -e)
- return -a;
- return a;
- }
- void gen (int n, int type, double **p1, double **p2) { //генератор элементов по типу
- double *t1 = *p1;
- double *t2 = *p2;
- long long ran = rand();
- if (type == 1)
- for (int i = 1; i <= n; i++) {
- t1++;
- t2++;
- *t1 = i / ran;
- *t2 = i / ran;
- }
- else if (type == 2)
- for (int i = n; i > 0; i--) {
- t1--;
- t2--;
- *t1 = i / ran;
- *t2 = i / ran;
- }
- else
- for (int i = 0; i < n; i++) {
- t1++;
- t2++;
- *t1 = (double) (rand() * rand() * rand() * rand() * rand()) / 1000000;
- *t1 *= (double) rand();
- *t2 = *t1;
- }
- }
- void bubble (double *a, int n) { //пузырьковая сортировка
- for (int i = 0; i < n; i++) {
- for (int j = i; j < n - 1; j++) {
- comp++;
- if (absd(a[j]) > absd(a[i])) {
- mov++;
- swap(&a[j], &a[i]);
- }
- }
- }
- }
- void heapify(double *a, int i, int n) { //просеивание элемента
- int root = i;
- int l = 2 * i + 1;
- int r = 2 * i + 2;
- comp++;
- if (l < n && absd(a[l]) < absd(a[root])) {
- root = l;
- }
- comp++;
- if (r < n && absd(a[r]) < absd(a[root])) {
- root = r;
- }
- if (root != i) {
- mov++;
- swap(&a[i], &a[root]);
- heapify(a, root, n);
- }
- }
- void build_heap(double *a, int n) { //построение кучи
- for (int i = n / 2; i >= 0; i--)
- heapify(a, i, n);
- }
- void heap_sort(double *a, int n) { //пирамидальная сортировка
- build_heap(a, n);
- int k = n - 1;
- for (int i = k; i > 0; i--) {
- mov++;
- swap(&a[i], &a[0]);
- n--;
- heapify(a, 0, n);
- }
- }
- void check_sort(double *a, int n) {
- int flag = 1;
- for(int i = 1; i < n; i++) {
- if(a[i] - a[i-1] > e)
- }
- }
- int main(void) {
- int n, type;
- scanf("%d%d", &n, &type);
- double *a1;
- double *a2;
- a1 = (double *)malloc(n * sizeof(double) + 10);
- a2 = (double *)malloc(n * sizeof(double) + 10);
- gen(n, type, &a1, &a2);
- bubble(a1, n);
- printf("%d %d\n", comp, mov);
- comp = 0;
- mov = 0;
- heap_sort(a2, n);
- printf("%d %d\n", comp, mov);
- for (int i = 0; i < n; i++)
- printf("%lf ", a1[i]);
- printf("\n");
- for (int i = 0; i < n; i++)
- printf("%lf ", a2[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement