sorting
play

Sorting Sorting used as a step in many algorithms Savitch Chapter - PDF document

Why sort Easier to search (binary search) Sorting Sorting used as a step in many algorithms Savitch Chapter 7.4 Sorting algorithms Selection Sort There are many algorithms for sorting: Find the smallest item Selection sort


  1. Why sort  Easier to search (binary search) Sorting  Sorting used as a step in many algorithms Savitch Chapter 7.4 Sorting algorithms Selection Sort  There are many algorithms for sorting:  Find the smallest item  Selection sort  Put it in the first position  Insertion sort  Find the 2 nd smallest item  Bubble sort  Put it in the 2 nd position  Merge sort  Find the 3 rd smallest item  Heap sort  Put it in the 3 rd position  Radix sort ….  Quick sort  Stooge sort  Each has its advantages and disadvantages

  2. Selection Sort code Selection Sort code public void selectionSort (Comparable [] array){ public void selectionSort (Comparable [] array){ int min; int min; for (int i = 0; i < array.length-1; i++) { for (int i = 0; i < array.length-1; i++) { outer loop min = i; min = i; for (int j = i+1; j < array.length; j++){ for (int j = i+1; j < array.length; j++){ if (array[j].compareTo(array[min]) < 0) if (array[j].compareTo(array[min]) < 0) min = j; inner loop min = j; } } swap (array, min, i); swap (array, min, i); } } } } private void swap(Comparable[] array, int i, int j){ private void swap(Comparable[] array, int i, int j){ Comparable temp = array[i]; Comparable temp = array[i]; array[i] = array[j]; array[i] = array[j]; array[j] = temp; array[j] = temp; } } Loop Invariant for Selection Sort Loop Invariant for Selection Sort public void selectionSort (Comparable [] array){ public void selectionSort (Comparable [] array){ int min; int min; for (int i = 0; i < array.length-1; i++) { for (int i = 0; i < array.length-1; i++) { min = i; min = i; for (int j = i+1; j < array.length; j++){ for (int j = i+1; j < array.length; j++){ if (array[j].compareTo(array[min]) < 0) if (array[j].compareTo(array[min]) < 0) min = j; min = j; } } swap (array, min, i); swap (array, min, i); } } } } Invariants? Invariant: The elements array[0..i] are in sorted order

  3. Insertion sort Arranging a hand of cards  Works the same way you arrange your hand when playing cards.  Pick up a card and place it in your hand in the correct position relative to the cards you’re already holding. 7 5 7 Arranging a hand of cards Insertion Sort 7 K 5 7 7 5 1 5 6 7 7 5 5 6 7 K 7 2 5 6 7 8 K 3 5 7

  4. Insertion Sort (cont.) Insertion Sort (cont.) 6 5 7 6 K 7 5 K 8 6 5 7 5 8 7 K 1 1 5 7 6 6 7 5 K 8 5 5 6 7 7 K 2 2 3 3 5 6 7 6 7 8 K 5 Insertion Sort – more formally Insertion Sort – another example  insertion sort partitions the array into two regions: sorted, and unsorted  each iteration the sorted part grows by 1

  5. Insertion Sort Algorithm With a for loop public void insertionSort(Comparable[] array) { public void insertionSort(Comparable[] array) { for (int i = 1; i < array.length; i++) { for (int i = 1; i < array.length; i++) { Comparable temp = array[i]; Comparable temp = array[i]; int position = i; // shift larger values to the right // shift larger values to the right for (int position = i; while (position > 0 && position > 0 && array[position-1].compareTo(temp) > 0) { array[position-1].compareTo(temp) > 0; array[position] = array[position–1]; position--) { array[position] = array[position–1]; position--; } } // insert the current item // insert the current item array[position] = temp; array[position] = temp; } } } } Insertion Sort Algorithm Loop Invariant for Insertion Sort public void insertionSort(Comparable[] array) { public void insertionSort(Comparable[] array) { for (int i = 1; i < array.length; i++) { for (int i = 1; i < array.length; i++) { outer loop Comparable temp = array[i]; Comparable temp = array[i]; int position = i; int position = i; // shift larger values to the right while (position > 0 && while (position > 0 && array[position-1].compareTo(temp) > 0) { array[position-1].compareTo(temp) > 0) { array[position] = array[position–1]; inner loop array[position] = array[position–1]; position--; position--; } } array[position] = temp; // insert the current item } array[position] = temp; } } } Invariant : array[0…i-1] consists of elements originally in array[0…i-1] but in sorted order

  6. Loop Invariant for Insertion Sort Sorting Linked Lists  Accessing an element in a linked list takes time. public void insertionSort(Comparable[] array) { for (int i = 1; i < array.length; i++) {  Can you sort a linked list with Selection Sort or Comparable temp = array[i]; int position = i; Insertion Sort maintaining the same level of while (position > 0 && array[position-1].compareTo(temp) > 0) { efficiency as using arrays? array[position] = array[position–1]; position--; } array[position] = temp; } } Invariant : array[0…i-1] consists of elements originally in array[0…i-1] but in sorted order How is this different than in Selection Sort? Bubble Sort Bubble Sort  Compares neighboring elements, and swaps public void bubbleSort (Comparable [] array) { them if they are not in order for (int position = array.length-1; position>=0;  Effect: the largest value will “bubble” to the last position--) { for (int i = 0 ; i < position; i++) { position in the array. if (array[i].compareTo(array[i+1]) > 0)  Repeating the process will bubble the 2 nd to swap(array, i, i+1); } largest value to the 2 nd to last position in the array } }

  7. Bubble Sort Bubble Sort  Compares neighboring elements, and swaps public void bubbleSort (Comparable [] array) { outer loop them if they are not in order for (int position = array.length-1; position>=0; position--) {  Effect: the largest value will “bubble” to the last for (int i = 0 ; i < position; i++) { position in the array. if (array[i].compareTo(array[i+1]) > 0) inner loop  Repeating the process will bubble the 2 nd to swap(array, i, i+1); } largest value to the 2 nd to last position in the array } } Loop Invariant: After i iterations the largest i elements are in their correct sorted position Bubble Sort Stooge Sort public void bubbleSort (Comparable [] array) { public void stoogeSort(Comparable [] array, int i, int j) { for (int position = array.length-1; position>=0; if (array[i].compareTo(array[j]) > 0 ) { position--) { swap(array, i, j); for (int i = 0 ; i < position; i++) { } if (array[i].compareTo(array[i+1]) > 0) if (j – i > 1) { swap(array, i, i+1); int third = (j – i + 1) / 3; } stoogeSort(array, i, j-third); //first two thirds } stoogeSort(array, i + third, j);//second two thirds } stoogeSort(array, i, j-third); //first two thirds Inner Invariant: array[i] is the largest element in the } } first i elements in the array Outer Invariant: After i iterations the largest i public void stoogeSort(Comparable [] array) { elements are in their correct sorted position stoogeSort(array, 0, array.length – 1); }

  8. Sort Animations Search the net for sort animations

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend