Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0...

23
Selection Sort

Transcript of Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0...

Page 1: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort

Page 2: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort

10 4 3 0 11 8

0 1 2 3 4 5

What do we do first?

Page 3: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start0

Find minimum element idx between start to end

10 4 3 0 11 8

What next?

minValIdx3

Page 4: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start0

Swap the elements at start and minValIdx

0 4 3 10 11 8

What next?

minValIdx3

Page 5: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start1

Decrease the interval.

0 4 3 10 11 8

What next?

Page 6: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start1

Find minimum element between start to end

0 4 3 10 11 8

What next?

minValIdx2

Page 7: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start1

Swap the elements at start and minValIdx

0 3 4 10 11 8

What next?

minValIdx2

Page 8: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start2

Decrease the interval.

0 3 4 10 11 8

What next?

Page 9: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start2

Find minimum element idx between start to end

0 3 4 10 11 8

What next?

minValIdx2

Page 10: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start2

Swap the elements at start and minValIdx

0 3 4 10 11 8

What next?

minValIdx2

Page 11: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start3

Decrease the interval.

0 3 4 10 11 8

What next?

Page 12: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start3

Find minimum element idx between start to end

0 3 4 10 11 8

What next?

minValIdx5

Page 13: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

Swap the elements at start and minValIdx

0 3 4 8 11 10

What next?

start3

minValIdx5

Page 14: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start4

Decrease the interval.

0 3 4 8 11 10

What next?

Page 15: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start4

Find minimum element idx between start to end

0 3 4 8 11 10

What next?

minValIdx5

Page 16: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

Swap the elements at start and minValIdx

0 3 4 8 10 11

What next?

start4

minValIdx5

Page 17: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection Sort0 1 2 3 4 5

start5

Decrease the interval.

0 3 4 8 10 11

We’re done!

Page 18: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection sortselectionSort(L):

for startIdx in range(len(L)):

minIdx = findMinimum(startIdx, L)

swap(startIdx, minIdx, L)

Page 19: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection sortfindMinimum(startIdx, L):

minIdx = startIdx

for i in range(startIdx, len(L)):

if L[i] < L[minIdx]:

minIdx = i

return minIdx

Page 20: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3

Selection sortswap(i, j, L):

temp = L[i] # step 1

L[i] = L[j] # step 2

L[j] = temp # step 3

Temp i j

Temp i j

Temp i j

Temp i j

Page 21: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3
Page 22: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3
Page 23: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3