Download presentation

Presentation is loading. Please wait.

Published bySamantha Quinn Modified over 4 years ago

1
Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool done = true; int tmp; for (int k = 0; k < top - 1; k++) { if (list[k] > list[k + 1]) { tmp = list[k+1]; list[k+1] = list[k]; list[k] = tmp; done = false; } return done; } rThis algorithm moves the largest item in the list to the top. rEvery time we run it the next biggest item moves to the top rConsider l 23 18 78 45 34 15 20 l After bubble_once l 18 23 45 34 15 20 78 l After another l 18 23 34 15 20 45 78 l After another l 18 23 15 20 34 45 78 rIf nothing moves we are done so we return true.

2
Lecture 1 -- 2Computer Science I - Martin Hardwick Bubble Sort Completed void bubble( vector &list) // Bubble sort a list { int done = false; int top = list.size() ; while (!done) { done = bubble_once (list, top); top--; } return; } rWe call bubble_once until it stops bubbling. rEventually this must happen even if we have to wait until top = 0 rThe algorithm is two nested loops l So performance is O (N * N) rBut if the data is nearly sorted then it can be much better.

3
Lecture 1 -- 3Computer Science I - Martin Hardwick Quick Sort 23 18 78 45 34 15 20 rQuick sort is quick. O ( N * Log (n)) rBecause it is a divide and conquer algorithm rEach time the list is split in half using a pivot l Items less than the pivot go on the left. l Items greater than the pivot go on the right. rUsually the first item is used as the pivot 23 18 15 2078 45 34 1520 DONE 18 78 45 34 Unlucky pivot 45 34 DONE 15 18 20 23 34 45 78 Final Result:

4
Lecture 1 -- 4Computer Science I - Martin Hardwick Quick Sort Algorithm rThe core of Quick Sort is the algorithm to divide the data into. l List of items before the pivot l List of items after the pivot rThe algorithm needs to be O (n) and we need to remember that in the unlucky case all of the data may have to go before or after the pivot. l If we are unlucky too much then the algorithm become O(n*n) l The worst case is if the data is sorted into reverse order l In this case the pivot is always the worst possible item –78 45 34 20 18 15 –45 34 20 18 1578(empty) –34 20 18 1545(empty) –20 18 1534(empty) –18 1520(empty) –1518(empty)

5
Lecture 1 -- 5Computer Science I - Martin Hardwick Pivot Algorithm // Perform one pivot pivot (vector &list, int bot, int top) { int pivot = list[bot]; int hi = top; int lo = bot; while (hi > low) { if (list[hi] pivot) { int tmp = list[hi]; list[hi] = list[lo]; list[lo] = tmp; } else if (list[hi] >= pivot) hi--; else lo++; } if (list[lo] < pivot) {// end case list[bot] = list[lo]; list[lo] = pivot; } return; } rThis algorithm partitions the data l The lower partition contains the data less than the pivot l The higher partition contains the data higher than the pivot. rConsider l 23 18 78 45 34 15 20 l Pivot =23 list[lo] =78 list[hi] =20 l 23 18 20 45 34 15 78 l Pivot =23 list[lo] =45 list[hi]=15 l 23 18 20 15 34 45 78 l Lo = hi && a[lo] =15 // end case l 15 18 20 23 34 45 78 rTo complete the quick sort you need to extend the pivot function to make two recursive calls l One with the list below the pivot l One with the list above the pivot

Similar presentations

OK

Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.

Algorithm Analysis 2P03 © Dave Bockus Acknowledgments to Mark Allen Weiss 2014 Data Structures & Algorithm Analysis in Java.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on summary writing worksheets Ppt on marketing plan Ppt on barack obama leadership failure Ppt on solid figures Ppt on pin diode switch Ppt on forest society and colonialism class 9 Financial management books free download ppt on pollution Ppt on cadbury dairy milk Ppt on earthquake in nepal Ppt on various properties of air