-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternate_time_optimal.c
More file actions
124 lines (116 loc) · 3.32 KB
/
alternate_time_optimal.c
File metadata and controls
124 lines (116 loc) · 3.32 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Required libraries
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
//Structure for arguments
typedef struct{
int *arr;
int n;
int center;
}Arguments;
// Utility function to print the array
void printArray(int *arr, int n){
for(int i = 0; i < n;i ++){
printf("%d ",arr[i]);
}
printf("\n");
}
// Utility function to swap two numbers
void swap(int *arr, int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Utility function to find the minimum of two numbers
int minimum(int a,int b){
if(a < b){
return a;
}
else{
return b;
}
}
// Utility function to find the maximum of two numbers
int maximum(int a,int b){
if(a > b){
return a;
}
else{
return b;
}
}
// Comparison function used while performing an individual process
void *compare(void *arg){
Arguments *args = (Arguments*)arg;
// edge case
if(args->center - 1 < 0){
if(args->arr[args->center] > args->arr[args->center + 1]){
swap(args->arr, args->center, args->center + 1);
}
}
else if(args->center + 1 >= args->n){
if(args->arr[args->center] < args->arr[args->center - 1]){
swap(args->arr, args->center, args->center - 1);
}
}
// non - edge case
else{
int minValue = minimum(args->arr[args->center], minimum(args->arr[args->center - 1], args->arr[args->center + 1]));
int maxValue = maximum(args->arr[args->center], maximum(args->arr[args->center - 1], args->arr[args->center + 1]));
int midValue = args->arr[args->center] + args->arr[args->center - 1] + args->arr[args->center + 1] - minValue - maxValue;
args->arr[args->center - 1] = minValue;
args->arr[args->center + 1] = maxValue;
args->arr[args->center] = midValue;
}
}
// Main function performing the alternate time optimal sorting
void alternateTimeOptimalSorting(int arr[], int n){
int max_threads = n;
pthread_t threads[max_threads];
Arguments *args = (Arguments*)malloc(sizeof(Arguments));
args->arr = arr;
args->n = n;
// For n - 1 rounds
for(int i = 1; i < n; i ++){
int remainder = (i + 1) % 3;
int j;
if(remainder == 0){
j = 2;
}
else if(remainder == 1){
j = 0;
}
else{
j = 1;
}
int id = 0;
// For all centers possible at a distance of 3
while(j < n){
args->center = j;
// Thread creation
pthread_create(&threads[id], NULL, compare, (void *)args);
// Thread waiting
pthread_join(threads[id], NULL);
// Incrementing by 3 to find the next center
j += 3;
id ++;
}
}
}
// Driver function of the program
int main(){
// Input
int n;
printf("Enter the number of elements in your sequence:\n");
scanf("%d",&n);
int arr[n];
printf("Enter your sequence:\n");
for(int i = 0;i < n; i ++){
scanf("%d",&arr[i]);
}
alternateTimeOptimalSorting(arr,n);
// Output
printf("By alternate time optimal sorting method, the sorted sequence is:\n");
printArray(arr,n);
return 0;
}