-
Notifications
You must be signed in to change notification settings - Fork 2
/
postman.c
91 lines (86 loc) · 2.48 KB
/
postman.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
/*
* C Program to Implement Postman Sort Algorithm
*/
#include <stdio.h>
void arrange(int,int);
int array[100], array1[100];
int i, j, temp, max, count, maxdigits = 0, c = 0;
void main()
{
int t1, t2, k, t, n = 1;
printf("Enter size of array :");
scanf("%d", &count);
printf("Enter elements into array :");
for (i = 0; i < count; i++)
{
scanf("%d", &array[i]);
array1[i] = array[i];
}
for (i = 0; i < count; i++)
{
t = array[i]; /*first element in t */
while(t > 0)
{
c++;
t = t / 10; /* Find MSB */
}
if (maxdigits < c)
maxdigits = c; /* number of digits of a each number */
c = 0;
}
while(--maxdigits)
n = n * 10;
for (i = 0; i < count; i++)
{
max = array[i] / n; /* MSB - Dividnng by perticular base */
t = i;
for (j = i + 1; j < count;j++)
{
if (max > (array[j] / n))
{
max = array[j] / n; /* greatest MSB */
t = j;
}
}
temp = array1[t];
array1[t] = array1[i];
array1[i] = temp;
temp = array[t];
array[t] = array[i];
array[i] = temp;
}
while (n >= 1)
{
for (i = 0; i < count;)
{
t1 = array[i] / n;
for (j = i + 1; t1 == (array[j] / n); j++);
arrange(i, j);
i = j;
}
n = n / 10;
}
printf("\nSorted Array (Postman sort) :");
for (i = 0; i < count; i++)
printf("%d ", array1[i]);
printf("\n");
}
/* Function to arrange the of sequence having same base */
void arrange(int k,int n)
{
for (i = k; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (array1[i] > array1[j])
{
temp = array1[i];
array1[i] = array1[j];
array1[j] = temp;
temp = (array[i] % 10);
array[i] = (array[j] % 10);
array[j] = temp;
}
}
}
}