Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Class 25: Sorting and Searching (guest lecture by Yuchi Tian)

Class 25: Sorting and Searching (guest lecture by Yuchi Tian)

cs1120: Introduction to Computing
Explorations in Language, Logic, and Machine
University of Virginia, Spring 2016

http://xplorecs.org/class25

Class 25:
Problems and Algorithms
Sorting
Selection Sort
Best-First Sort
Insertion Sort

David Evans

March 30, 2016
Tweet

More Decks by David Evans

Other Decks in Programming

Transcript

  1. Algorithm vs. Computer program Algorithm: a systematic approach for solving

    a specific problem. Computer program: a code for solving a problem but written in a language understandable by computer or compiler. a specific implementation of an algorithm.
  2. Properties of an algorithm finiteness: The algorithm must always terminate

    after a finite number of steps. definiteness: Each step must be precisely defined. input: An algorithm has zero or more inputs, taken from a specified set of objects. output: An algorithm has one or more outputs, which have a specified relation to the inputs. effectiveness: An algorithm must give correct answer (output).
  3. Sorting problem Input: list of elements, comparison function Output: new

    list containing the same elements A sorting algorithm is an algorithm that solves the sorting problem.
  4. Selection Sort def selection_sort(alist): for index in range(0, len(alist)): minpos

    = index minvalue = alist[index] for index1 in range(index, len(alist)): if (alist[index1] < minvalue): minpos = index1 minvalue = alist[index1] temp = alist[index] alist[index] = alist[minpos] alist[minpos] = temp
  5. What is the asymptotic running time of selection_sort? def selection_sort(alist):

    for index in range(0, len(alist)): minpos = index minvalue = alist[index] for index1 in range(index, len(alist)): if (alist[index1] < minvalue): minpos = index1 minvalue = alist[index1] temp = alist[index] alist[index] = alist[minpos] alist[minpos] = temp
  6. Best-First Sort def list_find_best (p): if p[1:]==[]: return 0 else:

    bestpos = list_find_best (p[1:]) if p[0] < p[1 + bestpos]: return 0 else: return 1 + bestpos def list_sort_best_first (p): if len(p)==0: return p else: bestpos = list_find_best (p) bestel = p.pop(bestpos) return [bestel] + \ list_sort_best_first (p)
  7. Time complexity for best-first sort Best case running time: Average

    case running time: Worst case running time:
  8. Insertion Sort def list_insert_one (el, p): if len(p)==0: return [el]

    if el < p[0]: return [el] + p return [p[0]] + list_insert_one (el, p[1:]) def list_sort_insert (p): if len(p)==0: return p return list_insert_one (p[0], list_sort_insert (p[1:]))
  9. Generalizing Comparison def list_insert_one (cf, el, p): if len(p)==0: return

    [el] if cf(el, p[0]): return [el] + p return [p[0]] + list_insert_one (el, p[1:]) def list_sort_insert (cf, p): if len(p)==0: return p return list_insert_one (cf, p[0], list_sort_insert (cf,p[1:]))
  10. Sorting Problem: What is the time complexity for sorting problem?

    Is sorting problem in O(n^2)? Is sorting problem in (n)? Is sorting problem in (n^2)?
  11. Permutation Sort import itertools def permutation_sort (alist): for i in

    itertools.permutations(alist): if (check_in_order (list(i))): return i def check_in_order (alist): for i in range(len(alist)-1): if alist[i]>alist[i+1]: return False return True
  12. Quicker Sort Is there any sorting algorithm faster than O(n^2)?

    Yes. Quick Sort O(nlogn). Merge Sort O(nlogn). Heap Sort O(nlogn). Bucket Sort O(m+n)