sorting arrays 12

59
Sorting Bhargab Kakati

Upload: swapnanilpt

Post on 24-Jan-2016

217 views

Category:

Documents


0 download

DESCRIPTION

class 12 cs

TRANSCRIPT

Page 1: Sorting Arrays 12

Sorting

Bhargab Kakati

Page 2: Sorting Arrays 12

Introduction

• Common problem: sort a list of values, starting from lowest to highest. – List of exam scores

– Words of dictionary in alphabetical order

– Students names listed alphabetically

– Student records sorted by ID#

• Generally, we are given a list of records that have keys. These keys are used to define an ordering of the items in the list.

Page 3: Sorting Arrays 12

Sorting Algorithms

• We are given n records to sort.

• Algorithms to study:– Selection sort– Insertion sort– Bubble sort

Page 4: Sorting Arrays 12

Sorting an Array of IntegersSorting an Array of Integers

• Example: we are given an array of six integers that we want to sort from smallest to largest

0

10

20

30

40

50

60

70

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

Page 5: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Start by finding the smallest entry.

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

Page 6: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Swap the smallest entry with the first entry.

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

Page 7: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Swap the smallest entry with the first entry.

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

Page 8: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Part of the array is now sorted.

Sorted side Unsorted side

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

Page 9: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Find the smallest element in the unsorted side.

Sorted side Unsorted side

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

Page 10: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

• Swap with the front of the unsorted side.

Sorted side Unsorted side

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

Page 11: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

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

Sorted side Unsorted side

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

Page 12: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

• The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

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

Page 13: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

• The process continues...

Sorted side Unsorted side

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

Swap

with

front

Swap

with

front

Page 14: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort Algorithm

• The process continues...

Sorted side Unsorted sideSorted side

is bigger

Sorted sideis bigger

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

Page 15: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort 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 16: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort 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 17: Sorting Arrays 12

The Selection Sort AlgorithmThe Selection Sort 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 18: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

• The Insertion Sort algorithm also views the array as having a sorted side and an unsorted side.

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

Page 19: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort 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 20: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort 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 21: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort 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 22: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort Algorithm

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

Sorted side Unsorted side

Page 23: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Insertion Sort AlgorithmThe Insertion Sort 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 24: Sorting Arrays 12

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 25: Sorting Arrays 12

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 26: Sorting Arrays 12

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 27: Sorting Arrays 12

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 28: Sorting Arrays 12

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 29: Sorting Arrays 12

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 30: Sorting Arrays 12

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 31: Sorting Arrays 12

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 32: Sorting Arrays 12

How to Insert One ElementHow to Insert One Element

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

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

Sorted side Unsorted side

Page 33: Sorting Arrays 12

Sorted ResultSorted Result

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

Page 34: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 35: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 36: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 37: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 38: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 39: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 40: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 41: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 42: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 43: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 44: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.

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

Page 45: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 46: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 47: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 48: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 49: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 50: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 51: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Repeat.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 52: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 53: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 54: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 55: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 56: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? Yes.

Page 57: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 58: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Loop over array n-1 times, swapping pairs of entries as needed.

[0] [1] [2] [3] [4] [5] Swap? No.

Page 59: Sorting Arrays 12

0

10

20

30

40

50

60

70

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

The Bubble Sort AlgorithmThe Bubble Sort Algorithm

• Continue looping, until done.

[0] [1] [2] [3] [4] [5] Swap? Yes.