cs 171 introduction to computer science ii algorithm
play

CS 171: Introduction to Computer Science II Algorithm Analysis + - PowerPoint PPT Presentation

CS 171: Introduction to Computer Science II Algorithm Analysis + Simple Sorting Li Xiong Today Algorithm Analysis (cont) Simple sorting algorithms Tilde Notation


  1. CS 171: Introduction to Computer Science II Algorithm Analysis + Simple Sorting Li Xiong

  2. Today � Algorithm Analysis (cont) � Simple sorting algorithms

  3. Tilde Notation � ������������������������������������������ � ���������������������������������������������� ������������������������ � � � + �������� � �� � ����� � ��������� �

  4. Big-Oh Notation � Given functions � � � �� and ������ � � � � , we say that � � � �� is �� � � � � � �� if there are ����� ����� positive constants � � and � � such that � and � � such that ��� � � � � ≤ �� � � ��� for �� ≥ � � �� � Example: � � + �� is � � � � � � �� ��� ����� � pick �� = �� and � �� = �� �

  5. Important Functions in Big-Oh Analysis � Constant: � � Logarithmic: log � � Linear: � � N-Log-N: �� log � � Quadratic: � � � Cubic: � � � Cubic: � � � Polynomial: � � � Exponential: � � � Factorial: ��

  6. Practical method for Big-Oh Analysis � Write down cost function � � � �� 1.Look for highest-order term (tilde notation) 2.Drop constant factors � Examples � �� � ����� � ��� � ������������

  7. Common notations for algorithm analysis

  8. Useful Approximations � Harmonic sum 1 + 1/2 + 1/3 + … + 1/N ~ lnN � Triangular sum 1 + 2 + 3 + … + N = N(N+1)/2 ~ N 2 /2 1 + 2 + 3 + … + N = N(N+1)/2 ~ N /2 � Geometric sum 1 + 2 + 4 + … + N = 2N -1 ~ 2N when N = 2 n � Stirling’s approximation lg N! = lg1 + lg2 + lg3 + … + lgN ~ NlgN

  9. Common order-of-growth classifications

  10. Practical implications of Order-or-growth

  11. Example 4 ��� ���� � � �� � � �� � ��� � ��� ���� � � �� � � �� � ��� � ��� �� ���� � �

  12. Example 4 ��� ���� � � �� � � �� � ��� � ��� ���� � � �� � � �� � ��� � ��� �� ���� � � � � � + �� � + � � − �� + � � − �� + ��� + � + � = ��� ���� � 0.5 ( n 2 + n) � O(n 2 )

  13. Example 5 ������ ������� � ���� ��� ���� � � �� � �� �� � �� �� � ������� �� �� �

  14. Example 5: Solution ������ ������� � ���� ��� ���� � � �� � �� �� � �� �� � ������� �� �� � This has a logarithmic cost: � ���� � � � or � ���� � � as the change of base is merely a matter of a constant factor.

  15. Example 6 ������ ������� � ���� ��� ���� � � �� � �� �� � �� �� � ��� ���� � � �� � �� �� � ��� � ������� �� �� � �

  16. Example 6 What about this: ������ ������� � ���� ��� ���� � � �� � �� �� � �� �� � ��� ���� � � �� � �� �� � ��� � ������� �� �� � � � + � + ! + " + ��� + � �� � � � �

  17. Review Question � What is the Order of growth (big-oh) of the following code? for (int i=1; i<=N; ++i){ for (int j=1; j<=N; j*=2){ count++; count++; } }

  18. Search in Ordered vs. Unordered Array �������������������������������������������� � ��������������

  19. Search in Ordered vs. Unordered Array ��������������������������������������������� ���� � ��������������������� Binary search has much better running time, Binary search has much better running time, particularly for large-scale problems

  20. Today � Algorithm Analysis (cont) � Simple sorting algorithms

  21. Sorting problem

  22. Sorting Problem

  23. Sorting Problem How do you sort a hand of poker cards?

  24. Simple sort � Bubble sort � Selection sort � Insertion sort

  25. Two useful sorting abstractions

  26. Bubble Sort Intuition: # Find the biggest number. # Find the second biggest number. # Find the third biggest number. # Find the third biggest number. # … This gives you an ordering of the numbers. Bubble sort achieves this by repeatedly swapping two adjacent numbers.

  27. Bubble Sort After one pass, we find the biggest number. It’s like the biggest ‘bubble’ floats to the top of the surface, hence the name ‘bubble sort’.

  28. Bubble Sort In the second pass, we repeat the same process, but now we only have N-1 numbers to work on. The third pass is the same, with only N-2 The third pass is the same, with only N-2 numbers. … Repeat until all players are in order.

  29. Analysis of Bubble Sort Number of comparisons? Number of swaps? Number of swaps?

  30. Analysis of Bubble Sort Number of comparisons? � � � − �� = � � � � � � Number of swaps? Number of swaps? best case: � ��� � � � − �� = � � � � � worst cast: � � � � − �� average: = � � � � � !

  31. Selection Sort 1. Keep track of the index of the smallest number in each round. 2. Swap the smallest number towards the beginning of the array. beginning of the array. 3. Repeat the above two steps.

  32. Selection Sort

  33. Selection Sort

  34. Selection Sort Implementation

  35. Selection Sort � Online demo � http://www.sorting-algorithms.com/selection-sort � Gypsy dance demo � http://www.youtube.com/watch?v=Ns4TPTC8whw

  36. Selection Sort Number of comparisons? Number of swaps?

  37. Selection Sort Number of comparisons? � � � � � � � � � � Number of swaps? � � � �

Recommend


More recommend