13 Sorting and Recursion

13 Sorting and Recursion - Click to edit Master subtitle...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Engineering 101 Sorting and Recursion 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? 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; } 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 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 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 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 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?...
View Full Document

This note was uploaded on 04/07/2010 for the course ENGR engr 101 taught by Professor Rinenberg during the Spring '10 term at University of Michigan.

Page1 / 97

13 Sorting and Recursion - Click to edit Master subtitle...

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

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