Download presentation
1
Chapter 8 ARRAYS Continued
1 1
2
SORTING THE CONTENTS OF AN ARRAY
Quite often it is necessary to arrange or sort items in a particular order. 2 2
3
SORTING THE CONTENTS OF AN ARRAY
Many sorting algorithms exist that can be used to sort a collection of data items. In this section, we will study the selection sort algorithm and its implementation in a method that sorts the contents of an array of integers. 3 3
4
SORTING THE CONTENTS OF AN ARRAY
A collection of data items may be sorted in ascending or descending order. The values in an array are sorted in ascending order if the value of the element at subscript i is greater than or equal to the value stored at subscript i - 1, for all subscripts i. The values in an array are sorted in descending order if the value of the element at subscript i is less than or equal to the value stored at subscript i - 1, for all subscripts i. 4 4
5
SORTING THE CONTENTS OF AN ARRAY Selection Sort
To sort an array in ascending order using selection sort, the array is scanned looking for the smallest value. This value is exchanged with the value at subscript 0. Then the array is scanned looking for the second smallest value, this value is exchanged with the value subscript 1. This process continues until all the elements in the array are in the proper order. 5 5
6
SORTING THE CONTENTS OF AN ARRAY Selection Sort
6 6
7
SORTING THE CONTENTS OF AN ARRAY Selection Sort
The text provides the following pseudocode for an ascending order selection sort on an array: For startScan is each subscript in the array from 0 through the next-to-last subscript Set minIndex variable to startScan. Set minValue variable to array[startScan]. For index is each subscript in the array from (startScan + 1) through the last subscript If array[index] is less than minValue Set minValue to array[index]. Set minIndex to index. End If. Increment index. End For. Set array[minIndex] to array[startScan]. Set array[startScan] to minValue. 7 7
8
SORTING THE CONTENTS OF AN ARRAY Selection Sort
The text provides the following source code for a method called selectionSort that can be used to sort an integer array in ascending order: public static void selectionSort(int[ ] array) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (array.length - 1); startScan++) minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) if (array[index] < minValue) minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; array[startScan] = minValue; 8 8
9
SORTING THE CONTENTS OF AN ARRAY Selection Sort
What would need to be changed in this method if we wanted to sort an array of doubles instead of an array of integers? Make these change(s) and test the result using the computer. public static void selectionSort(int[ ] array) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (array.length - 1); startScan++) minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) if (array[index] < minValue) minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; array[startScan] = minValue; 9 9
10
SORTING THE CONTENTS OF AN ARRAY Selection Sort
What would need to be changed in this method if we wanted to sort an array of strings instead of an array of integers? Make these change(s) and test the result using the computer. public static void selectionSort(int[ ] array) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (array.length - 1); startScan++) minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) if (array[index] < minValue) minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; array[startScan] = minValue; 10 10
11
SORTING THE CONTENTS OF AN ARRAY Selection Sort
What would need to be changed in this method if we wanted to sort the array in descending order instead of ascending order? Make these change(s) and test the result using the computer. public static void selectionSort(int[ ] array) { int startScan, index, minIndex, minValue; for(startScan = 0; startScan < (array.length - 1); startScan++) minIndex = startScan; minValue = array[startScan]; for(index = startScan + 1; index < array.length; index++) if (array[index] < minValue) minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; array[startScan] = minValue; 11 11
12
SORTING THE CONTENTS OF AN ARRAY Testing Strategy for a Sorting Algorithm
When testing a sorting algorithm try to sort a list that is in the reverse order of a sorted list, sort a list that is already sorted, sort a list that is somewhat out of order, and sort a list with duplicate elements. 12 12
13
SORTING THE CONTENTS OF AN ARRAY Selection Sort
*** See the program DemoSelectionSort.java on webct 13 13
14
SORTING THE CONTENTS OF AN ARRAY Selection Sort
What if you have a system of parallel arrays and you want to sort based on the contents of one of the arrays? 14 14
15
SEARCHING ARRAYS In many applications it is necessary to be able to locate a particular data item in a collection of data items. For example: We might need to locate an item in an inventory by part number; or to locate a particular employee by social security # or name; or to locate a particular book by ISBN number, title, or author. Many algorithms have been developed for searching for a particular data item in a collection. In this chapter we will discuss two search algorithms useful for locating a particular item in an array. 15 15
16
SEARCHING ARRAYS The Sequential Search Algorithm
The simplest search algorithm is the sequential search. A sequential search locates an item by comparing each item in the collection with the value being searched for. The search ends when the value is found or when the collection is exhausted. 16 16
17
SEARCHING ARRAYS The Sequential Search Algorithm
The textbook provides the source code on the next slide for a method called sequentialSearch that performs a sequential search on an array of integers. 17 17
18
SEARCHING ARRAYS The Sequential Search Algorithm
/**The sequentialSearch method searches an array for a value. @param array The array to search. @param value The value to search for. @return The subscript of the value if found in the array, otherwise -1. */ public static int sequentialSearch(int[ ] array, int value) { int index, element; boolean found; // Flag indicating search results index = 0; element = -1; found = false; while (!found && index < array.length) if (array[index] == value) found = true; element = index; } index++; return element; 18 18
19
SEARCHING ARRAYS Testing Strategy for a Search Algorithm
When testing a search algorithm try to search for the first value in the list, the last value in the list, some value in the middle, and some value that is not in the list. 19 19
20
SEARCHING ARRAYS The Sequential Search Algorithm
*** See the program DemoSequentialSearch.java on webct 20 20
21
SEARCHING ARRAYS The Sequential Search Algorithm
How would you modify the sequentialSearch method if you wanted to search an array of strings? Try this on a computer. public static int sequentialSearch(int[ ] array, int value) { int index, element; boolean found; // Flag indicating search results index = 0; element = -1; found = false; while (!found && index < array.length) if (array[index] == value) found = true; element = index; } index++; return element; 21 21
22
SEARCHING ARRAYS Performance of the Sequential Search Algorithm
In the worst case, the data item is not in the array or the data item is the last element in the array. Using the sequential search we check all the elements to determine this. If the array has 250 elements, 250 elements must be checked. We say that the sequential search has a worst case performance of order n where n is the number of array elements. On average, if there is equal probability of searching for any element in the array, the performance of the sequential search is order n/2, because over all the searches on average we have to search half the list. As the number of unsuccessful searches increases, the average number of comparisons also increases. Although the sequential search is easy to understand and code, it should not be used on large arrays. 22 22
23
SEARCHING ARRAYS The Binary Search Algorithm
The binary search algorithm is a more efficient search algorithm. The binary search works on a sorted collection of data items. In the binary search, the item to be located is compared with the middle element of the sorted collection, if they match the search ends. Otherwise, we determine whether the item belongs before or after the middle element of the array and continue the search in the proper half of the array using the binary search algorithm. 23 23
24
SEARCHING ARRAYS The Binary Search Algorithm
Suppose we are searching for 101 in the array shown below: [0] [1] [2] [3] [4] [5] [6] 13 17 19 27 84 97 101 array[first] array[middle] array[last] address array found is true 24 24
25
SEARCHING ARRAYS The Binary Search Algorithm
Suppose we are searching for 15 in the array shown below: 25 25
26
SEARCHING ARRAYS The Binary Search Algorithm
The textbook provides the following pseudocode for an algorithm that performs a binary search on an array that is in ascending order: Set first to 0. Set last to the last subscript in the array. Set position to -1. Set found to false. While found is not true and first is less than or equal to last Set middle to the subscript half-way between array[first] and array[last]. If array[middle] equals the desired value Set found to true. Set position to middle. Else If array[middle] is greater than the desired value Set last to middle - 1. Else Set first to middle + 1. End If. End While. Return position. 26 26
27
SEARCHING ARRAYS The Binary Search Algorithm
The textbook provides the method called binarySearch, shown on the next slide, that performs a binary search on an array of integers that is in ascending order: 27 27
28
SEARCHING ARRAYS The Binary Search Algorithm
public static int binarySearch(int[ ] array, int value) { int first, last, middle; int position; // Position of search value boolean found; // Flag first = 0; last = array.length - 1; position = -1; found = false; while (!found && first <= last) middle = (first + last) / 2; // Note: integer division if (array[middle] == value) // If value is found at midpoint... found = true; position = middle; } else if (array[middle] > value) // else if value is in lower half... last = middle - 1; else // else if value is in upper half.... first = middle + 1; return position; // Return the position of the item, or –1 if it was not found. 28 28
29
SEARCHING ARRAYS The Binary Search Algorithm
How would you modify the method binarySearch if you wanted to search an array of floats that was sorted in descending order? Try this on a computer. 29 29
30
SEARCHING ARRAYS The Binary Search Algorithm
public static int binarySearch(int[ ] array, int value) { int first, last, middle; int position; // Position of search value boolean found; // Flag first = 0; last = array.length - 1; position = -1; found = false; while (!found && first <= last) middle = (first + last) / 2; // Note: integer division if (array[middle] == value) // If value is found at midpoint... found = true; position = middle; } else if (array[middle] > value) // else if value is in lower half... last = middle - 1; else // else if value is in upper half.... first = middle + 1; return position; // Return the position of the item, or –1 if it was not found. 30 30
31
SEARCHING ARRAYS The Binary Search Algorithm
*** See the program DemoBinarySearch.java on webct 31 31
32
SEARCHING ARRAYS The Performance of the Binary Search Algorithm
Every time the binary search makes a comparison with the middle element and fails to find the desired item, half the array is eliminated. In the worst case, the number of comparisons that are required is k, where 2k is greater than or equal to the number of elements in the array, n. If there are n elements in the array, binary search has a worst case performance of log2n. 32 32
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.