5
Lecture 1 -- 1 Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector <int> &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; } This algorithm moves the largest item in the list to the top. Every time we run it the next biggest item moves to the top Consider 23 18 78 45 34 15 20 After bubble_once 18 23 45 34 15 20 78 After another 18 23 34 15 20 45 78 After another 18 23 15 20 34 45 78 If nothing moves we are done so we return true.

Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool

Embed Size (px)

Citation preview

Page 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

Lecture 1 -- 1Computer Science I - Martin Hardwick

Bubble Sort

bool bubble_once( vector <int> &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;

}

This algorithm moves the largest item in the list to the top.

Every time we run it the next biggest item moves to the top

Consider 23 18 78 45 34 15 20 After bubble_once 18 23 45 34 15 20 78 After another 18 23 34 15 20 45 78 After another 18 23 15 20 34 45 78

If nothing moves we are done so we return true.

Page 2: Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool

Lecture 1 -- 2Computer Science I - Martin Hardwick

Bubble Sort Completed

void bubble( vector <int> &list)

// Bubble sort a list

{int done = false;

int top = list.size() ;

while (!done) {done = bubble_once (list,

top);top--;

}

return;}

We call bubble_once until it stops bubbling.

Eventually this must happen even if we have to wait until top = 0

The algorithm is two nested loops So performance is O (N *

N)

But if the data is nearly sorted then it can be much better.

Page 3: Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool

Lecture 1 -- 3Computer Science I - Martin Hardwick

Quick Sort

23 18 78 45 34 15 20 Quick sort is quick.

O ( N * Log (n))

Because it is a divide and conquer algorithm

Each time the list is split in half using a “pivot”

Items less than the pivot go on the left.

Items greater than the pivot go on the right.

Usually the first item is used as the pivot

2318 15 20 78 45 34

15 20

DONE

18

7845 34

Unluckypivot

4534

DONE

15 18 20 23 34 45 78Final Result:

Page 4: Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool

Lecture 1 -- 4Computer Science I - Martin Hardwick

Quick Sort Algorithm

The core of Quick Sort is the algorithm to divide the data into. List of items before the pivot List of items after the pivot

The 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.

If we are unlucky too much then the algorithm become O(n*n)

The worst case is if the data is sorted into reverse order In this case the pivot is always the worst possible item

– 78 45 34 20 18 15– 45 34 20 18 15 78 (empty)– 34 20 18 15 45 (empty)– 20 18 15 34 (empty)– 18 15 20 (empty)– 15 18 (empty)

Page 5: Lecture 1 -- 1Computer Science I - Martin Hardwick Bubble Sort bool bubble_once( vector &list, int top) // Perform one bubble iteration on a list { bool

Lecture 1 -- 5Computer Science I - Martin Hardwick

Pivot Algorithm

// Perform one pivotpivot (vector <integer> &list , int bot, int

top){

int pivot = list[bot];int hi = top;int lo = bot;

while (hi > low) { if (list[hi] < pivot && list[lo] > pivot) {

int tmp = list[hi];list[hi] = list[lo];list[lo] = tmp;}

else if (list[hi] >= pivot)hi--;

elselo++;

}if (list[lo] < pivot) { // end case list[bot] = list[lo]; list[lo] = pivot;

}return;}

This algorithm partitions the data The lower partition contains the

data less than the pivot The higher partition contains the

data higher than the pivot. Consider

23 18 78 45 34 15 20 Pivot =23 list[lo] =78 list[hi] =20 23 18 20 45 34 15 78 Pivot =23 list[lo] =45 list[hi]=15 23 18 20 15 34 45 78 Lo = hi && a[lo] =15 // end case 15 18 20 23 34 45 78

To complete the quick sort you need to extend the pivot function to make two recursive calls

One with the list below the pivot One with the list above the pivot