Download presentation

Presentation is loading. Please wait.

Published bySamantha Quinn Modified over 3 years ago

1
Lecture Computer 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 l After bubble_once l l After another l l After another l rIf nothing moves we are done so we return true.

2
Lecture Computer 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 Computer Science I - Martin Hardwick Quick Sort 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 DONE Unlucky pivot DONE Final Result:

4
Lecture Computer 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 – – (empty) – (empty) – (empty) – (empty) –1518(empty)

5
Lecture Computer 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 l Pivot =23 list[lo] =78 list[hi] =20 l l Pivot =23 list[lo] =45 list[hi]=15 l l Lo = hi && a[lo] =15 // end case l 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

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google