Notes
Slide Show
Outline
1
Quadratic Sorting
  • Chapter 12 presents several common algorithms for sorting an array of integers.
  • Two slow but simple algorithms are Selectionsort and Insertionsort.
  • This presentation demonstrates how the two algorithms work.
2
Sorting an Array of Integers
  • The picture shows an array of six integers that we want to sort from smallest to largest
3
The Selectionsort Algorithm
  • Start by finding the smallest entry.
4
The Selectionsort Algorithm
  • Start by finding the smallest entry.
  • Swap the smallest entry with the first entry.
5
The Selectionsort Algorithm
  • Start by finding the smallest entry.
  • Swap the smallest entry with the first entry.
6
The Selectionsort Algorithm
  • Part of the array is now sorted.
7
The Selectionsort Algorithm
  • Find the smallest element in the unsorted side.
8
The Selectionsort Algorithm
  • Find the smallest element in the unsorted side.
  • Swap with the front of the unsorted side.
9
The Selectionsort Algorithm
  • We have increased the size of the sorted side by one element.
10
The Selectionsort Algorithm
  • The process continues...
11
The Selectionsort Algorithm
  • The process continues...
12
The Selectionsort Algorithm
  • The process continues...
13
The Selectionsort Algorithm
  • The process keeps adding one more number to the sorted side.
  • The sorted side has the smallest numbers, arranged from small to large.
14
The Selectionsort Algorithm
  • We can stop when the unsorted side has just one number, since that number must be the largest number.
15
The Selectionsort Algorithm
  • The array is now sorted.
  • We repeatedly selected the smallest element, and moved this element to the front of the unsorted side.
16
The Insertionsort Algorithm
  • The Insertionsort algorithm also views the array as having a sorted side and an unsorted side.
17
The Insertionsort Algorithm
  • The sorted side starts with just the first element, which is not necessarily the smallest element.
18
The Insertionsort Algorithm
  • The sorted side grows by taking the front element from the unsorted side...
19
The Insertionsort Algorithm
  • ...and inserting it in the place that keeps the sorted side arranged from small to large.
20
The Insertionsort Algorithm
  • In this example, the new element goes in front of the element that was already in the sorted side.
21
The Insertionsort Algorithm
  • Sometimes we are lucky and the new inserted item doesn't need to move at all.
22
The Insertionsort Algorithm
  • Sometimes we are lucky twice in a row.
23
How to Insert One Element
  • Copy the new element to a separate location.
24
How to Insert One Element
  • Shift elements in the sorted side, creating an open space for the new element.
25
How to Insert One Element
  • Shift elements in the sorted side, creating an open space for the new element.
26
How to Insert One Element
  • Continue shifting elements...
27
How to Insert One Element
  • Continue shifting elements...
28
How to Insert One Element
  • ...until you reach the location for the new element.
29
How to Insert One Element
  • Copy the new element back into the array, at the correct location.
30
How to Insert One Element
  • The last element must also be inserted. Start by copying it...
31
A Quiz
  • How many shifts will occur before we copy this element back into the array?
32
A Quiz
  •  Four items are shifted.
33
A Quiz
  •  Four items are shifted.
  • And then the element is copied back into the array.
34
   Timing and Other Issues
  • Both Selectionsort and Insertionsort have a worst-case time of O(n2), making them impractical for large arrays.
  • But they are easy to program, easy to debug.
  • Insertionsort also has good performance when the array is nearly sorted to begin with.
  • But more sophisticated sorting algorithms are needed when good performance is needed in all cases for large arrays.
35
THE  END