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,看来数据结构的灵活应用可以解决很多问题。
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,看来数据结构的灵活应用可以解决很多问题。
相关文章推荐
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Given a non-empty array of integers, return the k most frequent elements.
- The minimum number of elements to reach the end of an array
- given an array of integers Find the index of values that satisfy A+B = C + D
- You’re given an array containing both positive and negative integers and required to find the sub-a
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- GeeksforGeeks Sort an array according to the order defined by another array
- Given an array of positive and negative integers find the first subarray with zero sum
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Given an array of size n, find the majority element. The majority element is the element that appear
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
- Reduce a date range to an array of the last days of each month?
- Ruby: how to sort array of string parsing the content
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- The type of the expression must be an array type but it resolved to Point
- Find the first repeating element in an array of integers
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times