Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting And Searching CSE116A,B 2/22/2019 B.Ramamurthy.

Similar presentations


Presentation on theme: "Sorting And Searching CSE116A,B 2/22/2019 B.Ramamurthy."— Presentation transcript:

1 Sorting And Searching CSE116A,B 2/22/2019 B.Ramamurthy

2 Introduction The problem of sorting a collection of keys to produce an ordered collection is one of the richest in computer science. The richness derives from the fact that there are a number of ways of solving the sorting problem. Sorting is a fascinating operation that provides a model for investigating many problems in Computer Science. Ex: analysis and comparison of algorithms, divide and conquer, space and time tradeoff, recursion etc. 2/22/2019 B.Ramamurthy

3 Sorting Comparison based: selection, insertion sort (with online animations) Divide and conquer: merge sort, quick sort (with animations from supplements of your text book) Priority Queue /Heap based: heap sort Assume: Ascending order for all our discussion 2/22/2019 B.Ramamurthy

4 Selection Sort Select the first smallest element, place it in first location (by exchanging contents of locations), find the next smallest, place it in the next location, and so on. We will look at an example, an algorithm, analyze the algorithm, and look at code implementation. 2/22/2019 B.Ramamurthy

5 Selection Sort: Example
10 8 16 2 6 4 2 8 6 10 16 4 2 4 6 10 16 8 2 4 6 10 16 8 Sorted array 2 4 6 8 16 10 2 4 6 8 10 16 2/22/2019 B.Ramamurthy

6 Selection Sort Pseudo Code
Let cursor be pointer to first element of an n element list to be sorted. Repeat until cursor points to last but one element. 2.1 Search for the smallest element in the list starting from the cursor. Let it be at location target. 2.2 Exchange elements at cursor and target. 2.3 Update cursor to point to next element. 2/22/2019 B.Ramamurthy

7 Sort Analysis Let el be the list; n be the number of elements; cursor = 0; target = 0; while (cursor < n-1) 2.1.1 target = cursor; 2.1.2 for (i= cursor+1; i < n; i++) if (el[i] < el[target]) target =i; 2.2 exchange (el[target], el[cursor]); // takes 3 assignments 2.3 cursor++; (n-1) * *(n + (n-1) + (n-2) ..1) 4n – 4 + 2*n(n+1)/2 = 4n – 4 + n2 + n = n2 + 5n - 4 An2 + B n + C where A= 1, B = 5, C = -4; for large n, drop the constant, lower order term in n, and the multiplicative constant to get big-O notation O(n2) – quadratic sort 2/22/2019 B.Ramamurthy

8 Insertion Sort 10 8 16 2 6 4 Unsorted array 10 Trivially sorted 8 10
2/22/2019 B.Ramamurthy

9 Insertion Sort Pseudo Code
Single element is trivially sorted; start with first element; Repeat for second to nth element of the list: 2.1 cursor = next location; 2.2 Find a location to insert for list[cursor] by comparing and shifting; let the location be target; 2.3 Insert list[target] = list[cursor] 2/22/2019 B.Ramamurthy

10 Insertion Sort Analysis
cursor = 0; while (cursor < n) 2.1 cursor = cursor + 1; 2.2 temp = list[cursor] // save element to inserted j = cursor; //find location 2.2.3 while (j > 0 && list[j-1] > temp ) list[j] = list[j-1]; //shift right j = j –1; // assert : location found or hit left end(j=0) 2.3 list[j] = temp; Worst case: O(n2) quadratic Best case : linear (when the list is already sorted) 2/22/2019 B.Ramamurthy

11 Merge Sort Divide and Conquer
Divide the list into two subsets s1, and s2 (recurse) Sort s1 and s2 by divide and conquer (conquer) Merge the sorted s1 and s2. O(n log n) algorithm 2/22/2019 B.Ramamurthy

12 Merge Sort (s) mergeSort(s): If S.size() > 1
1. S1, S2  partition (S, n/2) 2. mergeSort(s1); 3. mergeSort(s2); 4. S  merge(s1,s2) Lets look at examples. 2/22/2019 B.Ramamurthy

13 Example 10 8 16 2 6 4 10 8 6 16 2 4 S1 S2 10 8 6 S11 S12 8 6 S121 S122 2/22/2019 B.Ramamurthy


Download ppt "Sorting And Searching CSE116A,B 2/22/2019 B.Ramamurthy."

Similar presentations


Ads by Google