-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort.c
78 lines (69 loc) · 1.39 KB
/
bubblesort.c
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
78
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define MAX 100000
int *bubbleSort(int *arr, int);
void display(int *, int);
void swap(int *, int *);
void randomArray(int *, int);
int main()
{
int arr[MAX];
int n;
double start, end;
printf("Enter the size of array: ");
scanf("%d", &n);
// randomArray(arr, n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\tBefore sorting: \n");
display(arr, n);
printf("\n");
printf("\tAfter sorting: \n");
start = clock();
bubbleSort(arr, n);
end = clock();
display(arr, n);
printf("\n");
printf("\t The total time taken by the Bubble Sort is %f seconds for sorting of %d elements\n", (end - start) / CLOCKS_PER_SEC, n);
return 0;
}
int *bubbleSort(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
return arr;
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void display(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
void randomArray(int arr[], int n)
{
srand(time(NULL));
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 1000;
}
}