1 / 13

8/8: Sorting and Searching Arrays

8/8: Sorting and Searching Arrays. Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the binary search. PassArray.java -- pt. 1. //Fig 7.10: PassArray.java //Passing arrays and individual elements to methods

james-rush
Download Presentation

8/8: Sorting and Searching Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8/8: Sorting and Searching Arrays • Look at PassArray.java • Sorting arrays: the bubble sort method • Searching arrays: the linear search • Searching arrays: the binary search

  2. PassArray.java -- pt. 1 //Fig 7.10: PassArray.java //Passing arrays and individual elements to methods import java.awt.Container; import javax.swing.*; public class PassArray extends JApplet { JTextArea outputArea; String output; public void init() { outputArea = new JTextArea(); Container c = getContentPane(); c.add( outputArea );

  3. PassArray.java -- pt. 2 int a[] = { 1, 2, 3, 4, 5 }; output = "Effects of passing entire " + "array call-by-reference:\n" + "The values of the original array are:\n"; for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; modifyArray ( a ); //passing the whole array output+="\n\nValues of the modified array are:\n";

  4. PassArray.java -- pt. 3 for ( int i = 0; i < a.length ; i++ ) output += " " + a[ i ]; output += "\n\nEffects of passing array " + "element call-by-value:\n" + "a[3] before modifyElement: " + a[ 3 ]; modifyElement ( a [ 3 ] ); output += "\na[3] after modifyElement: " + a [ 3 ]; outputArea.setText ( output ); }

  5. PassArray.java -- pt. 4 public void modifyArray ( int b[] ) { for ( int j = 0 ; j < b.length; j++ ) b [ j ] *= 2; } public void modifyElement ( int e ) { e *= 2; } }

  6. Sorting Arrays using Bubble Sort • Reorganizing an array in some order (low to high, etc.) • Bubble Sort compares two values, switches them in the array positions if appropriate, and checks the next two values.

  7. Sorting Arrays using Bubble Sort • In this case, 3 & 6 are compared, and NOT switched. • Then, 6 & 1 are compared, then switched. • Then 6 & 9 are compared and NOT switched. • This is ONLY ONE PASS through the array.

  8. Sorting Arrays using Bubble Sort • Core of sorting: an if structure. • This is nested inside a for loop to look at each pair of values in the array. • This loop in nested inside another loop to make multiple passes through the array. • Look at SortThem & its source code.

  9. Searching Arrays: Linear Search • How do you find a particular element value in an array? • One way: a linear search. • The core structure: an if statement in a for loop. • The for loop gives movement through the array • The if structure looks for a matching value in the elements.

  10. Searching Arrays: Linear Search • The for loop gives movement through the array • The if structure looks for a matching value in the elements. for ( int n = 0 ; n < array.length ; n++ ) { if ( array [ n ] == key ) return n ; }

  11. Searching Arrays: Binary Sort • Go to the middle of the array. • See if the number you are looking for is higher or lower, and go to the middle of that side. • Repeat until found. • Note that the array must already be sorted!

  12. Searching Arrays: Linear vs. Binary • A linear search works well for smaller arrays, and is simpler to understand and troubleshoot. • A binary search is better for a large array. It is more efficient in its searching.

  13. Program of the Day: BinarySearch • pg. 291 BinarySearch.java • Once you get it to work, figure out how it works. • Next time: review searching and learn about multiple-subscripted arrays.

More Related