Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 28: Sorting Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 2 Sorting Sort: arrange values into an order such as Alphabetical, Ascending numeric, or Descending numeric Two algorithms considered here : • Bubble sort • Selection sort

Slide 3

Slide 3 text

Bubble Sort

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Bubble sort

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Bubble sort

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Bubble sort

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Bubble 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); }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Bubble 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); }

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 More 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.

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 More 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; } } } }

Slide 11

Slide 11 text

Selection Sort

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Algorithm 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.

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Example 1

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Example 1

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Example 2

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16 Selection Sort public static void selectionSort (int[] data) { int minIndex; for (int index=0; index

Slide 17

Slide 17 text

Sorting Arrays of Objects

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 18 Arrays 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

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19 Arrays 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

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20 Swap | Arrays of Objects What about Sort (Bubble or Selection)? Search (Linear or Binary)? John Mary Alice Robert 4 1 3 2

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21 What 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

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22 What 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 }

Slide 23

Slide 23 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.