// APS101, Winter 2009: Lecture 33 (Mar. 31) // // Review: last time we developed an algorithm for sorting an array of integer elements in ascending order. Sort.bubbleSort(new int[]{5, 4, 3, 2, 1}); // worst case - descending order Sort.bubbleSort(new int[]{1, 2, 3, 4, 5}); // best case - already sorted // then, we made bubbleSort more efficient... Sort.bubbleSort2(new int[]{5, 4, 3, 2, 1}); // however, there are still better sorting algorithms out there... // Insertion sort: at each round we have a sorted array and we want to insert a new value into its correct position. Sort.insertionSort(new int[]{1, 2, 3, 4, 5}) Sort.insertionSort(new int[]{5, 4, 3, 2, 1}) Sort.insertionSort(new int[]{1, 2, 3, 3, 2, 1}) // intermediate case, with duplicates // Selection sort: at each round we find the minimum and put it at the begining. // (Note: in the demo, it finds the maximum and puts it at the end instead) Sort.selectionSort(new int[]{1, 2, 3, 4, 5}) Sort.selectionSort(new int[]{1, 2, 3, 3, 2, 1}) Sort.selectionSort(new int[]{5, 4, 3, 2, 1}) // a preliminary comparison of the sorting algorithms... Sort.bubbleSort(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) Sort.bubbleSort2(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) Sort.insertionSort(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) Sort.selectionSort(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}) // which one appears to be the "best"?