-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment3.c
191 lines (138 loc) · 3.08 KB
/
Assignment3.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<stdio.h>
void Q1();
void Q2();
int mergesort(int [],int ,int ,int );
int merge(int [],int ,int ,int ,int );
int mod(int );
int main()
{
int q;
//selecting based on question number
printf("Enter the question no.\n");
scanf("%d",&q);
switch(q)
{
case 1:Q1();break;
case 2:Q2();break;
default:printf("Enter a valid question.\n");break;
}
return 0;
}
void Q2()
{
//there are many methods to do this.
//method 1 is to do linear search comparing amplitudes which takes 0(n*k)
//method 2 is to sort and search which takes 0(nlogn)
//method 3 is to use minheap(heapsort) of differences which takes n or klogn (whichever is smaller)
//method 4 is to use very simple hash table and takes 0(n+m) where m is maxelement.
//doing in method 4 because we are dealing with intergers only and not decimals.
int i,j,k,key,n;
printf("Enter n:\n");
scanf("%d",&n);
int list[n];
printf("Enter values:\n");
//taking input
for(i=0;i<n;i++)
scanf("%d",&list[i]);
//finding max magnitude element
int max=0;
for(i=0;i<n;i++)
if(max<mod(list[i]))
max=mod(list[i]);
int hash[2*max+1];
//filling hash array with zeroes
for(i=0;i<2*max+1;i++)
hash[i]=0;
//filling hash array with frequency of elements
for(i=0;i<n;i++)
hash[max+list[i]]++;
printf("Enter key and k:\n");
scanf("%d %d",&key,&k);
i=max+key;
j=i;
//checking for close numbers in hash array linearly
//this actually takes 0(k) time if observed carefully
while(1)
{
while(hash[i]!=0 && i>=0 && k>0)
{
printf("%d ",i-max);
hash[i]--;
k--;
}
while(hash[j]!=0 && j<2*max+1 && k>0)
{
printf("%d ",j-max);
hash[j]--;
k--;
}
if(k<=0) break;
i--;
j++;
}
printf("\n");
}
int mod(int x)
{
if(x<0)
return -x;
else
return x;
}
void Q1()
{
//doing by changing mergesort original ,takes 0(nlogn)
int n,i;
printf("Enter n:\n");
scanf("%d",&n);
int arr[n];
printf("Enter the values:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("No of inversions: %d\n",mergesort(arr,0,n-1,n));
}
int mergesort(int arr[],int start,int end,int size){
int count=0;
if(start>=end)
return 0;
int middle=(start+end)/2;
//returned values of no of inversions
count=mergesort(arr,start,middle,size);
count+=mergesort(arr,middle+1,end,size);
count+=merge(arr,start,middle,end,size);
return count;
}
int merge(int arr[],int start,int middle,int end,int n){
int size1,size2,count=0;
int i=0,j=0,k=0;
size1=middle-start+1;
size2=end-middle;
int a1[size1],a2[size2];
for(i=0;i<size1;i++)
a1[i]=arr[start+i];
for(i=0;i<size2;i++)
a2[i]=arr[middle+1+i];
i=0;k=start;
while(i<size1 && j<size2){
if(a1[i]<=a2[j])
arr[k++]=a1[i++];
//this is the modification
else
{
arr[k++]=a2[j++];
//if this number is greater, the all numbers to the right of this number
//in the left array will be greater since the array is in ascending order
//so total inversions will be size of right array - index
count+=(size1-i);
}
}
if(i>=size1)
while(j<size2)
arr[k++]=a2[j++];
else if(j>=size2)
while(i<size1)
arr[k++]=a1[i++];
return count;
}