Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle."— Presentation transcript:

1 Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle

2 2 Sorting an Array The data stored in an array can be manipulated most efficiently if it is sorted. The data stored in an array can be manipulated most efficiently if it is sorted. Data can be sorted using one of two orderings: Data can be sorted using one of two orderings: Ascending Ascending Descending Descending There are many different algorithms for sorting arrays, some more efficient than others. There are many different algorithms for sorting arrays, some more efficient than others. The first algorithm we will look at is bubble sort, one of the simplest, but also one of the least efficient of the lot. The first algorithm we will look at is bubble sort, one of the simplest, but also one of the least efficient of the lot.

3 3 Bubble Sort Sorting algorithm in which smaller values “bubble” their way to the top of the array (i.e., toward the first element), while larger values sink to the bottom of the array (i.e., toward the last element) Sorting algorithm in which smaller values “bubble” their way to the top of the array (i.e., toward the first element), while larger values sink to the bottom of the array (i.e., toward the last element) Basically, bubble sort: Basically, bubble sort: Passes through the array from beginning to end … Passes through the array from beginning to end … Compares adjacent pairs of elements... Compares adjacent pairs of elements... Interchanges any two elements that are out of order … Interchanges any two elements that are out of order … Repeats this process until the array is sorted. Repeats this process until the array is sorted.

4 4 Bubble Sort The number of passes required to sort an array using bubble sort is generally n – 1, where n is the size of the array. The number of passes required to sort an array using bubble sort is generally n – 1, where n is the size of the array. At the end of the first pass, the largest element will be in the last index of the array (assuming an ascending sort). At the end of the first pass, the largest element will be in the last index of the array (assuming an ascending sort). At the end of the second pass, the second largest element will be in the next-to-last index of the array (assuming an ascending sort). At the end of the second pass, the second largest element will be in the next-to-last index of the array (assuming an ascending sort). The process continues in like manner until the array is completely sorted. The process continues in like manner until the array is completely sorted.

5 5 Algorithm Animation To understand how the bubble sort algorithm works, let’s look at an animation of it. To understand how the bubble sort algorithm works, let’s look at an animation of it. The following Java applet animates bubble sort, as well as other sorting algorithms: The following Java applet animates bubble sort, as well as other sorting algorithms: Sort Animation Sort Animation Sort Animation Sort Animation

6 6 Sample Program Now let’s look at the code for bubble sort. Now let’s look at the code for bubble sort. The following Java program implements the bubble sort algorithm: The following Java program implements the bubble sort algorithm: Bubblesort.java Bubblesort.java Bubblesort.java

7 7 Lab 11 Finally, let’s take another look at this past Friday’s lab, since many of you still seem to have questions about it. Finally, let’s take another look at this past Friday’s lab, since many of you still seem to have questions about it.

8 8 Selection Sort The second algorithm we will look at for sorting arrays is selection sort. The second algorithm we will look at for sorting arrays is selection sort. Selection sort, like bubble sort, is simple, yet inefficient. (Unfortunately, the two sorting algorithms are equally inefficient.  ) Selection sort, like bubble sort, is simple, yet inefficient. (Unfortunately, the two sorting algorithms are equally inefficient.  )

9 9 Selection Sort Sorting algorithm which proceeds by repeatedly dividing an array into two subarrays, one that is sorted and one that is not, until the entire array is sorted Sorting algorithm which proceeds by repeatedly dividing an array into two subarrays, one that is sorted and one that is not, until the entire array is sorted Basically, selection sort works as follows: Basically, selection sort works as follows: Find the smallest value in the array … Find the smallest value in the array … Swap it with the value in the first position... Swap it with the value in the first position... Repeat the above steps for the remaining unsorted subarray (starting at the second position) … Repeat the above steps for the remaining unsorted subarray (starting at the second position) … Continue this process until the entire array is sorted. Continue this process until the entire array is sorted.

10 10 Selection Sort The number of passes required to sort an array using selection sort is generally n – 1, where n is the size of the array. The number of passes required to sort an array using selection sort is generally n – 1, where n is the size of the array. At the end of the first pass, the smallest element will be in the first (0 th ) index of the array (assuming an ascending sort). At the end of the first pass, the smallest element will be in the first (0 th ) index of the array (assuming an ascending sort). At the end of the second pass, the second smallest element will be in the second (1 th ) index of the array (assuming an ascending sort). At the end of the second pass, the second smallest element will be in the second (1 th ) index of the array (assuming an ascending sort). The process continues in this fashion until the array is completely sorted. The process continues in this fashion until the array is completely sorted.

11 11 Algorithm Animation To understand how the selection sort algorithm works, let’s look at an animation of it. To understand how the selection sort algorithm works, let’s look at an animation of it. The following Java applet animates selection sort, as well as other sorting algorithms: The following Java applet animates selection sort, as well as other sorting algorithms: Sort Animation Sort Animation Sort Animation Sort Animation

12 12 Sample Program Now let’s look at the code for selection sort. Now let’s look at the code for selection sort. The following Java program implements the selection sort algorithm: The following Java program implements the selection sort algorithm: SelectionSort.java SelectionSort.java SelectionSort.java

