Chapter 1 - Introducing Data Structures and Algorithms - 4 PDF
Chapter 1 - Introducing Data Structures and Algorithms - 4 PDF
Faculty of Engineering
Chapter 1
Introducing Data Structures and Algorithms
Arrays
Ordered Arrays
The Bubble Sort
The Insertion Sort
Outline
3
Arrays
Ordered Arrays
The Bubble Sort
The Insertion Sort
Bubble Sort1
4
The Unordered (by Height) Baseball Team The Ordered (by Height) Baseball Team
Bubble Sort: How Does It Work?
5
Then, go back to the left end of the line, compare player 0 and 1
again, and so on to the right. At this time (second pass), will stop at
position (n-2), because the last position (n-1) is the tallest. Continue
this process until all the players are in order.
Bubble Sort: Code in C/C++
7
for( i=0; i<nElems‐1; i++ ){ //number of sorted elements or ith pass
for( j=0; j<nElems‐i‐1; j++ ){ //circle to take the tallest to the right end
if( arr[j] > arr[j+1] ){ //if the left is taller?
tmp = arr[ j ]; /*swap arr[ j ] and arr[ j+1 ] */
arr[j] = arr[ j+1 ];
arr[ j+1 ] = tmp;
} //End if
} //End j loop
} //End i loop
To be continued…