Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras.

Similar presentations


Presentation on theme: "Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras."— Presentation transcript:

1 Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras

2 Joseph Lindo Activity Algorithms Definition Reason Sorting An operation that segregates items into groups according to specified criterion A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 } Definition

3 Joseph Lindo Activity Algorithms Definition Reason Sorting Why sorting? Examples: Sorting Books in Library (Dewey system) Sorting Individuals by Height (Feet and Inches) Reason

4 Joseph Lindo Activity Algorithms Definition Reason Sorting Why sorting? Examples: Sorting Movies in Blockbuster (Alphabetical) Sorting Numbers (Sequential) Reason

5 Joseph Lindo Activity Algorithms Definition Reason Sorting Why sorting? It is used in a wide range of applications Several sorting algorithms have been invented because the task is so fundamental and also frequently used Reason

6 Joseph Lindo Activity Algorithms Definition Reason Sorting Algorithms

7 Joseph Lindo Activity Algorithms Definition Reason Sorting Algorithms Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Algorithms

8 Joseph Lindo Activity Algorithms Definition Reason Sorting Group Case Study Group maximum of five members Short bond paper Create the flowchart of:  Bubble Sort  Selection Sort  Insertion Sort  Merge Sort  Quick Sort Activity

9 Joseph Lindo Activity Algorithms Definition Reason Sorting Group Case Study Group maximum of five members Yellow paper Construct your own “Sorting Algorithm” Provide example Provide the Code Activity

10 Joseph Lindo Sorting Algorithms --end-- Sir Joseph Lindo University of the Cordilleras

11 Joseph Lindo Insertion Bubble Merge Selection Quick Sorting Algorithms Bubble Sort The simple idea is keep passing through the list, swapping the next element that is out of order, until the list is sorted. Bubble

12 Joseph Lindo Insertion Bubble Merge Selection Quick Sorting Algorithms Selection Sort Works by selecting elements in specific order, and putting them in proper position in the final list It is an in-place sorting algorithm Selection

13 Joseph Lindo Insertion Bubble Merge Selection Quick Sorting Algorithms Insertion Sort It is a comparison based sort, which sorts the array one entry at a time. Insertion

14 Joseph Lindo Insertion Bubble Merge Selection Quick Sorting Algorithms Merge Sort Original problem is split into subproblems Solutions to subproblems lead to the solution of the main problem Merge

15 Joseph Lindo Insertion Bubble Merge Selection Quick Sorting Algorithms Quick Sort Invented by C.A.R. Hoare Also based on the divide-and- conquer paradigm Quick

16 Joseph Lindo Characteristics jo Stable Algorithm A stable sort keeps equal elements in the same order This may matter when you are sorting data according to some characteristic Example: sorting students by test scores

17 Joseph Lindo Characteristics jo Stable Algorithm Bob Ann Joe Zöe Dan Pat Sam 90 98 86 75 86 90 original array Bob Ann Joe Zöe Dan Pat Sam 90 98 86 75 86 90 stably sorted

18 Joseph Lindo Characteristics Unstable Algorithm An unstable sort may or may not keep equal elements in the same order Stability is usually not important, but sometimes it is important

19 Joseph Lindo Characteristics Unstable Algorithm Bob Ann Joe Zöe Dan Pat Sam 90 98 86 75 86 90 original array Bob Ann Joe Zöe Dan Pat Sam 90 98 86 75 86 90 unstably sorted

20 Joseph Lindo Characteristics Bubble Sort Compare each element (except the last one) with its neighbor to the right Compare each element (except the last two) with its neighbor to the right Compare each element (except the last three) with its neighbor to the right Repeat the process

21 Joseph Lindo Diagram Bubble Sort 7 2854 2 7854 2 7854 2 7584 2 7548 2 7548 2 5748 2 5478 2 7548 2 5478 2 4578 2 5478 2 4578 2 4578 (done)

22 Joseph Lindo Code Bubble Sort public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--){ for (inner = 0; inner a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }

23 Joseph Lindo Characteristics Selection Sort Given an array of length n, Search elements 0 through n-1 and select the smallest Swap it with the element in location 0) Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 Continue in this fashion until there’s nothing left to search

24 Joseph Lindo Diagram Selection Sort 7 2854 2 7854 2 4857 2 4587 2 4578

