Lecture No.45 Data Structures Dr. Sohail Aslam.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Foundations of Algorithms, Fourth Edition
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
Sorting: Advanced Techniques Smt Genap
CS221: Algorithms and Data Structures Lecture #4 Sorting Things Out Steve Wolfman 2011W2 1.
Sorting – Part II CS 367 – Introduction to Data Structures.
CSCS-200 Data Structure and Algorithms Lecture
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Advanced Sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Searching and Sorting Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Week 12 - Wednesday CS221.
Teach A level Computing: Algorithms and Data Structures
Advance Analysis of Algorithms
Quicksort 1.
Quicksort and Mergesort
Chapter 4: Divide and Conquer
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
ITEC 2620M Introduction to Data Structures
QuickSort Previous slides based on ones by Ethan Apter & Marty Stepp
COMP 352 Data Structures and Algorithms
Yan Shi CS/SE 2630 Lecture Notes
Sub-Quadratic Sorting Algorithms
Chapter 4.
Quicksort.
Algorithms Dr. Youn-Hee Han April-May 2013
CSE 373 Data Structures and Algorithms
Quicksort.
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Lecture No.45 Data Structures Dr. Sohail Aslam

Divide and Conquer What if we split the list into two parts? 10 10 12 8 4 2 11 7 5 12 8 4 2 11 7 5

Divide and Conquer Sort the two parts: 4 8 10 12 2 5 7 11 10 12 8 4 2

Divide and Conquer Then merge the two parts together: 2 4 5 7 8 10 11 12 4 8 10 12 2 5 7 11 End of lecture 44.

Analysis To sort the halves  (n/2)2+(n/2)2 To merge the two halves  n So, for n=100, divide and conquer takes: = (100/2)2 + (100/2)2 + 100 = 2500 + 2500 + 100 = 5100 (n2 = 10,000) Start of lecture 45

Divide and Conquer Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At n = 1

Divide and Conquer Recall: Binary Search Search Search Search

Divide and Conquer Sort Sort Sort Sort Sort Sort Sort

Divide and Conquer Combine Combine Combine

Mergesort Mergesort is a divide and conquer algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented recursively

Mergesort The mergesort algorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group

Merging: animation 4 8 10 12 2 5 7 11 2

Merging: animation 4 8 10 12 2 5 7 11 2 4

Merging: animation 4 8 10 12 2 5 7 11 2 4 5

Merging 4 8 10 12 2 5 7 11 2 4 5 7

Mergesort Split the list in half. Mergesort the left half. 8 12 11 2 7 5 4 10 Split the list in half. Mergesort the left half. 8 12 4 10 Split the list in half. Mergesort the left half. 4 10 Mergesort the right. 10 4

Mergesort the right half. 8 12 11 2 7 5 4 10 10 4 8 12 Mergesort the right half. Merge the two halves. 8 12 10 4 4 10 8 12 Merge the two halves. 8 12

Mergesort the right half. 8 12 11 2 7 5 4 10 Merge the two halves. 10 12 8 4 10 4 8 12 Mergesort the right half. Merge the two halves. 10 4 4 10 10 4 8 12 8 12

Mergesort the right half. 4 8 10 12 11 2 7 5 11 2 7 5 11 2 11 2

Mergesort the right half. 4 8 10 12 11 2 7 5 2 11 11 2 7 5 2 11 2 11

Mergesort the right half. 4 8 10 12 11 2 7 5 2 11 2 11 7 5 5 7 7 5

Mergesort the right half. 4 8 10 12 11 2 7 5 2 11 11 2 7 5

Mergesort the right half. 4 8 10 12 2 5 7 11

Mergesort Merge the two halves. 2 4 5 7 8 10 11 12

Mergesort void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size]; if (tmpArrayPtr != NULL) mergeSortRec(array, size, tmpArrayPtr); else cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr;

Mergesort void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp); for (i = 0; i < size; i++) array[i] = tmp[i]; }

mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 aSize: 5 bSize: 6 tmp:

mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=0 j=0 tmp: k=0

mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=0 j=0 tmp: 3 k=0

mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=1 j=0 tmp: 3 5 k=1

mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=2 j=0 tmp: 3 5 6 k=2

mergeArrays a: b: i=2 j=1 tmp: k=3 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays a: b: i=2 j=2 tmp: k=4 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays a: b: i=2 j=3 tmp: k=5 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays a: b: i=3 j=3 tmp: k=6 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays a: b: i=3 j=4 tmp: k=7 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays a: b: i=4 j=4 tmp: k=8 3 5 15 28 30 6 10 14 22 43 50 3 5 6

mergeArrays Done. a: b: i=5 j=4 tmp: k=9 3 5 15 28 30 6 10 14 22 43 50

Merge Sort and Linked Lists

Mergesort Analysis . Merging the two lists of size n/2: O(n) Merging the four lists of size n/4: O(n) . O (lg n) times Merging the n lists of size 1: O(n) Mergesort is O(n lg n) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O(n) extra space for merging

Mergesort Analysis Mergesort is O(n lg n) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O(n) extra space for merging

Quicksort Quicksort is another divide and conquer algorithm Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value

Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 4 12 4 10 8 5 5 2 11 7 3 5 pivot value

Quicksort The pivot is swapped to the last position and the remaining elements are compared starting at the ends. 4 12 4 10 8 3 2 11 7 5 5 low high 5 pivot value

Quicksort Then the low index moves right until it is at an element that is larger than the pivot value (i.e., it is on the wrong side) 4 12 12 10 8 3 6 6 2 11 7 5 low high 5 pivot value

Quicksort Then the high index moves left until it is at an element that is smaller than the pivot value (i.e., it is on the wrong side) 4 12 4 10 8 3 6 6 2 2 11 7 5 low high 5 pivot value

Quicksort Then the two values are swapped and the index values are updated: 4 2 4 10 12 8 6 3 6 12 2 11 7 5 low high 5 pivot value

Quicksort This continues until the two index values pass each other: 4 2 10 3 8 10 3 6 6 12 11 7 5 low high 5 pivot value

Quicksort This continues until the two index values pass each other: 4 2 4 3 8 10 6 6 12 11 7 5 high low 5 pivot value

Quicksort Then the pivot value is swapped into position: 4 2 4 3 5 8 10 6 6 12 11 7 5 8 high low

Quicksort Recursively quicksort the two parts: 4 2 4 3 5 5 6 10 6 12 11 7 8 Quicksort the left part Quicksort the right part

Quicksort void quickSort(int array[], int size) { int index; if (size > 1) index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } End of lecture 45. At last!

Quicksort int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid); for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } swap(array, array+index); return index;

Data Structures-Course Recap Arrays Link Lists Stacks Queues Binary Trees Sorting