Sort: arrange values into an order such as Alphabetical, Ascending numeric, or Descending numeric Two algorithms considered here : • Bubble sort • Selection sort
sort public static void bubbleSort(int[] array1) { boolean swap; do { swap = false; for (int index=0;index<=array1.length-2;index++) { // compare element at index and at index+1 if (array1[index] > array1[index+1]) { //swap them if they are not in the order int temp = array1[index+1]; array1[index+1] = array1[index]; array1[index] = temp; swap = true } } } while (swap == true); }
sort public static void bubbleSort(int[] array1) { boolean swap; do { swap = false; for (int index=0;index<=array1.length-2;index++) { // compare element at index and at index+1 if (array1[index] > array1[index+1]) { //swap them if they are not in the order int temp = array1[index+1]; array1[index+1] = array1[index]; array1[index] = temp; swap = true ; } } } while (swap == true); }
Efficient Bubble sort • Notice that after the 1st pass, the largest element is placed in its final position (right most location). • That indicates that after the 2nd pass, the second largest element will be placed in its final position and so on. • So we can reduce the number of comparisons by 1 for every pass. • If we have n elements in the array, then the 1st pass requires (n-1) comparisons, the second pass requires (n-2) comparisons, and so on.
Efficient Bubble sort //More efficient version of Bubble Sort public static void bubbleSort2(int[ ] array1) { for(int position=array1.length-2; position>=0; position--) { for (int index=0; index<=position; index++) { //compare the element at index and at index+1 if (array1[index] > array1[index+1]){ //swap them if they are not in the order int temp = array1[index+1]; array1[index+1] = array1[index]; array1[index] = temp; } } } }
1. Selection Sort locates the smallest element in the array and exchanges it with the element in position 0. 2. It locates the next smallest element in the array and exchanges it with the element in position 1. 3. It continues until all elements are in order.
of Objects Student [] array = new Student[4]; array[0] = new Student(”Robert”, 4); array[1] = new Student(”Mary”, 1); array[2] = new Student(”Alice”, 3); array[3] = new Student(”John”, 2); John Mary Alice Robert 4 1 3 2
of Objects | swap // int temp = data[index_1]; // data[index_1] = data[index_2]; // data[index_2] = temp; How to do this for arrays of objects? John Mary Alice Robert 4 1 3 2
about “Selection Sort” for A of O? // Do changes in the highlighted places public static void selectionSort (int[] data) { int minIndex; for (int index=0; index<data.length-1; index++) { minIndex = index; for (int scan=index+1;scan<data.length; scan++){ if (data[scan] < data[minIndex]) { minIndex = scan; } } //swap the two elements, data[minIndex] and data[index] int temp = data[minIndex]; data[minIndex] = data[index]; data[index] = temp; } }
about “Binary Search” for A of O? // Do changes in the highlighted places public static int binarySearch (int[] data, int target) { int start = 0, end = data.length-1, middle; int position = -1; //return -1 if not found boolean found = false; while (found == false && start <= end) { middle = (start+end)/2; if (data[middle] == target){ found = true; position = middle; } else if (data[middle] > target) end = middle - 1; else start = middle + 1; } return position; //return -1 if not found }