# C Program to Implement Postman Sort Algorithm

This C Program implements Postman Sort Algorithm.

Here is source code of the C Program implements Postman Sort Algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/* `
2. ` * C Program to Implement Postman Sort Algorithm`
3. ` */`
4. `#include <stdio.h>`
5. ` `
6. `void arrange(int,int);`
7. `int array[100], array1[100];`
8. `int i, j, temp, max, count, maxdigits = 0, c = 0;`
9. ` `
10. `void main()`
11. `{`
12. `    int t1, t2, k, t, n = 1;`
13. ` `
14. `    printf("Enter size of array :");`
15. `    scanf("%d", &count);`
16. `    printf("Enter elements into array :");`
17. `    for (i = 0; i < count; i++)`
18. `    {`
19. `        scanf("%d", &array[i]);`
20. `        array1[i] = array[i];            `
21. `    }`
22. `    for (i = 0; i < count; i++)`
23. `    {`
24. `        t = array[i];        /*first element in t */`
25. `        while(t > 0)`
26. `        {`
27. `            c++;`
28. `            t = t / 10;        /* Find MSB */`
29. `        }`
30. `        if (maxdigits < c) `
31. `            maxdigits = c;   /* number of digits of a each number */`
32. `        c = 0;`
33. `    }`
34. `    while(--maxdigits) `
35. `        n = n * 10;      `
36. ` `
37. `    for (i = 0; i < count; i++)`
38. `    {`
39. `        max = array[i] / n;        /* MSB - Dividnng by perticular base */`
40. `        t = i;`
41. `        for (j = i + 1; j < count;j++)    `
42. `        {`
43. `            if (max > (array[j] / n))`
44. `            {`
45. `                max = array[j] / n;   /* greatest MSB */`
46. `                t = j;`
47. `            }`
48. `        }`
49. `        temp = array1[t];`
50. `        array1[t] = array1[i];`
51. `        array1[i] = temp;`
52. `        temp = array[t];`
53. `        array[t] = array[i];`
54. `        array[i] = temp;`
55. `    }`
56. `    while (n >= 1)`
57. `    {`
58. `        for (i = 0; i < count;)`
59. `        {`
60. `            t1 = array[i] / n;`
61. `            for (j = i + 1; t1 == (array[j] / n); j++);`
62. `                arrange(i, j);`
63. `            i = j;`
64. `        }`
65. `        n = n / 10;`
66. `    }`
67. `    printf("\nSorted Array (Postman sort) :");    `
68. `    for (i = 0; i < count; i++)`
69. `        printf("%d ", array1[i]);`
70. `    printf("\n");`
71. `}`
72. ` `
73. `/* Function to arrange the of sequence having same base */`
74. `void arrange(int k,int n)`
75. `{`
76. `    for (i = k; i < n - 1; i++)`
77. `    {`
78. `        for (j = i + 1; j < n; j++)`
79. `        {`
80. `            if (array1[i] > array1[j])`
81. `            {`
82. `                temp = array1[i];`
83. `                array1[i] = array1[j];`
84. `                array1[j] = temp;`
85. `                temp = (array[i] % 10);`
86. `                array[i] = (array[j] % 10);`
87. `                array[j] = temp;`
88. `            }`
89. `        }`
90. `    }`
91. `}`

```\$ cc postman.c
\$ a.out
/* Average case */

Enter size of array :8
Enter elements into array :170
45
90
75
802
24
2
66

Sorted Array (Postman sort) :2 24 45 66 75 90 170 802

\$ a.out
/* Best case */
Enter size of array :7
Enter elements into array :25
64
185
136
36
3645
45

Sorted Array (Postman sort) :25 36 45 64 136 185 3645

\$ a.out
/* Worst case */
Enter size of array :8
Enter elements into array :15
214
166
0836
98
6254
73
642

Sorted Array (Postman sort) :15 73 98 166 214 642 836 6254```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]