Sort array based on count of occurrences in ascending order
Here is an efficient way to do it using TreeMap.
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class FrequencySort {
public static void main(String[] args) {
int[] ar = new int[] {5,2,8,8,5,5,8,1,9,0,1,1,0,1};
Map<Integer,Integer> numbers = new HashMap<>();
for(int number : ar) {
if(numbers.containsKey(number)) {
Integer count = numbers.get(number);
numbers.put(number, ++count);
} else {
numbers.put(number,1);
}
}
final class FrequencyComparator implements Comparator<Integer> {
Map<Integer,Integer> refMap;
public FrequencyComparator(Map<Integer,Integer> base) {
this.refMap = base;
}
@Override
public int compare(Integer k1, Integer k2) {
Integer val1 = refMap.get(k1);
Integer val2 = refMap.get(k2);
int num = val1.compareTo(val2) ;
// if frequencies are same then compare number itself
return num == 0 ? k1.compareTo(k2) : num;
}
}
FrequencyComparator comp = new FrequencyComparator(numbers);
TreeMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>(comp);
sortedMap.putAll(numbers);
for(Integer i : sortedMap.keySet()) {
int frequencey = sortedMap.get(i);
for(int count = 1 ; count <= frequencey ; count++) {
System.out.print(i + " " );
}
}
}
}
If you just want to sort the array, use the following:
Arrays.sort(a);
If you want to sort it manually, I would recommend reading this page, where you can find pseudo-code for a variety of sorting algorithms.
However, if you are searching for a way to sort the array based on number frequency, I would recommend this page. You would have to inverse the sorting order, sonce you want them in ascending order.
Here's one approach to start you off could be based on the idea of keeping a count of how many times each integer in the initial array has occurred in a map. Once all the numbers have been counted, sort the map by order of ascending value, and then print the output of the map:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;
public class SortCount {
public static void main(String[] args) {
int nums[] = {0,0,0,1,3,3,2,1,3,5,6,0};
HashMap<Integer,Integer> counts = new HashMap<Integer,Integer>();
for(int i = 0; i < nums.length; i++) {
if(counts.containsKey(nums[
Integer c = counts.get(nums[i]) + 1;
counts.put(nums[i], c);
}
else {
counts.put(nums[i],1);
}
}
ValueComparator<Integer,Integer> bvc = new ValueComparator<Integer,Integer>(counts);
TreeMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>(bvc);
sortedMap.putAll(counts);
ArrayList<Integer> output = new ArrayList<Integer>();
for(Integer i : sortedMap.keySet()) {
for(int c = 0; c < sortedMap.get(i); c++) {
output.add(i);
}
}
System.out.println(output.toString());
}
}
Which uses a Comparator
class to compare the values in a Map
:
import java.util.Comparator;
import java.util.Map;
public class ValueComparator<T1,T2 extends Comparable<T2>> implements Comparator<T1> {
Map<T1,T2> base;
public ValueComparator(Map<T1,T2> base) {
this.base = base;
}
@Override
public int compare(T1 k1, T1 k2) {
T2 val1 = base.get(k1);
T2 val2 = base.get(k2);
return val1.compareTo(val2);
}
}