Presentation is loading. Please wait.

Presentation is loading. Please wait.

M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.

Similar presentations


Presentation on theme: "M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1."— Presentation transcript:

1 M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1

2 2 Sorting A fundamental application for computers. Done to make finding data (searching) faster. Many different algorithms for sorting. One of the difficulties with sorting is working with a fixed size storage container (array) –if resize, that is expensive (slow)

3 Types of Sorting Algorithms There are many, many different types of sorting algorithms, but we will study the following primary algorithms: ● Selection Sort ● Insertion Sort ● Quick Sort ● Merge Sort

4 Big-Oh to the Selected Sorts ● Selection Sort = n² ● Insertion Sort = n² ● Merge Sort = n log(n) ● Quick Sort = n log(n)

5 Selection Sort 5

6 6 How does it work? –Search through the list and find the smallest element. –Swap the smallest element with the first element. –Repeat starting at second element and find the second smallest element.

7 7 Selection Sort: Example 3565306020 scan 0-4, smallest 20 swap 35 and 20 2065306035scan 1-4, smallest 30 swap 65 and 30 2030656035scan 2-4, smallest 35 swap 65 and 35 2030356065scan 3-4, smallest 60 swap 60 and 60 2030356065done

8 8 Selection Sort: The Code public static void selectionSort(int[] list) {int min; int temp; for(int i = 0; i < list.length - 1; i++) { min = i; for(int j = i + 1; j < list.length; j++) if( list[j] < list[min] ) min = j; temp = list[i]; list[i] = list[min]; list[min] = temp; }

9 Insertion Sort 9

10 10 Insertion Sort Based on technique of card players to arrange a hand –Player keeps cards picked up so far in sorted order –When the player picks up a new card Makes room for the new card Then inserts it in its proper place To make room: Hold the target value in a variable Shuffle elements to the right until gap at right place.

11 11 Insertion Sort The first item is sorted Compare the second item to the first –if smaller swap Third item, compare to item next to it –need to swap –after swap compare again And so forth…

12 Insert Action: i=1 2085107 20 5107 8 temp 8 i = 1, first iteration 8205107--- Insertion Sort : Example

13 Insert Action: i=2 8205107 820 107 temp 5 5 i = 2, second iteration 88201075 5820107---

14 Insert Action: i=3 5820107 5820 7 temp 10 i = 3, third iteration 5810207---

15 Insert Action: i=4 5810207 581020 5810 20 5881020 7 temp 7 7 7 i = 4, forth iteration 5781020---

16 16 Insertion Sort: The Code public void insertionSort(int[] list) {int temp, j; for(int i = 1; i < list.length; i++) {temp = list[i]; j = i; while( j > 0 && temp < list[j - 1]) {// swap elements list[j] = list[j - 1]; list[j - 1] = temp; j--; }

17 Quick Sort 17

18 Quicksort The general idea behind Quicksort is this: Break an array into two parts Move elements around so that all the larger values are in one end and all the smaller values are in the other. Each of the two parts is then subdivided in the same manner, and so on until the subparts contain only a single value, at which point the array is sorted.

19 Quicksort: Example –To illustrate the process, suppose an unsorted array, called a, looks like the following figure.

20 Phase 1 1.If the length of the array is less than 2, then done. 2.Locate the value in the middle of the array and call it the pivot. The pivot is 7 in this example. 3.Tag the elements at the left and right ends of the array as i and j, respectively. Quicksort: Example

21 4.While a[i] < pivot value, increment i. While a[j] >= pivot value, decrement j : Quicksort: Example

22 5.If i > j then end the phase else interchange a[i] and a[j]: Quicksort: Example

23 6.Increment i and decrement j. If i > j then end the phase: Quicksort: Example

24 7.Repeat step 4, i.e., While a[i] < pivot value, increment i While a[j] >= pivot value, decrement j : Quicksort: Example

25 8.Repeat step 5, i.e., If i > j then end the phase else interchange a[i] and a[j]: Quicksort: Example

26 9.Repeat step 6, i.e., Increment i and decrement j. If i < j then end the phase: Quicksort: Example

27 10.Repeat step 4, i.e., While a[i] < pivot value, increment i While a[j] >= pivot value, decrement j : Quicksort: Example

28 11.Repeat step 5, i.e., If i > j then end the phase else interchange a[i] and a[j]. Quicksort: Example

29 –This ends the phase. –Split the array into the two subarrays a[0..j] and a[i..10]. –For clarity, the left subarray is shaded. –Notice that all the elements in the left subarray are less than or equal to the pivot, and those in the right are greater than or equal. Quicksort: Example

30 Phase 2 and Onward –Reapply the process to the left and right subarrays and then divide each subarray in two and so on until the subarrays have lengths of at most one. Quicksort: Example

31 Quicksort: The Code void quickSort (int[] a, int left, int right){ if (left >= right) return; int i = left; int j = right; int pivotValue = a[(left + right) / 2]; while (i < j){ while (a[i] < pivotValue) i++; while (pivotValue < a[j]) j--; if (i <= j){ int temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; } quickSort (a, left, j); quickSort (a, i, right); }

32 Merge Sort 32

33 33 Merge Sort Algorithm 1.If a list has 1 element or 0 elements it is sorted 2.If a list has more than 2 split into into 2 separate lists 3.Perform this algorithm on each of those smaller lists 4.Take the 2 sorted lists and merge them together How Does it Work?

34 34 Merge Sort

35 35 Merge Sort: Example Say unsorted array values are: 12, 9, 4, 99, 120, 1, 3, 10

36 Merge Sort: The Code 36 See the attached code


Download ppt "M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1."

Similar presentations


Ads by Google