package arraysorter;

import java.util.*;

/**
* Generates an array of integers then sorts it using a bubblesort
* and then reverses it
* @author BotRejectsInc
*/
public class Main
{

/** Creates a new instance of Main */
public Main()
{
}

public static void main(String[] args)
{
    int arrayLength = 10;
    int[] testArray = new int[arrayLength];
    Random rnd = new Random();
    
    //Populate array with random numbers between 0 and 99
    for(int i = 0; i < arrayLength-1; i++)
    {
        testArray[i] = rnd.nextInt(99);
    }
    //Display array
    System.out.println("The randomly generated array is:");
    displayArray(testArray);
    
    //Reverse array for the sake of it
    System.out.println("Which reversed is:");
    reverseArray(testArray);
    displayArray(testArray);
    
    //Sort array
    bubblesort(testArray);
    //Display array
    System.out.println("After sorting the array is:");
    displayArray(testArray);
    
    //Reverse array
    reverseArray(testArray);
    //Display array
    System.out.println("And after reversing...");
    displayArray(testArray);
}

public static void displayArray(int[] arr)
{
    int arrayLength = arr.length;
    for(int i = 0; i < arrayLength; i++)
    {
        System.out.print(arr[i]);
        if( i < (arrayLength-1) )
        {
            System.out.print(", ");
        }
    }
    System.out.print("\n\n");
}

public static void bubblesort(int[] arr)
{
    boolean swapped = true;
    int j = 0;
    int temp;
    
    while(swapped)
    {
        swapped = false;
        j++;
        for(int i = 0; i < arr.length - j; i++)
        {
            if(arr[i] > arr[i+1])
            {
                temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
                swapped = true;
            }
        }
    }
}

public static void reverseArray(int[] arr)
{
    int arrayLength = arr.length;
    int temp;
    
    for(int i=0; i < arrayLength/2; i++)
    {
        temp = arr[i];
        arr[i] = arr[arrayLength-1-i];
        arr[arrayLength-1-i] = temp;
    }
}

}
Bubble Sort and Reverse Array in Java

The snippet below is just a little play with arrays in Java. It illustrates how to sort an array using a
bubble-sort and how to reverse an array. In a bubble sort, larger (or smaller) values 'bubble-up' through
the list of data items (in this case an array of integers) by pairwise comparisons: each data item is
compared with it neighbour and if it is larger (or smaller) then it swaps places with the neighbour.

Note that
Java always passes by value. This means that it either passes a copy of a value type (such as
an int) to a function as the required function argument, or it passes a copy of the reference for a
reference type. That is the reference is copied by value (a shallow copy) and then passed to the function.
An
array is a reference type and a copy of the reference (an alias) is passed to the function. (In this case
an alias (arr) for testArray is passed to the various functions). Thus changes made to the array in the
function are seen by the calling function (in this case main()). Personally I find this very elegant. Note that
we can not do similar things with strings in Java, since in Java strings are immutable.