[Lucky Algorithm] Counting Sort 2(41/77)
Counting Sort 2 (Hacker Rank)
Often, when a list is sorted, the elements being sorted are just keys to other values. For example, if you are sorting files by their size, the sizes need to stay connected to their respective files. You cannot just take the size numbers and output them in order, you need to output all the required file information.
However, if you are not concerned about any other information, then you can simply sort those numbers alone. This makes counting sort very simple. If you already counted the values in the list, you don’t need to access the original list again. This challenge involves a simple counting sort where the elements being sorted are all that matter.
Challenge Given an unsorted list of integers, output the integers in order.
Hint: You can use your previous code that counted the items to print out the actual values in order.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
int[] count = new int[100];
for(int i = 0; i < n; i++){
ar[i] = in.nextInt();
int k = ar[i];
count[k]++;
}
for(int j = 0; j < 100; j++){
for(int k = 0; k < count[j]; k++){
System.out.print(j + " ");
}
}
}
}
행운의 77문제 프로젝트
행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.