COSC 1P03 Data Structures and Abstraction 20.1
COSC 1P03 Data Structures and Abstraction 20.1
COSC 1P03 Data Structures and Abstraction 20.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>COSC</strong> <strong>1P03</strong><br />
<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong><br />
pass 1<br />
3 5 2 7 1 6 8 4<br />
pass 5<br />
3 5 2 7 1 4 6 8<br />
pass 2<br />
3 5 2 7 1 6 8 4<br />
pass 6<br />
3 5 2 1 4 6 7 8<br />
pass 3<br />
3 5 2 7 1 6 4 8<br />
pass 7<br />
3 5 1 2 4 6 7 8<br />
pass 4 3 5 2 7 1 4 6 8<br />
Figure <strong>20.1</strong>1 Insertion sort<br />
pass 8<br />
3 1 2 4 5 6 7 8<br />
<strong>COSC</strong> <strong>1P03</strong><br />
<br />
<br />
<br />
<br />
<br />
Exchange (Bubble) Sort<br />
Principle<br />
exchange pairs if out of order<br />
works?<br />
quit?<br />
Pehaviour<br />
largest “bubbles” to end<br />
next pass doesn’t have to go to end<br />
Analysis<br />
each pass moves 1 item to correct spot<br />
O(n) passes<br />
ave O(n) pairs<br />
O(n 2 )<br />
early termination<br />
no exchanges<br />
O(n) if sorted<br />
E.g. mark list in array<br />
In situ, stable<br />
<strong>Data</strong> <strong>Structures</strong> <strong>and</strong> <strong>Abstraction</strong> 20.20<br />
pass 1<br />
3 5 2 7 1 6 8 4<br />
3 5 2 7 1 6 8 4<br />
pass 3<br />
2 3 1 5 6 4 7 8<br />
2 3 1 5 6 4 7 8<br />
3 2 5 7 1 6 8 4<br />
2 1 3 5 6 4 7 8<br />
3 2 5 7 1 6 8 4<br />
2 1 3 5 6 4 7 8<br />
3 2 5 1 7 6 8 4<br />
2 1 3 5 6 4 7 8<br />
3 2 5 1 6 7 8 4<br />
3 2 5 1 6 7 8 4<br />
pass 4<br />
2 1 3 5 4 6 7 8<br />
1 2 3 5 4 6 7 8<br />
pass 2<br />
3 2 5 1 6 7 4 8<br />
1 2 3 5 4 6 7 8<br />
2 3 5 1 6 7 4 8<br />
1 2 3 5 4 6 7 8<br />
2 3 5 1 6 7 4 8<br />
2 3 1 5 6 7 4 8<br />
2 3 1 5 6 7 4 8<br />
2 3 1 5 6 7 4 8<br />
pass 5<br />
pass 6<br />
1 2 3 4 5 6 7 8<br />
1 2 3 4 5 6 7 8<br />
1 2 3 4 5 6 7 8<br />
1 2 3 4 5 6 7 8<br />
1 2 3 4 5 6 7 8<br />
Figure <strong>20.1</strong>4 Exchange sort<br />
pass 7<br />
1 2 3 4 5 6 7 8<br />
20.7