Download presentation
Presentation is loading. Please wait.
1
Sorting Mr. Jacobs
2
10.3 Sorting When the elements are in random order, we need to rearrange them before we can take advantage of any ordering. This process is called sorting. Suppose we have an array a of five integers that we wish to sort from smallest to largest. In Figure 10-2, the values currently in a are as depicted on the left; we wish to end up with values as on the right.
3
10.3 Sorting
4
10.3 Sorting Selection Sort The basic idea of a selection sort is:
For each index position i Find the smallest data value in the array from positions i through length - 1, where length is the number of data values stored. Exchange the smallest value with the value at position i.
5
10.3 Sorting Table 10-3 shows a trace of the elements of an array after each exchange of elements is made. The items just swapped are marked with asterisks, and the sorted portion is shaded. Notice that in the second and fourth passes, since the current smallest numbers are already in place, we need not exchange anything. After the last exchange, the number at the end of the array is automatically in its proper place.
6
10.3 Sorting
7
10.3 Sorting Before writing the algorithm for this sorting method, note the following: If the array is of length n, we need n - 1 steps. We must be able to find the smallest number. We need to exchange appropriate array items. When the code is written for this sort, note that strict inequality (<) rather than weak inequality (<=) is used when looking for the smallest remaining value.
8
10.3 Sorting The algorithm to sort by selection is:
For each i from 0 to n - 1 do Find the smallest value among a[i], a[i + 1],. . .a[n - 1] and store the index of the smallest value in minIndex Exchange the values of a[i] and a[index], if necessary
9
10.3 Sorting Here we need to find the smallest value of array a.
We will incorporate this segment of code in a method, findMinimum, for the selection sort. We will also use a method swap to exchange two elements in an array.
10
10.3 Sorting public static void selectionSort(int[] a){
Using these two methods, the implementation of a selection sort method is: public static void selectionSort(int[] a){ for (int i = 0; i < a.length - 1; i++){ int minIndex = findMinimum(a, i); if (minIndex != i) swap(a, i, minIndex); }
11
10.3 Sorting The method for finding the minimum value in an array takes two parameters: the array and the position to start the search. The method returns the index position of the minimum element in the array. Its implementation uses a for loop: public static int findMinimum(int[] a, int first){ int minIndex = first; for (int i = first + 1; i < a.length; i++) if (a[i] < a[minIndex]) minIndex = i; return minIndex; }
12
10.3 Sorting public static void swap(int[] a, int x, int y){
The swap method exchanges the values of two array cells: public static void swap(int[] a, int x, int y){ int temp = a[x]; a[x] = a[y]; a[y] = temp; }
13
10.3 Sorting Bubble Sort Given a list of items stored in an array, a bubble sort causes a pass through the array to compare adjacent pairs of items. Whenever two items are out of order with respect to each other, they are swapped. The effect of such a pass through an array of items is traced in Table 10-4. The items just swapped are marked with asterisks, and the sorted portion is shaded.
14
10.3 Sorting Bubble Sort Notice that after such a pass, we are assured that the array will have the item that comes last in order in the final array position. That is, the last item will "sink" to the bottom of the array, and preceding items will gradually "percolate" to the top.
15
10.3 Sorting
16
10.3 Sorting A complete Java method to implement a bubble sort for an array of integers is shown next:
17
10.3 Sorting public static void bubbleSort(int[] a){ int k = 0;
boolean exchangeMade = true; // Make up to n - 1 passes through array, exit early if no exchanges // are made on previous pass while ((k < a.length - 1) && exchangeMade){ exchangeMade = false; k++; for (int j = 0; j < a.length - k; j++) if (a[j] > a[j + 1]){ swap(a, j, j + 1); exchangeMade = true; }
18
10.3 Sorting Insertion Sort
The insertion sort attempts to take greater advantage of an array's partial ordering. The goal is that on the kth pass through, the kth item among: a[0], a[1], ..., a[k] should be inserted into its rightful place among the first k items in the array. This is analogous to the fashion in which many people pick up playing cards and order them in their hands.
19
10.3 Sorting For each k from 1 to n - 1 (k is the index of array element to insert) Set itemToInsert to a[k] Set j to k - 1 (j starts at k - 1 and is decremented until insertion position is found) While (insertion position not found) and (not beginning of array) If itemToInsert < a[j] Move a[j] to index position j + 1 Decrement j by 1 Else The insertion position has been found itemToInsert should be positioned at index j + 1
20
10.3 Sorting An insertion sort for each value of k is traced in Table 10-5. In each column of this table, the data items are sorted in order relative to each other above the item with the asterisk; below this item, the data are not affected.
21
10.3 Sorting Sorting Arrays of Objects
Any of the sort methods can be modified to sort arrays of objects. We assume that the objects implement the Comparable interface and support the method compareTo. Then, we simply replace the type of all array parameters with Object and make the appropriate use of compareTo where the comparison operators are used.
22
10.3 Sorting public static int findMinimum(Object[] a, int first){
int minIndex = first; for (int i = first + 1; i < a.length(); i++) if (((Comparable)a[i]).compareTo(a[minIndex]) < 0) minIndex = i; return minIndex; }
23
Quicksort 23 23 Quicksort: An algorithm that is O(n log n).
Break an array into two parts, then move the elements so that the larger values are in one end and the smaller values are in the other. Each part is subdivided in the same way, until the subparts contain only a single value. Then the array is sorted. 23 23
24
Quicksort (continued)
Phase 1: Step 1: if the array length is less than 2, it is done. Step 2: locates the pivot (middle value), 7. Step 3: Tags elements at left and right ends as i and j. 24 24
25
Quicksort (continued)
Step 4: While a[i] < pivot value, increment i. While a[j] > pivot value, decrement j. Step 5: if i > j, then end the phase. Else, interchange a[i] and a[j] 25 25
26
Quicksort (continued)
Step 6: increment i and decrement j. Steps 7-9: repeat steps 4-6. Step 10-11: repeat steps 4-5. Step 12: the phase is ended. Split the array into two subarrays a[0…j] and a[i…10]. 26 26
27
Quicksort (continued)
Phase 2 and Onward: Repeat the process to the left and right subarrays until their lengths are 1. Complexity Analysis: At each move, either an array element is compared to the pivot or an interchange takes place. The process stops when I and j pass each other. Thus, the work is proportional to the array’s length (n). 27 27
28
Quicksort (continued)
Complexity Analysis (continued): Phase 2, the work is proportional to the left plus right subarrays’ lengths, so it is proportional to n. To complete the analysis, you need to know how many times the array are subdivided. Best case: O(n log I) Worst case: O(n2). Implementation: An iterative approach requires a data structure called a stack. 28 28
29
Merge Sort Merge sort: a recursive, divide-and-conquer strategy to break the O(n2) barrier. Compute the middle position of an array, and recursively sort its left and right subarrays. Merge the subarrays back into a single sorted array. Stop the process when the subarrays cannot be subdivided. 29 29
30
Merge Sort (continued)
This top-level design strategy can be implemented by three Java methods: mergeSort: the public method called by clients. mergeSortHelper: a private helper method that hides the extra parameter required by recursive calls. merge: a private method that implements the merging process. 30 30
31
Merge Sort (continued)
copyBuffer: an extra array used in merging. Allocated once in mergeSort, then passed to mergeSortHelper and merge. When mergeSortHelper is called, it needs to know the low and high (parameters that bound the subarray). After verifying that it has been passed a subarray of at least two items, mergeSortHelper computes the midpoint, sorts above and below, and calls merge to merge the results. 31 31
32
Merge Sort (continued)
Subarrays generated during calls of mergeSort 32 32
33
Merge Sort (continued)
Merging the subarrays generated during a merge sort 33 33
34
Merge Sort (continued)
The merge method combines two sorted subarrays into a larger sorted subarray. First between low and middle; second between middle + 1 and high. The process consists of: Set up index pointers (low and middle + 1). Compare items, starting with first item in subarray. Copy the smaller item to the copy buffer and repeat. Copy the portion of copyBuffer between low and high back to the corresponding positions of the array. 34 34
35
Merge Sort (continued)
Complexity Analysis for Merge Sort: The run time of the merge method is dominated by two for statements, each of which loop (high – low + 1) times. Run time: O(high – low). Number of stages: O(log n). Merge sort has two space requirements that depend on an array’s size: O(log n) is required on the call stack; O(n) space is used by the copy buffer. 35 35
36
Merge Sort (continued)
Improving Merge Sort: The first for statement makes a single comparison per iteration. A complex process that lets two subarrays merge without a copy buffer or changing the order of the method. Subarrays below a certain size can be sorted using a different approach. 36 36
37
CompareTo Method 37 37 Comparing Objects and the Comparable Interface:
When using binary search with an array of objects, we must compare two objects. <, >, and == are not good choices. The Comparable interface includes the method compareTo. 37 37
38
CompareTo Method (continued)
Comparing Objects and the Comparable Interface (cont): Before sending the compareTo message, the object must be cast to Comparable. Object does not implement the Comparable interface or include a compareTo method. 38 38
39
CompareTo Method (continued)
Implementing the Method compareTo: 39 39
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.