/**
*
* Problem #11462 - Age Sort - Loesung 1
*
* @author Mariya Vlaseva
*
* Methode: Radix sort
* Status : Accepted
* Runtime: 3.320
*/
import java.util.Scanner;
public class Main {
private
static final int MAX_NUMBERS_OF_PEOPLE = 2000000;
private
static final int INPUT_TERMINATION = 0;
/**
*
@param args
*/
public
static void main(String[] args) {
Scanner
sc = new Scanner(System.in);
StringBuilder
sb = new StringBuilder();
while(sc.hasNext())
{
int
numberOfPeople = sc.nextInt();
if(numberOfPeople
== INPUT_TERMINATION) {
break;
}
if(checkInput(numberOfPeople))
{
int[]
ages = new int[numberOfPeople];
for(int
i = 0; i < numberOfPeople; i++) {
ages[i]
= sc.nextInt();
}
radixSort(ages);
for(int
age : ages) {
sb.append(age
+ " ");
}
System.out.println(sb.toString().trim());
sb.delete(0,
sb.length());
}
}
}
private
static boolean checkInput(int size) {
return
size > 0 && size <= MAX_NUMBERS_OF_PEOPLE;
}
public
static void radixSort(int[] a) {
int
n;
// Fachnummer
int[]
nPart = new int[2]; //
Anzahl der Elemente in den beiden Faechern
int[][]
part = new int[2][a.length]; // die beiden Faecher haben
die Groesse des Arrays a
//
Schleife ueber alle Bits der Schluessel (bei int: 32 Bit)
for
(int i=0; i<32; i++) {
nPart[0]
= 0;
nPart[1]
= 0;
//
Partitionierungsphase: teilt "a" auf die Faecher auf
for
(int j=0; j<a.length; j++) {
n
= (a[j]>>i)&1;
// ermittelt die Fachnummer: 0 oder 1
part[n][nPart[n]++]
= a[j]; // kopiert j-tes Element ins richtige Fach
}
//
Sammelphase: kopiert die beiden Faecher wieder nach "a" zusammen
System.arraycopy(part[0],
0, a, 0, nPart[0]);
System.arraycopy(part[1],
0, a, nPart[0], nPart[1]);
}
}
}
2.
/**
*
* Problem #11462 - Age Sort - Loesung 2
*
* @author Mariya Vlaseva
*
* Methode: Countingsort
* Status : Accepted
* Runtime: 3.784
*/
import java.util.Arrays;
import java.util.Scanner;
public class Main {
private
static final int MAX_NUMBERS_OF_PEOPLE = 2000000;
private
static final int INPUT_TERMINATION = 0;
/**
*
@param args
*/
public
static void main(String[] args) {
Scanner
sc = new Scanner(System.in);
StringBuilder
sb = new StringBuilder();
while(sc.hasNext())
{
int
numberOfPeople = sc.nextInt();
if(numberOfPeople
== INPUT_TERMINATION) {
break;
}
if(checkInput(numberOfPeople))
{
int[]
ages = new int[numberOfPeople];
for(int
i = 0; i < numberOfPeople; i++) {
ages[i]
= sc.nextInt();
}
countingSort(ages);
for(int
age : ages) {
sb.append(age
+ " ");
}
System.out.println(sb.toString().trim());
sb.delete(0,
sb.length());
}
}
}
private
static boolean checkInput(int size) {
return
size > 0 && size <= MAX_NUMBERS_OF_PEOPLE;
}
public
static void countingSort(int[] a) {
//
find min, max to set range of counts array
int low = a[0];
int high = a[0];
for(int i=0; i<a.length; i++) {
if (low > a[i])
low = a[i];
else if (high < a[i])
high = a[i];
}
int[] counts = new int[high - low + 1]; // this will hold
all possible values, from low to high
for (int x : a)
counts[x - low]++; // - low so the lowest
possible value is always 0
int current = 0;
for (int i = 0; i < counts.length; i++)
{
Arrays.fill(a, current, current + counts[i],
i + low); // fills counts[i] elements of value i + low in current
current += counts[i]; // leap forward by
counts[i] steps
}
}
}