Lecture-Unknow - BubbleSort...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science I - Martin Hardwick Lecture 1 -- 1 Bubble Sort 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Computer Science I - Martin Hardwick Lecture 1 -- 2 Bubble Sort Completed // Bubble sort a list { int     done = false;      int     top = list.size() ;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Lecture-Unknow - BubbleSort...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online