-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode1
More file actions
92 lines (76 loc) · 2.28 KB
/
Copy pathcode1
File metadata and controls
92 lines (76 loc) · 2.28 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <time.h> // for clock() and CLOCKS_PER_SEC
// Function for ascending bubble sort
void bubbleSortAsc(int arr[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Function for descending bubble sort
void bubbleSortDesc(int arr[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
clock_t start, end;
start = clock(); // Start measuring time
int n, i, median;
int data[100], min_list[100], max_list[100];
int min_count = 0, max_count = 0;
printf("Enter total number of data entries: ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid input. Number of entries must be positive.\n");
return 0;
}
// Input all data values
for (i = 0; i < n; i++) {
printf("Enter value %d: ", i + 1);
scanf("%d", &data[i]);
}
// First value as median
median = data[0];
// Separate values into min_list and max_list
for (i = 1; i < n; i++) {
if (data[i] < median)
min_list[min_count++] = data[i];
else if (data[i] > median)
max_list[max_count++] = data[i];
}
// Sort entire dataset
int all_data[100];
for (i = 0; i < n; i++) {
all_data[i] = data[i];
}
// Ascending and Descending sorting for full dataset
bubbleSortAsc(all_data, n);
printf("\n--- RESULTS ---\n");
printf("Median Value: %d\n", median);
printf("\nAll values in Ascending order: ");
for (i = 0; i < n; i++)
printf("%d ", all_data[i]);
bubbleSortDesc(all_data, n);
printf("\nAll values in Descending order: ");
for (i = 0; i < n; i++)
printf("%d ", all_data[i]);
// End time measurement
end = clock();
double total_time = (double)(end - start) / CLOCKS_PER_SEC;
printf("\n\nTotal execution time: %.6f seconds\n", total_time);
return 0;
}