17
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Embed Size (px)

Citation preview

Page 1: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

CS 253: AlgorithmsChapter 2

Sorting

Insertion sort

Bubble Sort

Selection sort

Run-Time Analysis

Credit: Dr. George Bebis

Page 2: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

2

The Sorting ProblemInput:

A sequence of n numbers a1, a2, . . . , an

Output: A permutation (reordering) a1’, a2’, . . . , an’ of

theinput sequence such that a1’ ≤ a2’ ≤ · · · ≤ an’

Internal Sort - The data to be sorted is all stored in RAM.

External Sort - Data to be sorted does not fit in the RAM and therefore stored in an external storage device (e.g. HardDisk)

Page 3: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

To insert 12, we need to make room for it by moving first 36 and then 24.

Insertion Sort

6 10 24

12

36

Similar to sorting a hand of playing cards

Page 4: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

6 10 24

Insertion Sort

36

12

Page 5: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Insertion Sort

6 10 24 36

12

Page 6: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

6

Insertion Sort

5 2 4 6 1 3

input array

left sub-array right sub-array

at each iteration, the array is divided in two sub-arrays:

sorted unsorted

Page 7: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Insertion Sort

Page 8: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Alg.: INSERTION-SORT(A)for j ← 2 to ndo key ← A[ j ] % Insert A[ j ] into the sorted sequence A[1 . . j -1]

i ← j - 1 while i > 0 and A[i] > keydo A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key

Insertion sort – sorts the elements in place

a8a7a6a5a4a3a2a1

1 2 3 4 5 6 7 8

key

Page 9: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Operation count for Insertion Sort

times

n n-1 n-1 n-1 n-1

n

j jt2

n

j jt2

)1(

n

j jt2

)1(

)1(11)1()1()( 82

72

62

5421

nctctctcncncncnTn

jj

n

jj

n

jj

tj: # of times the while statement is executed at iteration j

INSERTION-SORT(A)for j ← 2 to n

do key ← A[ j ] % Insert A[ j ] into the sorted

….

i ← j - 1 while i > 0 and A[i] >

keydo A[i + 1] ← A[i] i ← i – 1

A[i + 1] ← key

cost c1

c2

0 c4

c5

c6

c7

c8

Page 10: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Best Case Analysis

The array is already sorted

A[i] ≤ key upon the first time the while loop test is

run

then (tj = 1) and

nnnT

knk

ccccnccccc

nctctctcncncncnTn

jj

n

jj

n

jj

isgrowth of Rate )()(

)()(

)1(11)1()1()(

21

854285421

82

72

62

5421

Page 11: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Worst Case Analysis

The array is sorted in reverse order◦ Always A[i] > key in while loop test

◦ Have to compare key with all elements to the left of

the jth position compare with j-1 elements tj = j

1 2 4 5 6 7 82 2 2

1 2 2

1 2 4 5 6

( ) ( 1) ( 1) 1 1 ( 1)

( 1) ( 1) ( 1)Since and -1 and ( 1)

2 2 2

( 1) ( 1)( ) ( 1) ( 1) ( 1) ( )

2 2

n n n

j j j

n n n

j j j

T n c n c n c n c j c j c j c n

n n n n n nj j j

n n n nT n c n c n c n c c

7 8

2 21 2 3

( 1) ( ) ( 1)

2

( ) ( ) ( )

n nc c n

T n k n k n k T n n

Page 12: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Bubble Sort

Swaps adjacent elements that are out of order

Repeatedly pass through the array

Simpler, but slower than Insertion sort

1 2 3 n

i

1329648

j

Page 13: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Bubble Sort Example

1329648i = 1 j

3129648i = 1 j

3219648

i = 1 j

3291648i = 1 j

3296148i = 1 j

3296418

i = 1 j

3296481

i = 1 j

3296481

i = 2 j

3964821

i = 3 j

9648321

i = 4 j

9684321

i = 5 j

9864321

i = 6 j

9864321

i = 7j

Page 14: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Bubble Sort

Alg.: BUBBLESORT(A)for i 1 to length[A]

do for j length[A] downto i + 1 do if A[j] < A[j -1]

then exchange A[j] A[j-1]

1329648i = 1 j

i

Page 15: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Bubble-Sort Running Time

Alg.: BUBBLESORT(A)for i 1 to length[A]

do for j length[A] downto i + 1 do if A[j] < A[j -1]

then exchange A[j] A[j-1]

Comparisons and Exchanges: n2/2

c1

c2

c3

n

i

n

i

n

i

inccn

incincncnT

132

13

121

)()(

)1()1()(

)()( Therefore

222

)1( Since

2

22

111

nnT

nnnnninin

n

i

n

i

n

i

Page 16: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

Selection Sort Find the smallest element in the array and exchange it with the

element in the first position Find the second smallest element and exchange it with the element

in the second position Continue until the array is sorted

Example

1329648

8329641

8349621

8649321

8964321

8694321

9864321

9864321

Page 17: CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis

»n2/2 comparisons

Analysis of Selection Sort

Alg.: SELECTION-SORT(A)

n ← length[A]

for j ← 1 to n - 1

do smallest ← j

for i ← j + 1 to n

do if A[i] < A[smallest]

then smallest ← i

exchange A[j] ↔ A[smallest]

cost

times

c1 1

c2 n

c3 n-

1

c4

c5

c6 n-1

1

1)1(

n

jjn

1

1)(

n

jjn»n

exchanges

)()1()1()1()( 26

1

15

1

14321 nncjncjncncnccnT

n

j

n

j