550

# CSE110 Lecture 28

Principles of Programming with Java
Sorting
(202007)

June 28, 2017

## Transcript

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

2. 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

3. Bubble Sort

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

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

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

7. 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);
}

8. 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);
}

9. 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.

10. 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;
}
}
}
}

11. Selection Sort

12. 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.

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

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

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

16. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16
Selection Sort
public static void selectionSort (int[] data) {
int minIndex;
for (int index=0; indexminIndex = index;
for (int scan=index+1;scanif (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;
}
}

17. Sorting Arrays of Objects

18. 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

19. 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

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

21. 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; indexminIndex = index;
for (int scan=index+1;scanif (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;
}
}

22. 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;
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;
}