-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.cpp
77 lines (68 loc) · 1.45 KB
/
bubblesort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubbleSort(double* arr, int n);
void swap(double *arr1el, double *arr2el);
void printArray(double* arr, int size);
double* getRandomValues(int n);
int main()
{
int n = 1000;
double *arr = getRandomValues(n);
bubbleSort(arr, n);
//printArray(arr, n);
//free(arr);
return 0;
}
double* getRandomValues(int n)
{
srand(time(0));
double* arr = (double*) calloc(n, sizeof(double));
if(arr == NULL)
{
printf("memory not allocated");
exit(0);
}
for(int i = 0; i < n ; i++)
*(arr + i) = rand() + 1;
return arr;
}
void bubbleSort(double* arr, int n)
{
int i, j;
bool swapped;
int change = 0;
int compare = 0;
for (i = 0; i < n - 1; i++)
{
swapped = false;
for (j = 0; j < n - i - 1; j++)
{
compare += 1;
if (*(arr + j) > *(arr + j + 1))
{
swap((arr + j), (arr + j + 1));
change += 1;
swapped = true;
}
}
if (swapped == false)
{
//printf(" %d %d", change, compare);
break;
}
}
printf(" %d %d", change, compare);
}
void swap(double *arr1el, double *arr2el)
{
double temp = *arr1el;
*arr1el = *arr2el;
*arr2el = temp;
}
void printArray(double* arr, int size)
{
int i;
for (i = 0; i < size; i++)
printf(" %0.0lf", *(arr + i));
}