-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion41.c
78 lines (59 loc) · 1.12 KB
/
question41.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
/*
http://practice.geeksforgeeks.org/problems/sorting-elements-of-an-array-by-frequency/0
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int index;
int value;
};
struct hash{
int index;
int freq;
int value;
};
int cmpfnc1(const void *a, const void *b){
const struct node *p1 = a;
const struct node *p2 = b;
if(p1->value < p2->value){
return -1;
}else if(p1->value > p2->value){
return 1;
}else{
return 0;
}
}
void sortByFreq(int *arr, int size){
struct node ref[size];
int i;
for(i=0;i<size;i++){
ref[i].value = arr[i];
ref[i].index = i;
}
qsort(ref, size, sizeof(struct node), cmpfnc1);
// for(i=0;i<size;i++){
// printf("%d, %d \n", ref[i].value, ref[i].index);
// }
struct hash *h = (struct hash *)calloc(61, sizeof(struct hash));
// for(i=0;i<size;i++){
// h[ref[i].value]++;
// h[ref[i].value].index = ref[i].index;
// }
// qsort(h, 61, sizeof(struct hash), cmfnc2);
}
int main(){
int cases;
scanf("%d",&cases);
int i;
for(i=0;i<cases;i++){
int n;
scanf("%d",&n);
int arr[n];
int j;
for(j=0;j<n;j++){
scanf("%d",&arr[j]);
}
sortByFreq(arr,n);
}
return 0;
}