intro to sorting algorithms
play

Intro to Sorting Algorithms CSSE 221 Fundamentals of Software - PowerPoint PPT Presentation

Intro to Sorting Algorithms CSSE 221 Fundamentals of Software Engineering Honors Rose-Hulman Institute of Technology Understanding the concepts of sorting a collection Sorting Sorting Ways to arrange data into sorted order 2 3 1 2 1


  1. Intro to Sorting Algorithms CSSE 221 Fundamentals of Software Engineering Honors Rose-Hulman Institute of Technology

  2. Understanding the concepts of sorting a collection Sorting

  3. Sorting • Ways to arrange data into sorted order 2 3 1 2 1 3

  4. Sorted Order • What is sorted order? – Numeric (int)? – Alphabetical (String)? • Depends on CompareTo() method – from Comparable interface

  5. Algorithms • Examples: – Bubble Sort Slow – Selection Sort – Insertion Sort – Merge Sort – Quick Sort Fast Arrays.sort()

  6. How to use and implement a selection sort algorithm Selection Sort

  7. Selection Overview • Finds the element of lowest value by searching the entire list • Then swaps the lowest value with the current first element • Same efficiency regardless of the collection’s initial state

  8. Selection Process 1. Start from the beginning 2. Search the collection for the element which should be placed at the start 3. Swap the found element with the current first element 4. Repeat the process starting from element at second index 5. Continue until starting index is the last element

  9. Example Given • [4, 2, 6, 1, 7, 2] Min value Swap 1 • [1, 2, 6, 4, 7, 2] Swap Swap 2 • [1, 2, 6, 4, 7, 2] Swap 3 • [1, 2, 2, 4, 7, 6] Swap 4 • [1, 2, 2, 4, 7, 6] Swap 5 • [1, 2, 2, 4, 6, 7]

  10. How to use and implement an insertion sort Insertion Sort

  11. Insertion Overview • Looks at an element and those left of it • Then shifts all elements of higher value to the right and inserts the element • Continues until the collection is full • Efficiency changes based on initial state of the collection

  12. Insertion Process 1. Start with the second element 2. Look to the left 3. Elements of higher value than selected element shift right 4. Insert element before those shifted 5. Repeat for each index in the collection until the end is reached

  13. Example Given • [4, 2, 6, 1, 7, 2] Swap 1 • [2, 4, 6, 1, 7, 2] Shifted region Swap 2 • [2, 4, 6, 1, 7, 2] Insertion Swap 3 • [1, 2, 4, 6, 7, 2] Swap 4 • [1, 2, 4, 6, 7, 2] Swap 5 • [1, 2, 2, 4, 6, 7]

  14. Efficiency (Big-Oh Analysis) • Selection & Insertion have the same efficiency when a collection is unsorted • But Insertion works faster on a partially sorted array Unsorted Sorted • O(n 2 ) • O(n 2 ) Selection Sort Insertion Sort • O(n 2 ) • O(n)

  15. Examining the properties of Insertion and Selection sorts Demo

  16. Implementing sorting algorithms Activity

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