13 13 Searching an Array Two algorithms commonly used to search an array are: Two algorithms commonly used to search an array are: Linear (or sequential) search Linear (or sequential) search Searches for a target value in an array by examining each element in sequence Searches for a target value in an array by examining each element in sequence Does not require that the array be sorted Does not require that the array be sorted Simple, inefficient searching algorithm Simple, inefficient searching algorithm Binary search Binary search Searches for a target value in an array using a halving algorithm Searches for a target value in an array using a halving algorithm Requires that the array be sorted Requires that the array be sorted Complex, efficient searching algorithm Complex, efficient searching algorithm

14 14 Sample Programs The following Java programs demonstrate linear search of an array: The following Java programs demonstrate linear search of an array: gradeSearch.java (unsorted array) gradeSearch.java (unsorted array) gradeSearch.java gradeSearchWithSort.java (sorted array) gradeSearchWithSort.java (sorted array) gradeSearchWithSort.java

15 15 Binary Search Binary search, unlike linear search, requires that the array be sorted. Binary search, unlike linear search, requires that the array be sorted. The algorithm proceeds as follows: The algorithm proceeds as follows: Compare the target value to the value in the middle (or approximate middle) of the array; if the two values match, we’re done; otherwise … Compare the target value to the value in the middle (or approximate middle) of the array; if the two values match, we’re done; otherwise … Search the bottom half of the array if the target value is less than the middle value; otherwise … Search the bottom half of the array if the target value is less than the middle value; otherwise … Search the top half of the array if the target value is greater than the middle value. Search the top half of the array if the target value is greater than the middle value.

16 16 Binary Search Algorithm in pseudocode (1 st version): * Algorithm in pseudocode (1 st version): * mid = approximate midpoint between 0 and (a.length – 1); if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[a.length – 1]. mid = approximate midpoint between 0 and (a.length – 1); if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[a.length – 1]. Smaller versions of the problem we’re trying to solve; correspond to recursive calls to the algorithm itself Smaller versions of the problem we’re trying to solve; correspond to recursive calls to the algorithm itself * Lecture material adapted from Java: An Introduction to Problem Solving & Programming by Walter Savitch

17 17 Binary Search Algorithm in pseudocode (2 nd version): * Algorithm in pseudocode (2 nd version): * mid = approximate midpoint between first and last; if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[last]. mid = approximate midpoint between first and last; if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[last]. Using first and last to specify the indices of the range of values to be searched allows the algorithm to be called recursively Using first and last to specify the indices of the range of values to be searched allows the algorithm to be called recursively * Lecture material adapted from Java: An Introduction to Problem Solving & Programming by Walter Savitch

18 18 Binary Search Algorithm in pseudocode (3 rd version): * Algorithm in pseudocode (3 rd version): * mid = approximate midpoint between first and last; if (first > last) return -1; else if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[last]. mid = approximate midpoint between first and last; if (first > last) return -1; else if (target == a[mid]) return mid; else if (target a[mid]) return the result of searching a[mid + 1] through a[last]. This version of the algorithm handles the case where target (the number being searched for) is not found in the array. This version of the algorithm handles the case where target (the number being searched for) is not found in the array. When this occurs, the value of first becomes larger than the value of last (condition one). When this occurs, the value of first becomes larger than the value of last (condition one). * Lecture material adapted from Java: An Introduction to Problem Solving & Programming by Walter Savitch

19 19 Sample Programs The following Java program demonstrates binary search of an array: The following Java program demonstrates binary search of an array: BinaryGradeSearch.java (sorted array) BinaryGradeSearch.java (sorted array) BinaryGradeSearch.java

20 20 Merge Sort Merge sort is a divide-and-conquer algorithm which works as follows: Merge sort is a divide-and-conquer algorithm which works as follows: The array to be sorted is divided into two halves. The array to be sorted is divided into two halves. Each half is sorted by recursive calls. Each half is sorted by recursive calls. The two sorted arrays are then merged into a single sorted array. The two sorted arrays are then merged into a single sorted array. An outline of the algorithm appears on the next slide. An outline of the algorithm appears on the next slide.

21 21 Merge Sort Algorithm If an array a consists of a single element, do nothing (base case). If an array a consists of a single element, do nothing (base case). Otherwise, do the following (recursive case): Otherwise, do the following (recursive case): Copy the elements in the first half of array a to an array named front. Copy the elements in the first half of array a to an array named front. Copy the elements in the last half of array a to an array named tail. Copy the elements in the last half of array a to an array named tail. Sort front with a recursive call. Sort front with a recursive call. Sort tail with a recursive call. Sort tail with a recursive call. Merge the elements in arrays front and tail into array a. Merge the elements in arrays front and tail into array a.

22 22 Algorithm Animation To understand how the merge sort algorithm works, let’s look at an animation of it. To understand how the merge sort algorithm works, let’s look at an animation of it. The following Java applet animates merge sort: The following Java applet animates merge sort: Merge Sort Merge Sort Merge Sort Merge Sort The following Java applets animate merge sort, as well as other sorting algorithms: The following Java applets animate merge sort, as well as other sorting algorithms: Sorting Applets Sorting Applets Sorting Applets Sorting Applets

23 23 Sample Program Now let’s look at the code for merge sort. Now let’s look at the code for merge sort. The following Java program implements the merge sort algorithm: The following Java program implements the merge sort algorithm: MergeSort.java MergeSort.java MergeSort.java


Download ppt "Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle."

Similar presentations


Ads by Google