11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Review (Week1) C++_ the unit of programming is the class from which objects are eventually instantiated. C++ classes contain functions that implement class.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
8/9: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 2. Program Construction in Java. 2.8 Searching.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
Layout Managers Arranges and lays out the GUI components on a container.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
4/17: Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2002 Prentice Hall. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig04_03.cpp (1 of 2) 1 // Fig. 4.3: fig04_03.cpp 2 // Initializing an array. 3 #include 4 5.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
8/2: Recursion About Scoping.java Recursion Program of the Day.
8/8: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays: the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array and Initializing.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Searching Arrays Linear search Binary search small arrays
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays Allocating an Array.
Introduction to Programming
Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using Arrays 7.5 References and Reference.
Presentation transcript:

11/30: 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 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 );

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";

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 ); }

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; }

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

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

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.source code

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.

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 ; }

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!

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.

1 st Program of the Day: BinarySearch pg. 291 BinarySearch.java Once you get it to work, figure out how it works.

Multiple-Subscripted Arrays About BinarySearch.java Multiple-Subscripted Arrays Program of the Day

BinarySearch.java pt.1 //fig. 7.13: BinarySearch.java: binary search of an array import java.awt.*; import java.awt.event.*; import java.text.*; import javax.swing.*; public class BinarySearch extends JApplet implements ActionListener { JLabel enterLabel, resultLabel; JTextField enter, result; JTextArea output; int a[]; String display = "";

BinarySearch.java pt.2 public void init () { Container c = getContentPane(); c.setLayout ( new FlowLayout() ); enterLabel = new JLabel ( "Enter key" ); c.add( enterLabel ); enter = new JTextField( 5 ); enter.addActionListener( this ); c.add( enter ); resultLabel = new JLabel ( "Result" ); c.add( resultLabel );

BinarySearch.java pt.3 result = new JTextField( 22 ); result.setEditable( false ); c.add( result ); output = new JTextArea( 6, 60 ); output.setFont(new Font("Courier",Font.PLAIN,12 ) ); c.add( output ); //create array and fill it with even numbers 0-28 a = new int[ 15 ]; for ( int i = 0; i < a.length; i++ ) a[ i ] = 2 * i; }

BinarySearch.java pt.4 public void actionPerformed ( ActionEvent e ) { String searchKey = e.getActionCommand(); //initialize display string for new search display = "Portions of array searched\n"; //perform the binary search int element = binarySearch ( a, Integer.parseInt ( searchKey ) ); output.setText( display ); if ( element != -1 ) result.setText( "Found it in slot " + element ); else result.setText( "Value not found" ); }

BinarySearch.java pt.5 //binary search method public int binarySearch ( int array[], int key ) { int low = 0; //low subscript int high = array.length - 1;//high subscript int middle; //middle subscript while ( low <= high ) { middle = ( low + high ) / 2; //The next line displays the part of the array still //available for searching through for the key. buildOutput ( low, middle, high );

BinarySearch.java pt.6 if ( key == array[middle] ) //a match return middle; else if ( key < array[middle] ) high = middle - 1; //search lower array else low = middle + 1; } return -1; //searchKey not found }

BinarySearch.java pt.7 //build 1 row of output w/ part of the array being processed. void buildOutput ( int low, int mid, int high ) { DecimalFormat twoDigits = new DecimalFormat ( "00" ); for ( int i = 0; i < a.length; i++ ) { if ( i high ) display += " "; else if ( i == mid ) //mark middle element in output display += twoDigits.format ( a [ i ] ) + "* "; else display += twoDigits.format ( a [ i ] ) + " "; } display += "\n"; }

Multiple-Subscripted Arrays Used to represent a table of values, not just a row. By convention, the 1st number = row number, and the 2nd number = column number. EX: int sample[] [] = new int [ 4 ] [ 2 ]; might have values like this: col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays Another way to remember: “first the row, then the position” int sample [] [] = { {3,1},{2,0},{6,8},{7,5} }; col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays EX: int sample[] [] = new int [ 4 ] [ 2 ]; sample [3][0] = 7 sample [1][1] = 0 sample [0][1] = ? sample [2][0] = ? col 0col 1 row 031 row 120 row 268 row 375

Multiple-Subscripted Arrays But how do you use them? Think of them as arrays of arrays (lists of lists). for ( int i = 0; i < id.length; i++ ) { for ( int j = 0; j < id[i].length ; j++ ){ g.drawString(“ “ + (id[ i ][ j ]),x,y); } see StringArray.java

2 nd Program of the Day pg. 297 DoubleArray.java