25 Joseph Lindo Code Selection Sort public static void selectionSort(int[] a){ int outer, inner, min; for(outer=0;outer<a.length-1;outer++){ min = outer; for(inner=outer+1;inner<a.length;inner++){ if (a[inner] < a[min]){ min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; }

26 Joseph Lindo Characteristics Insertion Sort Repeated the following steps until no elements are left in the unsorted part of the array – First available element is selected from the unsorted section of the array – Place selected element in its proper position in the sorted section of the array

27 Joseph Lindo Diagram Insertion Sort 27 3 471214 2021 33 3810559 232816 sortednext to be inserted 3 47 559 232816 10 temp 3833 21 2014 12 10 sorted less than 10

28 Joseph Lindo Code Insertion Sort void insertionSort(Object array[], int startIdx, int endIdx){ for (int i = startIdx; i < endIdx; i++){ int k = i; for (int j = i + 1; j < endIdx; j++){ if (array[k]>array[j]){ k = j; } int temp = array[k]; array[k] = array[j]; array[j] = temp; } } }

29 Joseph Lindo Characteristics Merge Sort Three steps: – Divide Divide the sequence of data elements into two halves – Conquer Conquer each half sorting – Combine Combine or merge the two halves to come up with the sorted sequence

30 Joseph Lindo Diagram Merge Sort Merge SORT Divide into two halves FirstPartSecondPart FirstPart SecondPart A A is sorted!

31 Joseph Lindo Diagram Merge Sort 6101422 351528 L:R: Temporary Arrays 515283061014 5 23781456 A:

32 Joseph Lindo Diagram Merge Sort 3515283061014 L: A: 3152830 6101422 R: i=0 j=0 k=0 2378 1456 1

33 Joseph Lindo Diagram Merge Sort 1515283061014 L: A: 351528 6101422 R: k=1 2378 1456 2 i=0 j=1

34 Joseph Lindo Diagram Merge Sort 1215283061014 L: A: 6101422 R: i=1 k=2 2378 1456 3 j=1

35 Joseph Lindo Diagram Merge Sort 12361014 L: A: 6101422 R: i=2 j=1 k=3 2378 1456 4

36 Joseph Lindo Diagram Merge Sort 123461014 L: A: 6101422 R: j=2 k=4 2378 1456 i=2 5

37 Joseph Lindo Diagram Merge Sort 1234561014 L: A: 6101422 R: i=2 j=3 k=5 2378 1456 6

38 Joseph Lindo Diagram Merge Sort 12345614 L: A: 6101422 R: k=6 2378 1456 7 i=2

39 Joseph Lindo Diagram Merge Sort - 123456714 L: A: 351528 6101422 R: 2378 1456 8 i=3 k=7

40 Joseph Lindo Diagram Merge Sort 12345678 L: A: 351528 6101422 R: 2378 1456 i=4 k=8

41 Joseph Lindo Code Merge Sort void mergeSort(Object array[], int startIdx,int endIdx){ if (array.length != 1) { mergeSort(leftArr,startIdx, midIdx); mergeSort(rightArr, midIdx+1,endIdx); combine(leftArr, rightArr); } }

42 Joseph Lindo Characteristics Quick Sort Divide-and-Conquer Paradigm – Divide Partition array into two subarrays A[p...q-1] and A[q+1...r] such that each element in A[p...q-1] is less than or equal to A[q] and each element in A[q+1...r] is greater than or equal to A[q] A[q] is called the pivot – Conquer Sort the subarrays

43 Joseph Lindo Diagram Quick Sort x < p p p ≤ x Partition FirstPart SecondPart p pivot A: x < p p p ≤ x Sorted FirstPart Sorted SecondPart Sorted

44 Joseph Lindo Diagram Quick Sort p p x < pp ≤ x p x < p A: A: A: p

45 Joseph Lindo Diagram Quick Sort A: 48635172

46 Joseph Lindo Diagram Quick Sort 48635172 i=0 j=1 A:

47 Joseph Lindo Diagram Quick Sort A: j=1 48635172 i=0 8

48 Joseph Lindo Diagram Quick Sort A: 486351726 i=0 j=2

49 Joseph Lindo Diagram Quick Sort 4 8 635172 i=0 3 83 j=3i=1A:

50 Joseph Lindo Diagram Quick Sort A: 43685172i=1 5 j=4

51 Joseph Lindo Diagram Quick Sort A: 43685172i=1 1 j=5

52 Joseph Lindo Diagram Quick Sort A: 43685172i=2 16 j=5

53 Joseph Lindo Diagram Quick Sort A: 438572i=2 16 7 j=6

54 Joseph Lindo Diagram Quick Sort A: 438572i=2 16 2 2 8i=3

55 Joseph Lindo Diagram Quick Sort A: 432678i=3 15

56 Joseph Lindo Diagram Quick Sort A: 41678i=3 25 4 2 3

57 Joseph Lindo Diagram Quick Sort A: 3 678 1 5 42 x < 4 4 ≤ x pivot in correct position

58 Joseph Lindo Code Quick Sort void quickSort(Object array[], int leftIdx, int rightIdx){ int pivotIdx; /* Termination condition! */ if (rightIdx > leftIdx) { pivotIdx = partition(array, leftIdx, ightIdx); quickSort(array, leftIdx, pivotIdx-1); quickSort(array, pivotIdx+1, rightIdx); } }

59 Joseph Lindo Stable Sort Algorithm More Unstable Sort Algorithm


Download ppt "Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras."

Similar presentations


Ads by Google