/* * Sorter class contains static methods for bubble sort, * insertion sort,selection sort and quick sort. * * */ //Sorter.java public class Sorter { //Bubble sort public static int[] bubbleSort(int list[]) { for (int c = 0; c < ( list.length - 1 ); c++) { for (int d = 0; d < list.length - c - 1; d++) { if (list[d]>list[d+1]) /* For ascending order*/ { int temp= list[d]; list[d]=list[d+1]; list[d+1]=temp; } } } return list; } //Selection sort public static int[] selectionSort(int[] list) { for (int i = 0; i < list.length - 1; i++) { int index = i; for (int j = i + 1; j < list.length; j++) if (list[j] 0 ; j--) { if(list[j] pivot) { j--; } if (i <= j) { exchangeNumbers(array,i, j); //move index to next position on both sides i++; j--; } } // call quickSort() if (lowerIndex < j) quickSort(array,lowerIndex, j); if (i < higherIndex) quickSort(array,i, higherIndex); } //exchange elements at index i and j private static void exchangeNumbers(int array[],int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } ---------------------------------------------------------------------------------------------------------------- /** * The java program Driver that calls the methods * of Sorter class to sort 8 element array for bubble sort, * insertion and selection sort. * Call quicksort for 16 element array and print * values before and after sorting . * */ //Driver.java public class Driver { public static void main(String[] args) { int arr1[]={1,7,3,2,0,5,0,8}; int arr2[]={1,7,3,2,0,5,0,8}; int arr3[]={1,7,3,2,0,5,0,8}; System.out.println(\"Before bubble sort\"); for (int i = 0; i < arr1.length; i++) System.out.printf(\"%5d\",arr1[i]); int bsort[]=Sorter.bubbleSort(arr1); System.out.println(\"\ After bubble sort\"); for (int i = 0; i < bsort.length; i++) System.out.printf(\"%5d\",bsort[i]); System.out.println(\"\ Before insertion sort\"); for (int i = 0; i < arr2.length; i++) System.out.printf(\"%5d\",arr2[i]); int isort[]=Sorter.insertionSort(arr2); System.out.println(\"\ After insertion sort\"); for (int i = 0; i < bsort.length; i++) System.out.printf(\"%5d\",isort[i]); System.out.println(\"\ Before selection sort\"); for (int i = 0; i < arr3.length; i++) System.out.printf(\"%5d\",arr3[i]); int ssort[]=Sorter.insertionSort(arr3); System.out.println(\"\ After selection sort\"); for (int i = 0; i < ssort.length; i++) System.out.printf(\"%5d\",ssort[i]); int arr4[]={22,36,6,79,26,45,75,13,31,62,27,76,33,16,62,47}; System.out.println(\"\ Before quicksort sort\"); for (int i = 0; i < arr4.length; i++) System.out.printf(\"%5d\",arr4[i]); Sorter.quickSort(arr4, 0, arr4.length-1); System.out.println(\"\ After quicksort sort\"); for (int i = 0; i < arr4.length; i++) System.out.printf(\"%5d\",arr4[i]); } } ---------------------------------------------------------------------------------------------------------------- Sample output: Before bubble sort 1 7 3 2 0 5 0 8 After bubble sort 0 0 1 2 3 5 7 8 Before insertion sort 1 7 3 2 0 5 0 8 After i.