430 likes | 440 Views
King Fahd University of Petroleum & Minerals College of Computer Science & Engineering. Information & Computer Science Department. ICS201 Lecture 21 : Sorting. Objectives. To learn how to use the standard sorting methods in the Java API
E N D
King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department ICS201 Lecture 21 : Sorting
Objectives • To learn how to use the standard sorting methods in the Java API • To learn how to implement the following sorting algorithms: • selection sort, • mergesort, and • quicksort • To understand the difference in performance of these algorithms, and • which to use for small arrays, • which to use for medium arrays, and • which to use for large arrays
Definition and applications of Sorting • Definition : Sorting is the task of putting data into a particular order either ascending or descending • The most intensively studied processing • Internal sorting - data fit into RAM • External sorting - data are on the disk • Sorting speeds up search: • Dictionary • Files in a directory • Calendar • Phone list
Using Java Sorting Methods • Java API provides a class Arrays with several overloaded static sort methods for different array types • The Collections class provides similar sorting methods • Sorting methods for arrays of primitive types are based on quicksort algorithm • Method of sorting for arrays of objects and Lists based on mergesort
Selection Sort • Sorting: Arrange things into either ascending or descending order • Task: rearrange books on shelf by height • Shortest book on the left • Approach: • Look at books, select shortest book • Swap with first book • Look at remaining books, select shortest • Swap with second book • Repeat …
Selection Sort Before and after exchanging shortest book and the first book.
Selection Sort • The Selection sort begins at the front of the list and looks for the first element smaller (or larger) than the current element. Then the elements are switched. • The actual identification of the element to switch is based upon the type of sort • Ascending or descending
Selection Sort A selection sort of an array of integers into ascending order.
Selection Sort • Iterative algorithm for selection sort Algorithm selectionSort(a, n) // Sorts the first n elements of an array a. for (index = 0; index < n - 1; index++){ indexOfNextSmallest = the index of the smallest value amonga[index], a[index+1], . . . , a[n-1]Interchange the values of a[index] and a[indexOfNextSmallest] // Assertion: a[0] £ a[1] £ . . . £ a[index], and these are the smallest// of the original array elements. // The remaining array elements begin at a[index+1].}
Mergesort • A recursive divide-and-conquer algorithm • A merge is a common data processing operation that is performed on two sequences of data with the following characteristics • Both sequences contain items with a common compareTo method • The objects in both sequences are ordered in accordance with this compareTo method
Merge Algorithm • Access the first item from both sequences • While not finished with either sequence Compare the current items from the two sequences, copy the smaller current item to the output sequence, and access the next item from the input sequence whose item was copied • Copy any remaining items from the first sequence to the output sequence • Copy any remaining items from the second sequence to the output sequence
Algorithm • Conceptually, merge sort works as follows: • Divide the unsorted list into two sublists of about half the size • Sort each of the two sublists recursively until we have list sizes of length 1, in which case the list itself is returned • Merge the two sorted sublists back into one sorted list. • Mergesort incorporates two main ideas to improve its runtime: • A small list will take fewer steps to sort than a large list. • Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists. For example, you only have to traverse each list once if they're already sorted
Pseudocode function mergesort(m) varlist left, right, result if length(m) ≤ 1 return m else middle = length(m) / 2 for each x in m up to middle add x to left for each x in m after middle add x to right left = mergesort(left) right = mergesort(right) result = merge(left, right) return result
Pseudocode function merge(left,right) varlist result while length(left) > 0 and length(right) > 0 if first(left) ≤ first(right) append first(left) to result left = rest(left) else append first(right) to result right = rest(right) if length(left) > 0 append left to result if length(right) > 0 append right to result return result
Merge Sort • The merge sort algorithm requires recursion • The concept used in this algorithm is called divide-and-conquer • The array is first split into two smaller arrays • Each of the smaller two arrays are recursively sorted • The two arrays are merged back into one array • The base case for the merge sort is when the array to be sorted only has one element left that does not need to be rearranged • If there is more than one element to sort, split the arrays into two sections and call the merge sort
Quicksort • Developed in 1962 by C. A. R. Hoare • Recursive • Divide-and-conquer • The fastest algorithm known • Average running time O(N log N) • Worst-case running time O(N2) but very unlikely to happen
Quicksort • Quicksort rearranges an array into two parts so that all the elements in the left subarray are less than or equal to a specified value, called the pivot • Quicksort ensures that the elements in the right subarray are larger than the pivot • Advantage: • No extra memory is needed • Fast running time (in average) • Disadvantage: • Unstable in running time • Finding “pivot” element is a big issue!
Quicksort Algorithm To sorta[left...right]: 1.if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right] 2.Terminate
p p numbers greater than or equal top numbers less thanp Partitioning • A key step in the Quicksort algorithm is partitioning the array • We choose some (any) number p in the array to use as a pivot • We partition the array into three parts:
Partitioning • Choose an array value (say, the first) to use as the pivot • Starting from the left end, find the first element that is greater than or equal to the pivot • Searching backward from the right end, find the first element that is less than the pivot • Interchange (swap) these two elements • Repeat, searching from where we left off, until done
Example of Partitioning • choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 • search: 436 9 2 4 3 1 2 1 8 9 35 6 • swap: 433 9 2 4 3 1 2 1 8 9 65 6 • search: 43 39 2 4 3 1 2 18 9 6 5 6 • swap: 4331 2 4 3 1 2 98 9 65 6 • search: 433 1 24 3 1 29 8 9 6 5 6 • swap: 433 1 22 3 1 49 8 9 6 5 6 • search: 433 1 2 2 31498 9 65 6(left > right) • swap with pivot: 13 3 1 2 2 34498 9 6 5 6
Partitioning To partition a[left...right]: 1. Set pivot = a[left], l = left + 1, r = right; 2. while l < r, do 2.1. while l < right & a[l] < pivot , set l = l + 1 2.2. while r > left & a[r] >= pivot , set r = r - 1 2.3. if l < r, swap a[l] and a[r] 3. Set a[left] = a[r], a[r] = pivot 4. Terminate
Selecting the Pivot • First element - a bad choice • In general, don't select the pivot near the beginning or the end of the set • Middle element is a good choice • Median-of-three - the median of the first, middle, and last elements
Comparison of Sorts • Quick Sort and Merge Sort will always be faster than the Selection sort. • Quick Sort sorts “in place” and the hidden constant in the average case is smaller than Merge Sort's • Merge Sort’s worst case behavior is better than Quick Sort’s worst case
Selection Sort • Selection Sort: • best, worst, and average cases are: Q(n2) • Merge Sort: • best, worst, and average cases are Q(n log n) • Quick Sort: • Best and average cases are Q(n log n) • Worst case is Q(n2)
Bubble Sort • Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. • If at least one swap has been done, repeat step 1.
Bubble Sort in Java • Complexity: Q(n2)
Insertion Sort • Array is imaginary divided into two parts - sorted one and unsorted one. • At the beginning, sorted part contains first element of the array and unsorted one contains the rest. • At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. • When unsorted part becomes empty, algorithm stops.
Insertion Sort • Complexity: Q(n2)