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

intro to sorting algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Intro to Sorting Algorithms

CSSE 221 Fundamentals of Software Engineering Honors Rose-Hulman Institute of Technology

slide-2
SLIDE 2

Sorting

Understanding the concepts of sorting a collection

slide-3
SLIDE 3

Sorting

  • Ways to arrange data into sorted order

2 3 1 1 2 3

slide-4
SLIDE 4

Sorted Order

  • What is sorted order?

– Numeric (int)? – Alphabetical (String)?

  • Depends on CompareTo() method

– from Comparable interface

slide-5
SLIDE 5

Algorithms

  • Examples:

– Bubble Sort – Selection Sort – Insertion Sort – Merge Sort – Quick Sort

Arrays.sort()

Slow Fast

slide-6
SLIDE 6

Selection Sort

How to use and implement a selection sort algorithm

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 9

Example

Given • [4, 2, 6, 1, 7, 2] Swap 1 • [1, 2, 6, 4, 7, 2] 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]

Min value Swap

slide-10
SLIDE 10

Insertion Sort

How to use and implement an insertion sort

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 13

Example

Given • [4, 2, 6, 1, 7, 2] Swap 1 • [2, 4, 6, 1, 7, 2] Swap 2 • [2, 4, 6, 1, 7, 2] Swap 3 • [1, 2, 4, 6, 7, 2] Swap 4 • [1, 2, 4, 6, 7, 2] Swap 5 • [1, 2, 2, 4, 6, 7]

Shifted region Insertion

slide-14
SLIDE 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

  • O(n2)
  • O(n2)

Sorted

  • O(n2)
  • O(n)

Selection Sort Insertion Sort

slide-15
SLIDE 15

Demo

Examining the properties of Insertion and Selection sorts

slide-16
SLIDE 16

Activity

Implementing sorting algorithms