Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Usage:
- // > java BSort Petra Bob Peter Adam Zap John Adam
- // Adam Adam Bob John Peter Petra Zap
- class BSort {
- public static void main(String[] args) {
- bubbleSortSmart(args);
- for(String arg : args) System.out.print(arg + " ");
- System.out.println();
- System.exit(0);
- }
- static void bubbleSortSmart(String[] a) {
- int newIdx = a.length - 1;
- int oldIdx = 0;
- while(newIdx != oldIdx) {
- oldIdx = newIdx;
- for(int i = 0; i < oldIdx; i++) {
- if (a[i].compareTo(a[i+1]) > 0) {
- String tmp = a[i];
- a[i] = a[i+1];
- a[i+1] = tmp;
- newIdx = i;
- }
- }
- }
- }
- static void bubbleSortNaive(String[] a) {
- int maxIdx = a.length - 1;
- while(maxIdx != 0) {
- for(int i = 0; i < maxIdx; i++) {
- if (a[i].compareTo(a[i+1]) > 0) {
- String tmp = a[i];
- a[i] = a[i+1];
- a[i+1] = tmp;
- }
- }
- maxIdx--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement