-
Notifications
You must be signed in to change notification settings - Fork 33
/
countsort.c
101 lines (84 loc) · 2.05 KB
/
countsort.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
// Counting Sort in C program
#include <stdio.h>
#include <string.h>
#define RANGE 255
// The function to sort the given string arr[] in alphabetical order.
void countSort(char arr[])
{
// It is the output character array to store sorted arr.
char output[strlen(arr)];
// It is the count array to store count of individual characters.
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));
// Store count of each character.
for(i = 0; arr[i]; ++i)
++count[arr[i]];
// Position of this character in the output array.
for (i = 1; i <= RANGE; ++i)
count[i] += count[i-1];
// Build the output character array.
for (i = 0; arr[i]; ++i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
/*
For Stable algorithm
for (i = sizeof(arr)-1; i>=0; --i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}
For Logic: See implementation
*/
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}
// Driver program to test the above function.
int main()
{
char arr[] = "techgeekbuzz";//"applepp";
countSort(arr);
printf("The sorted character array is %sn.", arr);
return 0;
}
Output:
The sorted character array is techgeekbuzz.
Example of Counting Sort in C++ Programming
#include<bits/stdc++.h>
using namespace std;
void countsort(int arr[], int n, int k)
{
int output[n];
int count[k];
memset(count, 0, sizeof(count));
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
int commulative = 0;
for (int i = 0; i < k; i++)
{
int oldCount = count[i];
count[i] = commulative;
commulative += oldCount;
}
for (int i = 0; i < n; i++)
{
output[count[arr[i]]] = arr[i];
count[arr[i]]++;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
int main()
{
int arr[] = { 1, 5, 4, 3, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 6;
countsort(arr, n, k);
for (int i = 0; i < n; i++) {
cout<<arr[i]<<" ";
}
return 0;
}