1.

import java.util.Arrays;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 11462 Age Sort
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457
*
* @author Rolf Schirm
* @version 1.0, 04/13/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.988
*/
public class Main {

private static byte[] buffer;
private static int n;

private static int readDigit() {
// Number as string
String numberString = "";

// Single character from the input
char c = (char)buffer[n++];

// Skip all none digits
while(!Character.isDigit(c)) {
c = (char)buffer[n++];
}

// Add all digits
while(Character.isDigit(c)) {
numberString += c;
c = (char)buffer[n++];
}

// Return the digit
return Integer.parseInt(numberString);
}

public static void main(String... args) throws Exception {
// Buffer for the input
buffer = new byte[System.in.available()];

// Read all the input
System.in.read(buffer);

// Current index from the buffer
n = 0;

while(true) {
// Read a digit from the input
int count = readDigit();

// Termination condition
if(count == 0) {
break;
}

// Array with numbers
int[] array = new int[count];

// Length of the input/output line
int capacity = n;

for(int i = 0; i < count; i++) {
// Read a digit from the input
array[i] = readDigit();
}

// Length delta
capacity = n - capacity;

// Sort the array
Arrays.sort(array);

// Result string with an estimated capacity
StringBuilder result = new StringBuilder(capacity);

// Append first number
result.append(array[0]);

// Append the rest of the numbers with ' ' as separator between each
for(int i = 1; i < count; i++) {
result.append(' ');
result.append(array[i]);
}

// Print the output
System.out.println(result);
}
}
}


2.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 11462 - Age Sort
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457
* @author Mitterreiter Christian
* @author Posselt Christian
* @version 1.0, 04/24/2011
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 3.700
*/

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
StringBuilder out = new StringBuilder();
int count = sc.nextInt(); // einlesen der zu erwartenden Zahlen

while (true) {

short[] in = new short[10000];

// für jede eingelesene Zahl wird an ihrer Stelle im
// Array der entsprechene Index um 1 erhöht.
for (int i = 0; i < count; i++) {
in[sc.nextInt()]++;
}

for (int j = 0; j < in.length; j++) {
if (in[j] > 0) {
short a = in[j];
while (a > 0) { // wenn eine Zahl mehrfach vorkam, wird sie a mal ausgegeben
out.append(j + " ");
a--;
}
}
}

count = sc.nextInt();
out.deleteCharAt(out.length() - 1); // schneidet das letzte " " von jeder Zeile wieder weg

if (count == 0) {
break;
} else
out.append("\n");
}
System.out.println(out); // Ausgabe des Ergebnisses
}


}
-------------------------------------------------------------------------





1.

/**
 * 
 * Problem #11462 - Age Sort - Loesung 1
 * Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457

 * 
 * @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
 * http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457

 * 
 * @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
   }
}
}




3.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 11462 Age Sort
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=74&problem=2457&mosmsg=Submission+received+with+ID+8043192
*
* @author Viktoriya Ryabova
* @version 1.0, 06/28/2010
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 1.348
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer();
while(true){
int count = Integer.parseInt(in.readLine());
if (count==0)return;
StringTokenizer t = new StringTokenizer(in.readLine());

int [] input = new int [count];
for (int i = 0; i<count; i++){
input[i]= Integer.parseInt(t.nextToken());
}
int [] result = countingSort(input);

sb.append(result[0]);
for(int i=1; i<count; i++){
sb.append(" "+ result [i]);
}

System.out.println(sb);
sb.delete(0, sb.length());
}

}

public static int[] countingSort(int[] numbers) {
// Maximum der Zahlen berechnen
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
// wenn es größeres als das aktuelle gibt, ist das nun das neue größte
if (numbers[i] > max)
max = numbers[i];
}

// temporäres Array erzeugen mit: Länge = Maximum des Zahlenarrays + die "0"
int[] sortedNumbers = new int[max+1];

// Indizes des Zahlen-Arrays durchgehen
for (int i = 0; i < numbers.length; i++) {
// wir zählen, wie oft jede Zahl aus numbers vorkommt und
// speichern diese Anzahl in sortedNumbers[] bei Index number[i]
sortedNumbers[numbers[i]]++;
}

// insertPosition steht für die Schreib-Position im Ausgabe-Array
int insertPosition = 0;

// Indizes von sortedNumbers[] durchgehen, um zu sehen, wie oft jede Zahl vorkommt
for (int i = 0; i <= max; i++) {
// Anzahl von i durchgehen, um gleiche Zahlen hintereinander einzutragen
for (int j = 0; j < sortedNumbers[i]; j++) {
// das Zahlen-Array wird jetzt sortiert neu geschrieben für jedes
// Auftreten von i
numbers[insertPosition] = i;
insertPosition++;
}
}
return numbers;
}

}