Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 28

CSE110 Lecture 28

Principles of Programming with Java
Sorting
(202007)

Javier Gonzalez-Sanchez
PRO

June 28, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

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

    View Slide

  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

    View Slide

  3. Bubble Sort

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

  11. Selection Sort

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. Sorting Arrays of Objects

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide