Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.

Similar presentations


Presentation on theme: "ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department."— Presentation transcript:

1 ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

2 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

3 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

4 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

5 Selection Sort

6 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 …

7 Selection Sort Before and after exchanging shortest book and the first book.

8 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

9 Selection Sort A selection sort of an array of integers into ascending order.

10 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 among a[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]. }

11 Selection Sort in Java

12 Merge Sort

13 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

14 Merge Algorithm 1. Access the first item from both sequences 2. 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 3. Copy any remaining items from the first sequence to the output sequence 4. Copy any remaining items from the second sequence to the output sequence

15 Algorithm Conceptually, merge sort works as follows: 1. Divide the unsorted list into two sublists of about half the size 2. Sort each of the two sublists recursively until we have list sizes of length 1, in which case the list itself is returned 3. Merge the two sorted sublists back into one sorted list. Mergesort incorporates two main ideas to improve its runtime: 1. A small list will take fewer steps to sort than a large list. 2. 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

16 How merge sort works

17 Pseudocode function mergesort(m) var list 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

18 function merge(left,right) var list 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 Pseudocode

19 MergeSort in Java

20

21 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

22 Quick Sort

23 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(N 2 ) but very unlikely to happen

24 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!

25 Quicksort Algorithm To sort a[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

26 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: p numbers less than p numbers greater than or equal to p p

27 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

28 Example of Partitioning choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 search: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 swap: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6 search: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6 swap: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6 search: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6 swap: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (left > right) swap with pivot: 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6

29 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

30 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

31 How Quick Sort works

32 Quick Sort in Java

33 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

34 Selection Sort Selection Sort: best, worst, and average cases are:  (n 2 ) Merge Sort: best, worst, and average cases are  (n log n) Quick Sort: Best and average cases are  (n log n) Worst case is  (n 2 )

35 Other Sort algorithms

36

37 Bubble Sort 1. Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. 2. If at least one swap has been done, repeat step 1.

38 Bubble Sort

39 Bubble Sort in Java Complexity:  (n 2 )

40 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.

41 Insertion Sort

42 Complexity:  (n 2 )

43 The end


Download ppt "ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department."

Similar presentations


Ads by Google