35
Two slow but simple algorithms are Selectionsort and Insertionsort . This presentation demonstrates how the two algorithms work. Quadratic Sorting Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

Embed Size (px)

Citation preview

Page 1: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

Two slow but simple algorithms are Selectionsort and Insertionsort.

This presentation demonstrates how the two algorithms work.

Quadratic SortingQuadratic Sorting

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

Sorting an Array of IntegersSorting an Array of Integers

The picture shows an array of six integers that we want to sort from smallest to largest

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]

Page 3: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

[0] [1] [2] [3] [4] [5]

Page 4: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 5: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Start by finding the smallest entry.

Swap the smallest entry with the first entry.

[0] [1] [2] [3] [4] [5]

Page 6: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Selectionsort AlgorithmThe Selectionsort Algorithm

Part of the array is now sorted.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 7: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

Find the smallest element in the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 8: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

Find the smallest element in the unsorted side.

Swap with the front of the unsorted side.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 9: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

We have increased the size of the sorted side by one element.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 10: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

[0] [1] [2] [3] [4] [5]

Page 11: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Swap

with

front

Swap

with

front

Page 12: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

The process continues...

Sorted side Unsorted sideSorted side

is bigger

Sorted sideis bigger

[0] [1] [2] [3] [4] [5]

Page 13: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe 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.

Sorted side Unsorted side

[0] [1] [2] [3] [4] [5]

Page 14: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

We can stop when the unsorted side has just one number, since that number must be the largest number.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 15: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

The Selectionsort AlgorithmThe Selectionsort Algorithm

The array is now sorted.

We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]

Page 16: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The Insertionsort algorithm also views the array as having a sorted side and an unsorted side. [0] [1] [2] [3] [4] [5]

Page 17: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The sorted side starts with just the first element, which is not necessarily the smallest element. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 18: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

The sorted side grows by taking the front element from the unsorted side...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 19: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

...and inserting it in the place that keeps the sorted side arranged from small to large. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 20: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

In this example, the new element goes in front of the element that was already in the sorted side. [0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 21: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

Sometimes we are lucky and the new inserted item doesn't need to move at all.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 22: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

The Insertionsort AlgorithmThe Insertionsort Algorithm

Sometimes we are lucky twice in a row.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 23: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element to a separate location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 24: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 25: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Shift elements in the sorted side, creating an open space for the new element.

[0] [1] [2] [3] [4] [5]

Page 26: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 27: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Continue shifting elements...

[0] [1] [2] [3] [4] [5]

Page 28: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

...until you reach the location for the new element.

[0] [1] [2] [3] [4] [5]

Page 29: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

[1] [2] [3] [4] [5] [6]0

10

20

30

40

50

60

70

[1] [2] [3] [4] [5] [6]

How to Insert One ElementHow to Insert One Element

Copy the new element back into the array, at the correct location.

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 30: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

How to Insert One ElementHow to Insert One Element

The last The last element element must also be must also be inserted. inserted. Start by Start by copying it...copying it...

[0] [1] [2] [3] [4] [5]

Sorted side Unsorted side

Page 31: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

Merge SortMerge Sort

7777 4444 9999 6666 3333 5858 8888 2222

7777 4444 9999 6666 3333 5858 8888 2222

7777 4444 9999 6666 3333 5858 8888 2222

7777 4444 9999 6666 3333 5858 8888 2222

4444 7777 6666 9999 3333 5858 2222 8888

4444 6666 7777 9999 2222 3333 5858 8888

2222 2323 4444 5555 6767 7777 8888 9999

Page 32: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

A QuizA Quiz

How many shifts will occur before we copy this element back into the array?

[0] [1] [2] [3] [4] [5]

Page 33: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

A QuizA Quiz

Four items Four items are shifted.are shifted.

[0] [1] [2] [3] [4] [5]

Page 34: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

A QuizA Quiz

Four items Four items are shifted.are shifted.And then the And then the element is element is copied back copied back into the array.into the array.

[0] [1] [2] [3] [4] [5]

Page 35: P p Two slow but simple algorithms are Selectionsort and Insertionsort. p p This presentation demonstrates how the two algorithms work. Quadratic Sorting

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.

Timing and Other Issues Timing and Other Issues