{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

13 Sorting and Recursion

# 13 Sorting and Recursion - Engineering101...

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

Click to edit Master subtitle style Engineering 101 Sorting and Recursion

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

View Full Document
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.
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?

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

View Full Document
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…
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; }

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

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

{[ snackBarMessage ]}