sorting - insertion and bubble sortvishy/winter2016/notes/sorting.pdf · sorting insertion and...

20
Sorting Insertion and Bubble Sort S.V . N. (vishy) Vishwanathan University of California, Santa Cruz [email protected] January 6, 2016 S.V . N. Vishwanathan (UCSC) CMPS101 1 / 19

Upload: duongnhu

Post on 12-Mar-2018

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

SortingInsertion and Bubble Sort

S.V.N. (vishy) Vishwanathan

University of California, Santa [email protected]

January 6, 2016

S.V.N. Vishwanathan (UCSC) CMPS101 1 / 19

Page 2: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Sorting

Outline

1 Sorting

2 Insertion Sort

3 Bubble Sort

S.V.N. Vishwanathan (UCSC) CMPS101 2 / 19

Page 3: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Sorting

Sorting

Input

An array e.g., [1, 5, 6, 7, 3, 2, 1]

Output

Sorted array [1, 1, 2, 3, 5, 6, 7]

S.V.N. Vishwanathan (UCSC) CMPS101 3 / 19

Page 4: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Sorting

Formally

Input

An array e.g., [a1, a2, a3, . . . , an]

Output

A permuted version of the array [a′1, a′2, a

′3, . . . , a

′n] such that

a′1 ≤ a′2 ≤ a′3 ≤ . . . ≤ a′n

S.V.N. Vishwanathan (UCSC) CMPS101 4 / 19

Page 5: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Sorting

What is an Algorithm?

A sequence of well defined computational steps that

takes some inputtransforms it into an output (with a certain property)

Alternatively

An algorithm describes a specific computational procedure forachieving a specified input/output relationship

S.V.N. Vishwanathan (UCSC) CMPS101 5 / 19

Page 6: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Outline

1 Sorting

2 Insertion Sort

3 Bubble Sort

S.V.N. Vishwanathan (UCSC) CMPS101 6 / 19

Page 7: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Game of Cards

S.V.N. Vishwanathan (UCSC) CMPS101 7 / 19

Page 8: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

On an Array

S.V.N. Vishwanathan (UCSC) CMPS101 8 / 19

Page 9: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Insertion Sort

Insertion-Sort(A)

1 for j = 2 to A. length2 key = A[j ]3 // Insert A[j ] into the sorted sequence A[1 . . j − 1].4 i = j − 15 while i > 0 and A[i ] > key6 A[i + 1] = A[i ]7 i = i − 18 A[i + 1] = key

S.V.N. Vishwanathan (UCSC) CMPS101 9 / 19

Page 10: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Proving Correctness

Hint: Think of the property/invariant that the algorithm maintains.

S.V.N. Vishwanathan (UCSC) CMPS101 10 / 19

Page 11: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Proving Correctness

Loop Invariant: At the start of each iteration of the for loop of lines1–8, the subarray A[1 . . j − 1] consists of the elements originally inA[1 . . j − 1], but in sorted order.

S.V.N. Vishwanathan (UCSC) CMPS101 11 / 19

Page 12: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Proof by Induction

Typically consists of three parts. You need to show that1 The property holds at the beginning (Initialization)2 The algorithm maintains the property in the intermediate stages

(Maintenance)3 If the property holds when the algorithm finishes, then it solves your

problem (Termination)

S.V.N. Vishwanathan (UCSC) CMPS101 12 / 19

Page 13: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Proof

Initialization

j = 2 which implies that A[1 . . j − 1] = A[1] is (trivially) sorted

Maintenance

The while loop ensures that all A[i ] > A[j ] are shifted by one positionto the left

This creates a “hole” in the array for inserting A[j ] such that

all elements to the right of A[j ] are smaller than A[j ]all elements to the left of A[j ] are larger than A[j ]

Since A[1 . . j − 1] was already sorted, so A[1 . . j ] is now sorted

Termination

j = n + 1 which implies that A[1 . . n] = A is sorted

S.V.N. Vishwanathan (UCSC) CMPS101 13 / 19

Page 14: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Insertion Sort

Exploration

Question: Can we speed up insertion sort?

S.V.N. Vishwanathan (UCSC) CMPS101 14 / 19

Page 15: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

Outline

1 Sorting

2 Insertion Sort

3 Bubble Sort

S.V.N. Vishwanathan (UCSC) CMPS101 15 / 19

Page 16: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

Bubble Sort

BubbleSort(A)

1 for 1 = 1 to A. length − 12 for j = A. length downto i + 13 if A[j ] ≤ A[j − 1]4 exchange A[j ] with A[j − 1]

S.V.N. Vishwanathan (UCSC) CMPS101 16 / 19

Page 17: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

On an Array

S.V.N. Vishwanathan (UCSC) CMPS101 17 / 19

Page 18: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

Proving Correctness

Hint: Think of the property/invariant that the algorithm maintains.

S.V.N. Vishwanathan (UCSC) CMPS101 18 / 19

Page 19: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

Proving Correctness

Loop Invariant: At the start of each iteration of the for loop of lines2-4, A[j ] = min {A[k] : j ≤ k ≤ n} and the subarray A[j . . n] is apermutation of the values that were in A[j . . n] at the time that theloop started.

S.V.N. Vishwanathan (UCSC) CMPS101 19 / 19

Page 20: Sorting - Insertion and Bubble Sortvishy/winter2016/notes/Sorting.pdf · Sorting Insertion and Bubble Sort S.V:N. (vishy) Vishwanathan University of California, Santa Cruz vishy@ucsc.edu

Bubble Sort

Questions?

S.V.N. Vishwanathan (UCSC) CMPS101 20 / 19