{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

13 Sorting and Recursion - Engineering101...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style Engineering 101 Sorting and Recursion
Background image of page 1

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

View Full Document Right Arrow Icon
Quote of the Day - Helen  Keller When one door to happiness closes, another  opens; but often we look so long at the closed door that we do not see the one which has been  opened for us.
Background image of page 2
Sorting n Often we have a list of numbers and we would  like to order them. n What is a good way to do this using a  computer?
Background image of page 3

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

View Full Document Right Arrow Icon
Bubble Sort n Go through the list and make sure to move the  highest to the end. n Then go through the list again and make sure  the second highest is second to the end. n Rinse and Repeat…
Background image of page 4
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; }
Background image of page 5

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

View Full Document Right Arrow Icon
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 3 list[1 ] 4 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 2 i 5 j 0
Background image of page 6
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 3 list[1 ] 4 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 2 i 5 j 0
Background image of page 7

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

View Full Document Right Arrow Icon
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 2 list[1 ] 4 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 3 i 5 j 0
Background image of page 8
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 2 list[1 ] 4 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 3 i 5 j 1
Background image of page 9

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

View Full Document Right Arrow Icon
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 2 list[1 ] 3 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 4 i 5 j 1
Background image of page 10
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted? int j; // Which are we comparing? for(i=list.size( )-1; i > 0; i=i-1) for(j=0; j< i; j=j+1) if (list.at(j) > list.at(i)) swap(list.at(i), list.at(j)); return; list[0 ] 2 list[1 ] 3 list[2 ] 1 list[3 ] 6 list[4 ] 5 list[5 ] 4 i 5 j 2
Background image of page 11

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

View Full Document Right Arrow Icon
Bubble Sort void bubblesort( vector<int> & list){ int i; // How many have we sorted?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}