import javax.swing.*; import java.util.*; /** * Several examples of sorting algorithms * insert sort, bubble sort, merge sort, quick sort and radix sort */ public class SortingAlgorithms { // The array to sort private int[] a; // A copy of the original array private int [] initialArray; // Default dimension of a private static final int SIZE=10; // Maximum number of digits of the elements of a(for radix sort) private static final int MAXIMUM_NUMBER_OF_DIGITS = 3; /** * Generate an array of random integers of size SIZE */ public SortingAlgorithms() { // Use the other constructor this(SIZE); } /** * Generate an array of random integers of size n */ public SortingAlgorithms(int n) { // Take the default size if n <=0 if (n<0) n=SIZE; // Create a a = new int[n]; // Fill a with random integers (between 0 and maxNumber-1) double maxNumber = Math.pow(10,MAXIMUM_NUMBER_OF_DIGITS); for(int i=0; i0) out+=a[a.length-1]+"}"; return out; } /** * Sort the array with insert sort */ public void insertSort() { for(int i=1; i=0 && a[k]>temp; k--) a[k+1]=a[k]; a[k+1]=temp; } /** * Sort the array with bubble sort */ public void bubbleSort() { for(int pass=1; passa[i+1]) { int temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } /** * Sort the array with merge sort (a faster algorithm) */ public void mergeSort() { divideInTwoSortAndMerge(0,a.length-1); } /** * Divide the array in two * Order each part * Merge the two ordered parts */ private void divideInTwoSortAndMerge(int first, int last) { if (first