您的位置:首页 > 产品设计 > UI/UE

Given an array of integers, sort the array according to frequency of elements

2013-05-07 15:23 543 查看
Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the
array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}. 

Following is detailed algorithm.
1) Create a BST and while creating BST maintain the count i,e frequency of each coming element in same BST. This step may take O(nLogn) time if a self balancing BST is used.
2) Do Inorder traversal of BST and store every element and count of each element in an auxiliary array. Let us call the auxiliary array as ‘count[]‘. Note that every element of this array is element and frequency pair. This step takes O(n)
time.
3) Sort ‘count[]‘ according to frequency of the elements. This step takes O(nLohn) time if a O(nLogn) sorting algorithm is used.
4) Traverse through the sorted array ‘count[]‘. For each element x, print it ‘freq’ times where ‘freq’ is frequency of x. This step takes O(n) time.
Overall time complexity of the algorithm can be minimum O(nLogn) if we use a O(nLogn) sorting algorithm and use a self balancing BST with O(Logn) insert operation.
Following is C++ implementation of the above algorithm.
#include<iostream>
using namespace std;

typedef struct tree_node_s {
int value;
int freq;
struct tree_node_s *lchild;
struct tree_node_s *rchild;
}tree_node_t, *BSTree;

typedef struct data_s {
int value;
int freq;
}data_t;

int tree_search(BSTree T, int value, tree_node_t **p, tree_node_t *f) {
if (NULL == T) {
*p = f;
return 0;
}
if (value == T->value) {
*p = T;
return 1;
} else if (value < T->value) {
return tree_search(T->lchild, value, p, T);
} else {
return tree_search(T->rchild, value, p, T);
}
}

void tree_insert(BSTree *T, int value) {
tree_node_t *p = NULL;
if (!tree_search(*T, value, &p, NULL)) {
tree_node_t *s = (tree_node_t*)malloc(sizeof(tree_node_t));
s->value = value;
s->freq = 1;
s->lchild = NULL;
s->rchild = NULL;
if (NULL == (*T))
*T = s;
else if (value < p->value)
p->lchild = s;
else
p->rchild = s;
} else {
p->freq++;
}
}

int freq_compare(const void *a, const void *b) {
return (*(const data_t*)b).freq - (*(const data_t*)a).freq;
}

void store_data(BSTree T, data_t *datas, int *index) {
if (NULL == T)
return;
store_data(T->lchild, datas, index);
datas[*index].value = T->value;
datas[*index].freq = T->freq;
(*index)++;
store_data(T->rchild, datas, index);
}

void print_arr(int *arr, int n) {
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
}

void sort_by_freq(int *arr, int n) {
BSTree T = NULL;
int i;
for (i = 0; i < n; i++)
tree_insert(&T, arr[i]);
data_t *datas = (data_t*)malloc(n * sizeof(data_t));
int index = 0;
store_data(T, datas, &index);
qsort(datas, index, sizeof(datas[0]), freq_compare);
int j = 0;
int k;
for (i = 0; i < index; i++) {
for (k = datas[i].freq; k > 0; k--)
arr[j++] = datas[i].value;
}
free(datas);
}

int main(int argc, char *argv[]) {
int arr[] = {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12};
int n = sizeof(arr) / sizeof(int);
sort_by_freq(arr, n);
print_arr(arr, n);
cin.get();
return 0;
}

连续碰到几个题目都用到bst,看来数据结构的灵活应用可以解决很多问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