11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

228 Chap. 7 Internal Sorting4220171328142315i=0 1 2 3 4 5 61342201714281513144220171528231314154220172328131415174220232813141517204223281314151720234223 281314151720232842Figure 7.2 An illustr<strong>at</strong>ion of Bubble Sort. Each column shows the array afterthe iter<strong>at</strong>ion with the indic<strong>at</strong>ed value of i in the outer for loop. Values above theline in each column have been sorted. Arrows indic<strong>at</strong>e the swaps th<strong>at</strong> take placeduring a given iter<strong>at</strong>ion.neighbor, then the two values are swapped. Once the smallest value is encountered,this process will cause it to “bubble” up to the top of the array. The second passthrough the array repe<strong>at</strong>s this process. However, because we know th<strong>at</strong> the smallestvalue reached the top of the array on the first pass, there is no need to comparethe top two elements on the second pass. Likewise, each succeeding pass throughthe array compares adjacent elements, looking <strong>at</strong> one less value than the precedingpass. Figure 7.2 illustr<strong>at</strong>es Bubble Sort. A Java implement<strong>at</strong>ion is as follows:st<strong>at</strong>ic

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!