Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

LEUCEMIA MIELOIDE AGUDA TIPO 0
Lecture Computer Science I - Martin Hardwick Searching and sorting rReasons for not using the most efficient algorithm include: l The more efficient.
Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Chapter 40 - Physiology and Pathophysiology of Diuretic Action Copyright © 2013 Elsevier Inc. All rights reserved.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
and 6.855J Cycle Canceling Algorithm. 2 A minimum cost flow problem , $4 20, $1 20, $2 25, $2 25, $5 20, $6 30, $
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING Think Distributive property backwards Work down, Show all steps ax + ay = a(x + y)
Addition Facts
ALGEBRAIC EXPRESSIONS
Year 6 mental test 10 second questions Numbers and number system Numbers and the number system, fractions, decimals, proportion & probability.
Zabin Visram Room CS115 CS126 Searching
Chapter 9 continued: Quicksort
ZMQS ZMQS
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Parallel List Ranking Advanced Algorithms & Data Structures Lecture Theme 17 Prof. Dr. Th. Ottmann Summer Semester 2006.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
ABC Technology Project
O X Click on Number next to person for a question.
© S Haughton more than 3?
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
Twenty Questions Subject: Twenty Questions
Linking Verb? Action Verb or. Question 1 Define the term: action verb.
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
This, that, these, those Number your paper from 1-10.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Event 4: Mental Math 7th/8th grade Math Meet ‘11.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
Section 5: More Parallel Algorithms
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
Advanced Sorting Methods: Shellsort Shellsort is an extension of insertion sort, which gains speed by allowing exchanges of elements that are far apart.
1 Ke – Kitchen Elements Newport Ave. – Lot 13 Bethesda, MD.
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 3, 9, 3, 9, 7, 3, 9, 7, 6, First we use the number in the centre of the list as a ‘pivot’. We then.
1 Unit 1 Kinematics Chapter 1 Day
O X Click on Number next to person for a question.
Other Dynamic Programming Problems
Building Java Programs Chapter 13
Copyright © Cengage Learning. All rights reserved.
Copyright © Cengage Learning. All rights reserved.
Quicksort File: D|\data\bit143\Fall01\day1212\quicksort.sdd BIT Gerard Harrison Divide and Conquer Reduce the problem by reducing the data set. The.
CHAPTER 11 Sorting.
S: Application of quicksort on an array of ints: partitioning.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Partitioning in Quicksort n How do we partition the array efficiently? – choose partition element to be rightmost element – scan from right for smaller.
Presentation transcript:

Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm

Sorting- problem definition A sorting algorithm is an algorithm that puts elements of a list in a certain order. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. 2

Sorting- algorithms properties 3

Comparison sorting 4

Quicksort quickSort(A) quickSort(A, 0, A.length) quickSort(A, low, high) if (high > low){ pivot  partition(A, low, high) quickSort(A, low, pivot-1) quickSort(A, pivot+1, high) } 5

Quicksort cont’d partition routine chooses a pivot, partitions the array around it and returns its index low pivot high leftright left Swap! left right right left pivot

Quicksort cont’d int partition( A, low, high ) pivot_item  A[low] left  low, right  high, pivot  left while ( left < right ) { while (left < high && A[left] ≤ pivot_item) // Scan right left++ while (A[right] > pivot_item) // Scan left right-- if (left < right) swap(A, left, right) } // Right is the final position of the pivot swap(A, pivot, right) return right 7

Quicksort cont’d 8

Question 1 9

Question 1 cont’d The solution is based on quicksort algorithm. Select(k, S) { // Returns k-th element in S. pick x in S partition S into L, E, G such that: max(L) < x, E = {x}, x < min(G) if k ≤ length(L) // Searching for item ≤ x return Select(k, L) else if k ≤ length(L) + length(E) // Found return x else // Searching for item ≥ x return Select(k - length(L) - length(E), G) 10

Question 1 cont’d 11

Question 2 12

Question 2 cont’d 13

Question 2 cont’d 14

Question 3 15

Question 3 cont’d 16

Question 4 Given the following algorithm to sort an array A of size n, argue its correctness and find its recurrence formula. newSort(A) if |A| = 2 if A[0] > A[1] swap(A, 0, 1) retutn A else newSort(A[1..2n/3]) // Sort recursively the first 2/3 of A newSort(A[n/3+1..n]) // Sort recursively the last 2/3 of A newSort(A[1..2n/3])) // Sort recursively the first 2/3 of A 17

Question 4 cont’d 18

Question 4 19

Question 4 cont’d 20