CP164 Lecture Notes - Lecture 4: Selection Sort, High High, Merge Sort

100 views14 pages
1 Aug 2018
School
Course
Professor

Document Summary

# imports from math import log from bst_linked import bst class sorts: Uses class attribute "swaps" to determine how many times elements are swapped by the class. Use: sorts. swaps = 0 swaps = 0 # tracks swaps performed. Sorts an array using the selection sort algorithm. Preconditions: a - a python list of comparable elements (?) Contents of a are sorted. n = len(a) for i in range(n): # walk through entire array m = i j = i + 1 for j in range(i + 1, n): # find smallest value in unsorted part of array if a[m] > a[j]: # track smallest value so far m = j if m != i: Sorts an array using the insertion sort algorithm. Contents of a are sorted. n = len(a) for i in range(1, n): # move each key bigger than save in a[1:n-1] one space to right. save = a[i]

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents