for (i = 1; i
count[i] += count[i - 1]; // Build the output array
for (i = n - 1; i >= 0; i--){
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
for (i = 0; i
arr[i] = output[i];
}
static void radixsort(int arr[], int n)
{ // Find the maximum number to know number of digits
int m = getMax(arr, n);
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp);
}
static void print(int arr[], int n) {
for (int i=0; i
System.out.print(arr[i]+" ");
}
public static void main (String[] args) {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = arr.length;
radixsort(arr, n);
print(arr, n);
}
}
ArraySize 50K elements Comparisons Movements Total Time Experimental Results List Property: InOrder Insertion Sort Selection Sort Quick Sort Merge Sort Heap Sort Radix Sort Total Time List Property: Reverse Order Comparisons Movements Insertion Sort Selection Sort Quick Sort Merge Sort Heap Sort Radix Sort Total Time List Property: AlmostOrder Comparisons Movements Insertion Sort Selection Sort Quick Sort Merge Sort Heap Sort Radix Sort Total Time List Property: RandomOrder Comparisons Movements Insertion Sort Selection Sort Quick Sort Merge Sort Heap Sort Radix Sort LISTING 23.8 QuickSort.java 1 public class QuickSort { public static void quickSort(int[] list) { 3 quickSort(list, 0, list. length - 1); Hoi NODO OWN 12 13 24 15 16 17 18 19 20 21 22 25 26 27 28 29 30 31 B2 33 34 35 86 37 38 39 30 31 2 33 34 35 56 37 18 39 public static void quickSort(int[] list, int first, int last) { if (last > first) { int pivotIndex - partition(list, first, last): quickSort(list, first, pivotIndex - 1); quickSortlist, pivotIndex + 1, last): 1 } *** Partition the array list[first..last) / public static int partition(int[] list, int first, int last) { int pivot - list[First]: // Choose the first element as the pivot int low - first = 1; // Index for foncard search int high - last: // Index for backward search while Chigh > low) { // Search forward from left while (low pivot) high--; // Swap two elements in the list if (high > low) { int temp = list[high]: list(high) - list[low): list[low] - temp; } } while (high > first && list[high) >- pivot) high-- // Swap pivot with list[high] if (pivot > list[high]) { list[first] - list[high); list[high) - pivot; return high; else { return first; ODNOSA 14 15 LISTING 23.1 InsertionSort.java 1 public class Insertionsort { 2 /** The method for sorting the numbers */ public static void insertionSort(int[] list) { for (int i = 1; i = 0 && list[k] > currentElement: (--) { 10 list[k + 1] = list[k]; 11 } 12 13 // Insert the current element into list[k + 1] list[k + 1] = currentElement; } 16 } 17 } Ref: Intro to Java Programming, Comprehensive Version / Edition 10 by Y. Daniel Liang LISTING 7.8 SelectionSort.java 1 public class SelectionSort { 2. /** The method for sorting the numbers / 3 public static void selection Sort(double[] list) { for (int i = 0; i list[j]) { 11 currentMin - list[j]: 12 currentMinIndex - j; 13 } 14 } 15 16 // Swap list[i] with list[currentMinIndex] if necessary 17 if (currentMinIndex != i) { 18 list[currentMinIndex] - list[i]; 19 list[i] - current Min; 20 21 22 23 } } LISTING 23.9 Heap.java public class Heap extends comparablece private java.util.ArrayList 1st - new java.util.ArrayListo: / Create a default heap 5 public Heap 6 * Create a heap from an array of objects * for (int ici
0) let parent Index - (current Index - 1) / 2: // Swap if the current object is greater than its pret if (list.get(current Index).compare To list.get(parent Index)) > 0) temp - list.getCurrentIndex): list.set(currentIndex, list.get(parentIndex) Tist.setCarentIndex, temp): else break; // The tree is a hapo Current Index - parent Index 38 39 40 41 42 43 /** Remove the root from the help / public remove if (hist.size(-) return null; EremovedObject - list.get): list.setCo. 1st.getlist.size-D): list.remove(ist.size - 1); Int current Index = 0; while (current Index > // Swap if the current mode is less than the ir (list.getCurrentIndex). Compare toc list.get(max Index))) templist.get(max Index); list.set(max Index. list.get(currentIndex)); 11st.setCurrentIndex, teme): current Index - max Index ) break; // The tree is a hea ) 61 62 63 64 65 66 67 68 69 70 71 73 74 75 76 77 ) return removedobject; 1 Set the number of rodes in the tree public int get Size return Tist.size(): LISTING 23.10 HeapSort.java 1 public class HeapSort public static & extends Comparable> void heap Sort (El) list) 4 1/ Create a Heap of integers 5 Heap heap - new Heapoo: // Add elements to the heap 8 for (int 1 - 0:1 Hist[1] - heap.removeO: } 15 "A test method public static void main(String[] args) { 18 Integer list - (-44.-5.3.3.1.1.4.0. 1. 2. 4. 5. 53): heapSort(list): 20 for (int 1 - 0 + 11 12 13 14 16 17 13 18 LISTING 23.6 MergeSort.java 1 public class MergeSort 2 The method for sorting the members 3 public static void merge Sortint st) if (list.Tength > 1 // Merge sort the first half int) firstHalf - new int[list.length / 2] Systen arraycopylist. o, firsthalf. , list.Tength / 2): mergeSort firsthalt: // Merge sort the second half int secondalflength - list. length - list. length / 23 into seconditalt new int [secondiulfiength): System arraycopy(list. list.length/. 14 secondHalf 0 secondital length); 15 mergeSort (secondita: 16 17 Merge firsthalf with seconditalf its ss merge(Firstatt. secondiale, list): 19 1 1 21 22 Merge two sorted lists 23 public static void merge into histlntTt2 inten) 24 int current 1 - // Current indexin 25 int current2 - 0; // Current index in tist2 26 int current3 - 0:// Current index in tem 27 28 while (currenti (); bucket[key].add(list[i]); } // Now move the elements from the buckets back to list int k = 0; // k is an index for list for (int i = 0; i first) { int pivotIndex - partition(list, first, last): quickSort(list, first, pivotIndex - 1); quickSortlist, pivotIndex + 1, last): 1 } *** Partition the array list[first..last) / public static int partition(int[] list, int first, int last) { int pivot - list[First]: // Choose the first element as the pivot int low - first = 1; // Index for foncard search int high - last: // Index for backward search while Chigh > low) { // Search forward from left while (low pivot) high--; // Swap two elements in the list if (high > low) { int temp = list[high]: list(high) - list[low): list[low] - temp; } } while (high > first && list[high) >- pivot) high-- // Swap pivot with list[high] if (pivot > list[high]) { list[first] - list[high); list[high) - pivot; return high; else { return first; ODNOSA 14 15 LISTING 23.1 InsertionSort.java 1 public class Insertionsort { 2 /** The method for sorting the numbers */ public static void insertionSort(int[] list) { for (int i = 1; i = 0 && list[k] > currentElement: (--) { 10 list[k + 1] = list[k]; 11 } 12 13 // Insert the current element into list[k + 1] list[k + 1] = currentElement; } 16 } 17 } Ref: Intro to Java Programming, Comprehensive Version / Edition 10 by Y. Daniel Liang LISTING 7.8 SelectionSort.java 1 public class SelectionSort { 2. /** The method for sorting the numbers / 3 public static void selection Sort(double[] list) { for (int i = 0; i list[j]) { 11 currentMin - list[j]: 12 currentMinIndex - j; 13 } 14 } 15 16 // Swap list[i] with list[currentMinIndex] if necessary 17 if (currentMinIndex != i) { 18 list[currentMinIndex] - list[i]; 19 list[i] - current Min; 20 21 22 23 } } LISTING 23.9 Heap.java public class Heap extends comparablece private java.util.ArrayList 1st - new java.util.ArrayListo: / Create a default heap 5 public Heap 6 * Create a heap from an array of objects * for (int ici 0) let parent Index - (current Index - 1) / 2: // Swap if the current object is greater than its pret if (list.get(current Index).compare To list.get(parent Index)) > 0) temp - list.getCurrentIndex): list.set(currentIndex, list.get(parentIndex) Tist.setCarentIndex, temp): else break; // The tree is a hapo Current Index - parent Index 38 39 40 41 42 43 /** Remove the root from the help / public remove if (hist.size(-) return null; EremovedObject - list.get): list.setCo. 1st.getlist.size-D): list.remove(ist.size - 1); Int current Index = 0; while (current Index > // Swap if the current mode is less than the ir (list.getCurrentIndex). Compare toc list.get(max Index))) templist.get(max Index); list.set(max Index. list.get(currentIndex)); 11st.setCurrentIndex, teme): current Index - max Index ) break; // The tree is a hea ) 61 62 63 64 65 66 67 68 69 70 71 73 74 75 76 77 ) return removedobject; 1 Set the number of rodes in the tree public int get Size return Tist.size(): LISTING 23.10 HeapSort.java 1 public class HeapSort public static & extends Comparable> void heap Sort (El) list) 4 1/ Create a Heap of integers 5 Heap heap - new Heapoo: // Add elements to the heap 8 for (int 1 - 0:1 Hist[1] - heap.removeO: } 15 "A test method public static void main(String[] args) { 18 Integer list - (-44.-5.3.3.1.1.4.0. 1. 2. 4. 5. 53): heapSort(list): 20 for (int 1 - 0 + 11 12 13 14 16 17 13 18 LISTING 23.6 MergeSort.java 1 public class MergeSort 2 The method for sorting the members 3 public static void merge Sortint st) if (list.Tength > 1 // Merge sort the first half int) firstHalf - new int[list.length / 2] Systen arraycopylist. o, firsthalf. , list.Tength / 2): mergeSort firsthalt: // Merge sort the second half int secondalflength - list. length - list. length / 23 into seconditalt new int [secondiulfiength): System arraycopy(list. list.length/. 14 secondHalf 0 secondital length); 15 mergeSort (secondita: 16 17 Merge firsthalf with seconditalf its ss merge(Firstatt. secondiale, list): 19 1 1 21 22 Merge two sorted lists 23 public static void merge into histlntTt2 inten) 24 int current 1 - // Current indexin 25 int current2 - 0; // Current index in tist2 26 int current3 - 0:// Current index in tem 27 28 while (currenti (); bucket[key].add(list[i]); } // Now move the elements from the buckets back to list int k = 0; // k is an index for list for (int i = 0; i