-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencySort.c
More file actions
43 lines (43 loc) · 917 Bytes
/
frequencySort.c
File metadata and controls
43 lines (43 loc) · 917 Bytes
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
Input : 1 1 1 2 3 3 3 3 4 4 5 5 5 5 5
Output: 5 5 5 5 5 3 3 3 3 1 1 1 4 4 2
Code:
#include<stdio.h>
#define SIZE 100
struct node
{
int data;
int frequency;
};
void insertionSort( struct node *arr, int num )
{
int ind1, ind2;
struct node temp;
for( ind1 = 1 ; ind1 < num ; ind1++ )
{
temp = arr[ind1];
ind2 = ind1 - 1;
while( ind2 >= 0 && arr[ind2].frequency < temp.frequency )
{
arr[ind2+1] = arr[ind2];
ind2--;
}
arr[ind2+1] = temp;
}
}
int main()
{
char string[SIZE];
int ind;
struct node array[10];
scanf("%[^\n]s",string);
for(ind=0;ind<=9;array[ind].data=ind,array[ind].frequency=0,ind++);
for( ind = 0 ; string[ind] ; ind++ )
if( string[ind] != ' ')
array[ string[ind] - '0' ].frequency +=1;
insertionSort( array, 10 );
for( ind = 0 ; ind <= 9 ; ind++ )
if( array[ind].frequency != 0 )
while( array[ind].frequency-- )
printf("%d ", array[ind].data );
return 0;
}