-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_swaps.c
77 lines (76 loc) · 1.99 KB
/
minimum_swaps.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
#include <stdio.h>
#include<stdlib.h>
long int maxi(long int *p, long int n) //function to determine the position of maximum difference.
{
int max = *p;
static int j;
j = 0;
for (int i = 0; i < n; i++)
{
if (*(p + i) > max)
{
max = *p;
j = i;
}
}
return j;
}
int main()
{
long int n, min, max, temp, dif_check =0, d,count=0;
scanf("%ld", &n);
long int num[n], serial[n], dif[n];
for (long int i = 0; i < n; i++)
{
scanf("%ld", &num[i]);
printf("i = %ld num = %ld",i,num[i]);
}
min = num[0];
max = num[0];
for (long int i = 0; i < n; i++)
{
if (num[i] < min)
{
min = num[i];
}
if (num[i] > max)
{
max = num[i];
}
}
serial[0] = min;
for (long int i = 1; i < n; i++) //loop to sort the given array
{
serial[i] = max;
for (int j = 0; j < n; j++)
{
if ((num[j] > serial[i - 1]) && (num[j] < serial[i]))
{
serial[i] = num[j];
}
}
}
while(1)
{
for (long int i = 0; i < n; i++)
{
dif[i] = num[i] - serial[i]; //making the array of the diffence of the given array and the sorted array's value
dif_check += abs(dif[i]); //sum of the all difference
}
if (dif_check == 0) //If all the difference is zero that means the arranged array and the sorted array is in the same serial
{
break;
}
else
{
d = maxi(dif, n);//find the position of the maximum difference
temp = num[d];
num[d] = num[d + dif[d]];
num[d + dif[d]] = temp;//swap the number which have the maximum difference to make the difference zero
count++; // count the number of step.
dif_check =0; //make the sum of the difference zero again.
}
}
printf("%ld",count);
return 0;
}