/* * Created on Mar 14, 2005 * Cerated By Milan Stankovic * milstan@gmail.com * http://javaprimeri.blogspot.com */ /** * @author MilStan * Belgrade * Ovo je program za sortiranje preko bubbleSort algoritma */ import java.io.*; public class SortKakoTreba { static char[] ucitajNiz() throws IOException{ BufferedReader input = new BufferedReader(new InputStreamReader( System.in)); return input.readLine().toCharArray(); } static char[] bubbleSort(char[] niz){ for (int i = niz.length-1;i>= 0; i-- ) { for (int j = 0; j < i; j++) { if (niz[j] > niz[j+1]) { char c = niz[j]; niz[j] = niz[j+1]; niz[j+1] = c; } } } return niz; } static char[] bubbleInvertSort(char[] niz){ for (int i = niz.length-1;i>= 0; i-- ) { for (int j = 0; j < i; j++) { if (niz[j] < niz[j+1]) { char c = niz[j]; niz[j] = niz[j+1]; niz[j+1] = c; } } } return niz; } public static void main(String[] args) { System.out.println("Unesite niz: "); try{ char niz[] = ucitajNiz(); String st = new String(bubbleSort(niz)); System.out.println("Sortirano u rastucem poretku: " + st); String ts = new String(bubbleInvertSort(niz)); System.out.println("Sortirano u opadajucem poretku: " + ts); } catch (IOException e) {} } }