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

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,看来数据结构的灵活应用可以解决很多问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值