-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada1.c
53 lines (52 loc) · 1.33 KB
/
ada1.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void selectionSort(int arr[], int n)
{
int temp;
for (int i = 0; i < n - 2; i++)
{
int min_idx = i;
for (int j = i + 1; j < n-1; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
void generateRandomArray(int arr[], int n)
{
srand(time(NULL));
for (int i = 0; i < n; i++)
{
arr[i] = rand();
}
}
int main()
{
int n_values[] = {5000, 10000, 15000, 20000}; // Array of n values
int num_values = sizeof(n_values) / sizeof(n_values[0]);
double time_taken[num_values];
for (int i = 0; i < num_values; i++)
{
int n = n_values[i];
int *arr = (int*)malloc(n * sizeof(int));
generateRandomArray(arr, n);
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
time_taken[i] = ((double)(end - start)) / CLOCKS_PER_SEC;
free(arr);
}
printf("n\tTime Taken (s)\n");
for (int i = 0; i < num_values; i++)
{
printf("%d\t%f\n", n_values[i], time_taken[i]);
}
return 0;
}