/******************************************************************************
 * Sorting incoming words on command line 
 ******************************************************************************/
import java.util.Arrays;

public class zSort
{
    public static void main (String[] words) 
    {

        if (words.length < 3) {
            System.out.println("Re-execute with at least 3 words");
            System.exit(-1);
        }
    
        System.out.println("BEFORE THE SORT:");

        String str2 = "";                           //String for concatenation

        for (int i=0; i < words.length; i++)
        { 
            System.out.println(words[i]);           //print each word on a separate line
            str2 += words[i] + " ";                 //concatenate to a single string
        }
        System.out.println(str2);
        System.out.println();

//      Arrays.sort(words);                         //Java standard sort

        bubbleSort(words);                          //Bubble sort method                             

        System.out.println();
        System.out.println("AFTER THE SORT:");

        StringBuffer sb1 = new StringBuffer();      //StringBuffer for a single word
        StringBuffer sb2 = new StringBuffer();      //StringBuffer for concatenation

        for (int i=0; i < words.length; i++) 
        { 
//          StringBuffer sb1 = new StringBuffer(words[i]);      //copy into a new StringBuffer
            sb1 = sb1.replace(0,999,words[i]);                  //copy into an existing stringBuffer
            System.out.println(sb1);                            //print after sort
            sb2.append(sb1 + " ");                              //concatenate to a single stringBuffer
        }

        System.out.println(sb2);
    }

/*********************************************************************************
 * BUBBLE SORT method:
 * OBJECTIVE: Push the largest element all the way down, then repeat.
 * The method as a lot of optional prints to allow us to see how the sort works
 ********************************************************************************/  
    static void bubbleSort(String[ ] array)
    { 
        String  temp;
        int     upto    = array.length;

        System.out.println("Sorting array.........: "+ Arrays.toString(array));                 //<optional PRINT> 

        for (int loop=0; loop < array.length; loop++)
        {
//          System.out.println("Loop from 1 to " + upto);                                       //<optional PRINT> 

            for (int i=0; i < upto-1; i++)                          //loop for every element
            {
//              System.out.println("\t Comparing elements: " + array[i] +" "+ array[i+1]);      //<optional PRINT> 

                if (array[i].compareTo(array[i+1]) > 0)             //compare 2 elements, if greater        
                {
                    temp       = array[i];                          //switch elements
                    array[i]   = array[i+1];
                    array[i+1] = temp;

//                  System.out.println("\t SWITCHED ELEMENTS: " + array[i+1] +" "+array[i]);     //<optional PRINT> 
                }
            }
            upto = upto -1; 

//          System.out.println("End of loop: " + Arrays.toString(array));                        //<optional PRINT> 
        }
    }
}