1 / 11

Chapter 5 Arrays

Chapter 5 Arrays. Introducing Arrays Declaring Array Variables, Creating Arrays, and Initializing Arrays Passing Arrays to Methods Copying Arrays Multidimensional Arrays Search and Sorting Methods. Searching Arrays.

morrise
Download Presentation

Chapter 5 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. Chapter 5 Arrays • Introducing Arrays • Declaring Array Variables, Creating Arrays, and Initializing Arrays • Passing Arrays to Methods • Copying Arrays • Multidimensional Arrays • Search and Sorting Methods

  2. Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching, like sorting, is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search.

  3. Linear Search The linear search approach compares the key element, key, with each element in the array list[]. The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1.

  4. Example 5.10Testing Linear Search • Objective: Implement and test the linear search method by creating an array of 10 elements of int type randomly and then display this array. Prompt the user to enter a key for testing the linear search. LinearSearch Run

  5. Binary Search For binary search to work, the elements in the array must already be ordered. Without loss of generality, assume that the array is in ascending order. e.g. 2 4 7 10 11 45 50 59 60 66 69 70 79 The binary search first compares the key with the element in the middle of the array. Consider the following three cases:

  6. Binary Search, cont. ·If the key is less than the middle element, you only need to search the key in the first half of the array. ·If the key is equal to the middle element, the search ends with a match. ·If the key is greater than the middle element, you only need to search the key in the second half of the array.

  7. Binary Search, cont.

  8. Example 5.11Testing Binary Search • Objective: Implement and test the binary search method. The program first creates an array of 10 elements of int type. It displays this array and then prompts the user to enter a key for testing binary search. BinarySearch Run

  9. Example 5.12Using Arrays in Sorting • Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Sort it to produce 1, 2, 4, 5, 6, 8, 9 2, 9, 5, 4, 8, 1, 6 SelectionSort Run

  10. Example 5.12: (Cont) Using Arrays in Sorting int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Find the largest element in myList and swap it with the last element in myList. 2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7) 2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8(size = 6) 2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5) 2, 1, 5, 4 => 2, 1, 4, 5 2, 1, 4 => 2, 1, 4, 2, 1 => 1, 2 Sort it to produce 1, 2, 4, 5, 6, 8, 9

  11. Exercise 5.5: Bubble Sort int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Pass 1: 2, 5, 4, 8, 1, 6, 9 Pass 2: 2, 4, 5, 1, 6, 8, 9 Pass 3: 2, 4, 1, 5, 6, 8, 9 Pass 4: 2, 1, 4, 5, 6, 8, 9 Pass 5: 1, 2, 4, 5, 6, 8, 9 Pass 6: 1, 2, 4, 5, 6, 8, 9

More Related