notes13a

j lmax lj lj lmax complexity

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: put Output [4, 9, 3, 7, 6] [ ] [4, 3, 7, 6] [9] [4, 3, 6] [7, 9] [4, 3] [6, 7, 9] [8] [4, 6, 7, 9] [ ] [3, 4, 6, 7, 9] Python code for Selection Sort: def selection_sort(L): for i in range(len(L)): max = 0 for j in range(len(L)- i): if L[j] > L[max]: max = j if max != j: L[max], l[j] = L[j], L[max] Complexity: O(n^2) Insertion Sort 1. For all items in the list, insert the i- th item into the i- 1 sorted ones Example: [4, 9, 3, 7, 6] Start [4, 9, 3, 7, 6] Compare 9 to 4, if it is greater it goes after [4, 9, 3, 7, 6] *Red indicates list so far [4, 9, 3, 7, 6] Compare 3 with list so far [4, 9] [3, 4, 9, 7, 6] Since 3 < 4, it is placed in front of 4 …. [3, 4, 7, 9, 6] Compare 6 to list so far [3, 4, 6, 7, 9] 6 < 7, it is placed in front of 7...
View Full Document

Ask a homework question - tutors are online