Joseph Lindo Sorting Algorithms Sir Joseph Lindo University of the Cordilleras
Joseph Lindo Activity Algorithms Definition Reason Sorting An operation that segregates items into groups according to specified criterion A = { } A = { } Definition
Joseph Lindo Activity Algorithms Definition Reason Sorting Why sorting? Examples: Sorting Books in Library (Dewey system) Sorting Individuals by Height (Feet and Inches) Reason
Joseph Lindo Activity Algorithms Definition Reason Sorting Why sorting? Examples: Sorting Movies in Blockbuster (Alphabetical) Sorting Numbers (Sequential) Reason
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
Joseph Lindo Activity Algorithms Definition Reason Sorting Algorithms
Joseph Lindo Activity Algorithms Definition Reason Sorting Algorithms Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Algorithms
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
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
Joseph Lindo Sorting Algorithms --end-- Sir Joseph Lindo University of the Cordilleras
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
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
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
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
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
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
Joseph Lindo Characteristics jo Stable Algorithm Bob Ann Joe Zöe Dan Pat Sam original array Bob Ann Joe Zöe Dan Pat Sam stably sorted
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
Joseph Lindo Characteristics Unstable Algorithm Bob Ann Joe Zöe Dan Pat Sam original array Bob Ann Joe Zöe Dan Pat Sam unstably sorted
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
Joseph Lindo Diagram Bubble Sort (done)
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; } } } }
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
Joseph Lindo Diagram Selection Sort
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; }
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
Joseph Lindo Diagram Insertion Sort sortednext to be inserted temp sorted less than 10
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; } } }
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
Joseph Lindo Diagram Merge Sort Merge SORT Divide into two halves FirstPartSecondPart FirstPart SecondPart A A is sorted!
Joseph Lindo Diagram Merge Sort L:R: Temporary Arrays A:
Joseph Lindo Diagram Merge Sort L: A: R: i=0 j=0 k=
Joseph Lindo Diagram Merge Sort L: A: R: k= i=0 j=1
Joseph Lindo Diagram Merge Sort L: A: R: i=1 k= j=1
Joseph Lindo Diagram Merge Sort L: A: R: i=2 j=1 k=
Joseph Lindo Diagram Merge Sort L: A: R: j=2 k= i=2 5
Joseph Lindo Diagram Merge Sort L: A: R: i=2 j=3 k=
Joseph Lindo Diagram Merge Sort L: A: R: k= i=2
Joseph Lindo Diagram Merge Sort L: A: R: i=3 k=7
Joseph Lindo Diagram Merge Sort L: A: R: i=4 k=8
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); } }
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
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
Joseph Lindo Diagram Quick Sort p p x < pp ≤ x p x < p A: A: A: p
Joseph Lindo Diagram Quick Sort A:
Joseph Lindo Diagram Quick Sort i=0 j=1 A:
Joseph Lindo Diagram Quick Sort A: j= i=0 8
Joseph Lindo Diagram Quick Sort A: i=0 j=2
Joseph Lindo Diagram Quick Sort i= j=3i=1A:
Joseph Lindo Diagram Quick Sort A: i=1 5 j=4
Joseph Lindo Diagram Quick Sort A: i=1 1 j=5
Joseph Lindo Diagram Quick Sort A: i=2 16 j=5
Joseph Lindo Diagram Quick Sort A: i= j=6
Joseph Lindo Diagram Quick Sort A: i= i=3
Joseph Lindo Diagram Quick Sort A: i=3 15
Joseph Lindo Diagram Quick Sort A: 41678i=
Joseph Lindo Diagram Quick Sort A: x < 4 4 ≤ x pivot in correct position
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); } }
Joseph Lindo Stable Sort Algorithm More Unstable Sort Algorithm