SlideShare a Scribd company logo
1 of 32
Prof. Lili Saghafi
proflilisaghafi@gmail.com
1
Sorting Algorithms
Algorithms
and Data Structures
Quick Sort
By : Prof. Lili Saghafi
proflilisaghafi@gmail.com
@Lili_PLS
Prof. Lili Saghafi
proflilisaghafi@gmail.com
2
2
Objectives
–To study and analyze time complexity of
various sorting algorithms
–To design, implement, and analyze
selection sort, insertion sort
–To design, implement, and analyze bubble
sort To design, implement, and analyze
merge sort
–– To design, implement, and analyzeTo design, implement, and analyze
quick sortquick sort
Prof. Lili Saghafi
proflilisaghafi@gmail.com
3
3
Why study sorting?
Sorting is a classic subject in computer science.
There are three reasons for studying sorting algorithmssorting algorithms.
– First, sorting algorithms illustrate many creative
approaches to problem solving
– Second, sorting algorithms are good for practicing
fundamental programming techniques using
selection statements, loops, methods, and
arrays.
– Third, sorting algorithms are excellent examples to
demonstrate algorithm performance.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
4
4
What data to sort?
The data to be sorted might be integers,
doubles, characters, or objects.
For “Sorting Arrays,” for simplicity lets
assume:
• data to be sorted are integers,
• data are sorted in ascending order, and
• data are stored in an array.
• The programs can be easily modified to sort
other types of data, to sort in descending order,
or to sort data in an ArrayList or a LinkedList.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
5
5
Sorting Arrays
• Sorting, like searching, is also a common task in computer
programming.
• Sorting would be used, for instance, if you wanted to display
the grades in alphabetical order.
• Many different algorithms have been developed for
sorting.
• Selection Sort | Insertion Sort | Bubble Sort | Radix
Sort | Merge Sort | Merge two sorted lists | QuickQuick
SortSort | Partition in quick sort
• Quicksort is a little more complex than selection and bubble
• Best choice for general purpose and in memory sorting
• Used to be the standard algorithm for sorting of arrays of
primitive types in Java
Prof. Lili Saghafi
proflilisaghafi@gmail.com
6
6
Quick Sort
•Quick sort, developed by C. A. R. Hoare (1962),
works as follows:
– The algorithm selects an element, called the pivot, in the
array.
– Divide the array into two parts such that all the elements
in the first part are less than or equal to the pivot and
all the elements in the second part are greater than the
pivot.
– Recursively apply the quick sort algorithm to the first part
and then the second part.
Sir Charles Antony Richard Hoare
1980 Turing Award
Prof. Lili Saghafi
proflilisaghafi@gmail.com
7
Quick Sort
• Quicksort honoured as one of top 10
algorithms of 20th century in science and
engineering.
Basic plan.
• Shuffle the array.
• Partition so that, for some j
– entry a[j] is in place
– no larger entry to the left of j
– no smaller entry to the right of j
– Sort each piece recursively.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
8
Important factor in Quick Sort is Pivot
PIVOT needs to have following condition
after we sort it
Prof. Lili Saghafi
proflilisaghafi@gmail.com
9
What we need to choose in next
step
We need to swap these two
Now we need to repeat , choose
item from left and item from right
and if needed swap the two.
Quicksort is
Recursive sort
Prof. Lili Saghafi
proflilisaghafi@gmail.com
10
• We repeat until we see that item from left has a greater index
than item from right, so we know we're done .( LOW to HIGH , If
HIGH is less than LOW and pivot )
• we STOP and swap item from left ( HIGH) with our pivot 3 , our
pivot is now in its correct spot
We swap item from left
with are PIVOT
Prof. Lili Saghafi
proflilisaghafi@gmail.com
11
Characteristic of PIVOT
Quicksort is Recursive , we
choose larger partition
We choose 7 as are pivot
and move it to the end
So we swap 8 & 6
then we swap
8 and pivot
Now we have our PIVOT 7 in
the right place
We leave Recursion handle
the rest
We move pivot to the
end
STOP When
item from left
has a greater
index than
item from right
Prof. Lili Saghafi
proflilisaghafi@gmail.com
12
how do we choose the pivot
Choose the median of the array
•One popular method is called median of three
•In this method we look at the first middle and last elements of the
array we sort them properly and choose the middle item as our
pivot
•We're making the guess that the middle of these three items
could be close to the median of the entire array and as you can
see it's not too far off
Prof. Lili Saghafi
proflilisaghafi@gmail.com
13
Why does it work?
• On the partition step, algorithm divides thealgorithm divides the
array into two partsarray into two parts and every element a from
the left part is less or equal than every
element b from the right part.
• Also a and b satisfy a ≤ pivot ≤ b inequality.
• After completion of the recursion calls both of
the parts become sorted and, taking into account
arguments stated above, the whole array is
sorted.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
14
Quick Sort Partition
How it works
(Quick Sort Partition )
• In the beginning , First element as PIVOT
• Element after PIVOT as LOW the last element as HIGH
• Is LOW less than PIVOT ? Yes , Then move LOW to next element
– If NOT then we move HIGH one to the right since list[high] > pivot,
high was moved backward.
– Is LOW greater than PIVOT ? Yes ,
– and IF HIGH is lower than PIVOT then we swap the low and high
– If NOT then we move HIGH one to the right since list[high] > pivot,
high was moved backward.
– REPEAT until LOW and HIGH are equal .
– If it is MORE than PIVOT then we move high to the right.
– If it is LESS than Pivot then we swap high and pivot
– Now the partition is complete and PIVOTE makes the array into two sub
arrays for sorting .
• The only thing we care about for now is that all these elements to the right
of the wall are bigger than the pivot.
• No actual order is assumed yet.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
15
Quick Sort
How it works
Quick Sort
• Pivot is determined
• All elements less than PIVOTE in one subarray
• All elements more than PIVOTE in one subarray
• Recursively apply Quiksort to first subarray until
all elements sort
• Recursively apply Quiksort to the second
subarray until all elements sort
• Now the entire array/list is sorted
Prof. Lili Saghafi
proflilisaghafi@gmail.com
16
Analysis
• Each time we repeat the process on the left and right
sides of the array. we choose a pivot and do the
comparisons and create another level of left and right
subarrays.
• This recursive call will continue until we reach the end
when we've divided up the overall array into just
subarrays of length 1.
• From there, we know the array is sorted because every
element has, at some point, been a pivot.
• In other words, for every element, all the numbers to
the left are lesser values than PIVOT and all the
numbers to the right have greater values than PIVOT.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
17
Analysis continue…
• This method works very well, if the value of the pivot
chosen is approximately in the middle range of the
list values.
• This would mean that, after we move elements
around, there are about as many elements to the left
of the pivot as there are to the right.
• And the dividethe divide--andand--conquerconquer nature of the Quicksort
algorithm is then taken full advantage of it .
RUNTIME COMPLEXITY
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2)
• Average case: random arrays — Θ(n log n)
Considered the method of choice for internal sorting of
large files (n ≥ 10000)
Prof. Lili Saghafi
proflilisaghafi@gmail.com
18
Runtime
• This creates a runtime of O (n log n,) the n
because we have to do n minus 1 comparisons
on each generation, and log n because we
have to divide the list log n times.
• However, in the worst cases, this algorithm can
actually be O (n squared.)
• Suppose on each generation, the pivot just so
happens to be the smallest or the largest of
the numbers we're sorting.
• This would mean breaking down the list,
n times and making n minus 1 comparisons
every single time. Thus, Big O of n squared is
runtime.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
19
The importance of good Algorithms
Prof. Lili Saghafi
proflilisaghafi@gmail.com
20
Hoare’s Partitioning Algorithm
Prof. Lili Saghafi
proflilisaghafi@gmail.com
21
Pseudocode for Quicksort
Prof. Lili Saghafi
proflilisaghafi@gmail.com
22
22
Quick Sort
RunQuickSort
Prof. Lili Saghafi
proflilisaghafi@gmail.com
23
References
• Algorithms and Data Structures with implementations in Java and C++
• Introduction to Programming with C++ 3/E Y. Daniel Liang, Armstrong
State University
• C++ Without Fear: A Beginner's Guide That Makes You Feel Smart, 3/E
• Problem Solving and Programming Concepts, 9/E , Maureen Sprankle,
Jim Hubbard
• C++ Language http://www.cplusplus.com/doc/tutorial/
• Cormen, Leiserson, Rivest. Introduction to algorithms. (Theory)
• Aho, Ullman, Hopcroft. Data Structures and Algorithms. (Theory)
• Robert Lafore. Data Structures and Algorithms in Java. (Practice)
• Mark Allen Weiss. Data Structures and Problem Solving Using
C++. (Practice)
Prof. Lili Saghafi
proflilisaghafi@gmail.com
24
Sorting algorithms
ANY QUESTION ?
Algorithms and Data
Structures
Quick Sort
By : Prof. Lili Saghafi
proflilisaghafi@gmail.com @Lili_PLS
Prof. Lili Saghafi
proflilisaghafi@gmail.com
25
Quicksort
• Quicksort has worst case time complexity, a Big O of
N squared
• If a pivot is chosen properly it can be shown to have
an average case of ,Big O and n log N
Improvements:
1. better pivot selection: median of three partitioning
2. switch to insertion sort on small subfiles
3. elimination of recursion
These combine to 20-25% improvement
Prof. Lili Saghafi
proflilisaghafi@gmail.com
26
How can we improve the method?
• One good way to improve the method is to cut
down on the probability that the runtime is ever
actually O of n squared.
• Remember this worst worst case scenario can
only happen when the pivot chosen is always
the highest or lowest value in the array.
• To ensure this is less likely to happen, we can
find the pivot by choosing multiple elements and
taking the median value as we described
earlier.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
27
Quicksort
• Quicksort is a fast sorting algorithm,
which is used not only for educational
purposes, but widely applied in practice.
• On the average, it has O(n log n)
complexity, making quicksort suitable for
sorting big data volumes.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
28
Algorithm
• The idea of the algorithm is quite simple
• The divide-and-conquer strategy is used in quicksort.
Below the recursion step is described:
• Choose a pivot value. We take the value of the middle
element as pivot value, but it can be any value, which is
in range of sorted values, even if it doesn't present in the
array.
• Partition. Rearrange elements in such a way, that all
elements which are lesser than the pivot go to the left
part of the array and all elements greater than the pivot,
go to the right part of the array. Values equal to the pivot
can stay in any part of the array. Notice, that array may
be divided in non-equal parts.
• Sort both parts. Apply quicksort algorithm recursively to
the left and the right parts.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
29
Complexity analysis
• On the average quicksort has O(n log n)
complexity
• In worst case, quicksort runs O(n2) time
• On the most "practical" data it works just
fine and outperforms other O(n log n)
sorting algorithms.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
30
30
Worst-Case Time
1. To partition an array of n elements, it takes n
comparisons and n moves in the worst case.
2. In the worst case, each time the pivot divides
the array into one big subarray with the other
empty.
3. The size of the big subarray is one less than
the one before divided.
4. The algorithm requires time:
)(12...)2()1( 2
nOnn =+++−+−
Suppose on each generation, the pivot just so happens to be the smallest or the
largest of the numbers we're sorting.This would mean breaking down the list,
n times and making n minus 1 comparisons every single time. Thus, Big O of
n squared is runtime.
Prof. Lili Saghafi
proflilisaghafi@gmail.com
31
31
Best-Case Time
1. In the best case, each time the pivot
divides the array into two parts of about
the same size.
2. Let T(n) denote the time required for
sorting an array of elements using
quick sort.
3. So,
n
n
T
n
TnT ++= )
2
()
2
()(
Prof. Lili Saghafi
proflilisaghafi@gmail.com
32
32
Average-Case Time
1. On the average, each time the pivot will
not divide the array into two parts of the
same size nor one empty part.
2. Statistically, the sizes of the two parts
are very close.
3. So the average time is O(nlog n).
4. Be reminded that the exact average-case
analysis is beyond the scope of this
course. we have to do n minus 1 comparisons on each generation,
and log n because we have to divide the list log n times.

More Related Content

What's hot (20)

Sorting.ppt read only
Sorting.ppt read onlySorting.ppt read only
Sorting.ppt read only
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Presentation
PresentationPresentation
Presentation
 
Data Structure in Quantum Computing
Data Structure in Quantum ComputingData Structure in Quantum Computing
Data Structure in Quantum Computing
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Ijcse13 05-01-048
Ijcse13 05-01-048Ijcse13 05-01-048
Ijcse13 05-01-048
 
Me314 week 06-07-Time Response
Me314 week 06-07-Time ResponseMe314 week 06-07-Time Response
Me314 week 06-07-Time Response
 
Lec7
Lec7Lec7
Lec7
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
 
Quantum computer
Quantum computerQuantum computer
Quantum computer
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Quantum computing
Quantum computingQuantum computing
Quantum computing
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Quantum computer presentation
Quantum computer presentationQuantum computer presentation
Quantum computer presentation
 
Neural Networks - Types of Neurons
Neural Networks - Types of NeuronsNeural Networks - Types of Neurons
Neural Networks - Types of Neurons
 
Lecture01 algorithm analysis
Lecture01 algorithm analysisLecture01 algorithm analysis
Lecture01 algorithm analysis
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
September 30, Probabilistic Modeling
September 30, Probabilistic ModelingSeptember 30, Probabilistic Modeling
September 30, Probabilistic Modeling
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 

Similar to Quick Sort By Prof Lili Saghafi

Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhithRj Juhith
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsData Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsFerdin Joe John Joseph PhD
 
Searching Algorithm.pptx
Searching Algorithm.pptxSearching Algorithm.pptx
Searching Algorithm.pptxSUGUNA49
 
Apache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done betterApache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done better🧑‍💻 Manuel Coppotelli
 
Heuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligenceHeuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligencegrinu
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsAnushdika Jeganathan
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 

Similar to Quick Sort By Prof Lili Saghafi (20)

CPP12 - Algorithms
CPP12 - AlgorithmsCPP12 - Algorithms
CPP12 - Algorithms
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
SearchAlgorithm.pdf
SearchAlgorithm.pdfSearchAlgorithm.pdf
SearchAlgorithm.pdf
 
Rahat & juhith
Rahat & juhithRahat & juhith
Rahat & juhith
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Data Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search AlgorithmsData Structures and Algorithm - Week 9 - Search Algorithms
Data Structures and Algorithm - Week 9 - Search Algorithms
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 
Problem solving
Problem solvingProblem solving
Problem solving
 
Searching Algorithm.pptx
Searching Algorithm.pptxSearching Algorithm.pptx
Searching Algorithm.pptx
 
Apache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done betterApache Giraph: Large-scale graph processing done better
Apache Giraph: Large-scale graph processing done better
 
Heuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligenceHeuristic search-in-artificial-intelligence
Heuristic search-in-artificial-intelligence
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
daa unit 1.pptx
daa unit 1.pptxdaa unit 1.pptx
daa unit 1.pptx
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Algorithms 1
Algorithms 1Algorithms 1
Algorithms 1
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 

More from Professor Lili Saghafi

Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data,  By : Prof. Lili SaghafiArtificial Intelligence and the importance of Data,  By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data, By : Prof. Lili SaghafiProfessor Lili Saghafi
 
Introduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsIntroduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsProfessor Lili Saghafi
 
Software Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiSoftware Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiProfessor Lili Saghafi
 
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiQuantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiProfessor Lili Saghafi
 
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Professor Lili Saghafi
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiProfessor Lili Saghafi
 
Introduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiIntroduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiProfessor Lili Saghafi
 
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiCyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiProfessor Lili Saghafi
 
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Professor Lili Saghafi
 
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Professor Lili Saghafi
 
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiMachine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiProfessor Lili Saghafi
 
Machine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiMachine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiProfessor Lili Saghafi
 
What is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiWhat is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiProfessor Lili Saghafi
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiComputer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiProfessor Lili Saghafi
 
Data Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiData Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiProfessor Lili Saghafi
 
Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Professor Lili Saghafi
 
Data Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiData Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiProfessor Lili Saghafi
 

More from Professor Lili Saghafi (20)

Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data,  By : Prof. Lili SaghafiArtificial Intelligence and the importance of Data,  By : Prof. Lili Saghafi
Artificial Intelligence and the importance of Data, By : Prof. Lili Saghafi
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Ai
AiAi
Ai
 
Introduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: FundamentalsIntroduction to Quantum Computing Lecture 1: Fundamentals
Introduction to Quantum Computing Lecture 1: Fundamentals
 
Software Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili SaghafiSoftware Engineering_Agile Software Development By: Professor Lili Saghafi
Software Engineering_Agile Software Development By: Professor Lili Saghafi
 
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili SaghafiQuantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
Quantum Computing Quantum Internet 2020_unit 1 By: Prof. Lili Saghafi
 
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
Programming Languages Categories / Programming Paradigm By: Prof. Lili Saghafi
 
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili SaghafiIntroduction to blockchain lesson 2 By: Professor Lili Saghafi
Introduction to blockchain lesson 2 By: Professor Lili Saghafi
 
Introduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili SaghafiIntroduction to Blockchain Technology By Professor Lili Saghafi
Introduction to Blockchain Technology By Professor Lili Saghafi
 
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili SaghafiCyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
Cyber Security and Post Quantum Cryptography By: Professor Lili Saghafi
 
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
Machine learning by using python lesson 3 Confusion Matrix By : Professor Lil...
 
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
Machine learning by using python lesson 2 Neural Networks By Professor Lili S...
 
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili SaghafiMachine learning by using python Lesson One Part 2 By Professor Lili Saghafi
Machine learning by using python Lesson One Part 2 By Professor Lili Saghafi
 
Machine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili SaghafiMachine learning by using python By: Professor Lili Saghafi
Machine learning by using python By: Professor Lili Saghafi
 
What is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili SaghafiWhat is digital humanities ,By: Professor Lili Saghafi
What is digital humanities ,By: Professor Lili Saghafi
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili SaghafiComputer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
Computer Security Cyber Security DOS_DDOS Attacks By: Professor Lili Saghafi
 
Data Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili SaghafiData Science unit 2 By: Professor Lili Saghafi
Data Science unit 2 By: Professor Lili Saghafi
 
Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi Data science unit 1 By: Professor Lili Saghafi
Data science unit 1 By: Professor Lili Saghafi
 
Data Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili SaghafiData Scientist By: Professor Lili Saghafi
Data Scientist By: Professor Lili Saghafi
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

Quick Sort By Prof Lili Saghafi

  • 1. Prof. Lili Saghafi proflilisaghafi@gmail.com 1 Sorting Algorithms Algorithms and Data Structures Quick Sort By : Prof. Lili Saghafi proflilisaghafi@gmail.com @Lili_PLS
  • 2. Prof. Lili Saghafi proflilisaghafi@gmail.com 2 2 Objectives –To study and analyze time complexity of various sorting algorithms –To design, implement, and analyze selection sort, insertion sort –To design, implement, and analyze bubble sort To design, implement, and analyze merge sort –– To design, implement, and analyzeTo design, implement, and analyze quick sortquick sort
  • 3. Prof. Lili Saghafi proflilisaghafi@gmail.com 3 3 Why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithmssorting algorithms. – First, sorting algorithms illustrate many creative approaches to problem solving – Second, sorting algorithms are good for practicing fundamental programming techniques using selection statements, loops, methods, and arrays. – Third, sorting algorithms are excellent examples to demonstrate algorithm performance.
  • 4. Prof. Lili Saghafi proflilisaghafi@gmail.com 4 4 What data to sort? The data to be sorted might be integers, doubles, characters, or objects. For “Sorting Arrays,” for simplicity lets assume: • data to be sorted are integers, • data are sorted in ascending order, and • data are stored in an array. • The programs can be easily modified to sort other types of data, to sort in descending order, or to sort data in an ArrayList or a LinkedList.
  • 5. Prof. Lili Saghafi proflilisaghafi@gmail.com 5 5 Sorting Arrays • Sorting, like searching, is also a common task in computer programming. • Sorting would be used, for instance, if you wanted to display the grades in alphabetical order. • Many different algorithms have been developed for sorting. • Selection Sort | Insertion Sort | Bubble Sort | Radix Sort | Merge Sort | Merge two sorted lists | QuickQuick SortSort | Partition in quick sort • Quicksort is a little more complex than selection and bubble • Best choice for general purpose and in memory sorting • Used to be the standard algorithm for sorting of arrays of primitive types in Java
  • 6. Prof. Lili Saghafi proflilisaghafi@gmail.com 6 6 Quick Sort •Quick sort, developed by C. A. R. Hoare (1962), works as follows: – The algorithm selects an element, called the pivot, in the array. – Divide the array into two parts such that all the elements in the first part are less than or equal to the pivot and all the elements in the second part are greater than the pivot. – Recursively apply the quick sort algorithm to the first part and then the second part. Sir Charles Antony Richard Hoare 1980 Turing Award
  • 7. Prof. Lili Saghafi proflilisaghafi@gmail.com 7 Quick Sort • Quicksort honoured as one of top 10 algorithms of 20th century in science and engineering. Basic plan. • Shuffle the array. • Partition so that, for some j – entry a[j] is in place – no larger entry to the left of j – no smaller entry to the right of j – Sort each piece recursively.
  • 8. Prof. Lili Saghafi proflilisaghafi@gmail.com 8 Important factor in Quick Sort is Pivot PIVOT needs to have following condition after we sort it
  • 9. Prof. Lili Saghafi proflilisaghafi@gmail.com 9 What we need to choose in next step We need to swap these two Now we need to repeat , choose item from left and item from right and if needed swap the two. Quicksort is Recursive sort
  • 10. Prof. Lili Saghafi proflilisaghafi@gmail.com 10 • We repeat until we see that item from left has a greater index than item from right, so we know we're done .( LOW to HIGH , If HIGH is less than LOW and pivot ) • we STOP and swap item from left ( HIGH) with our pivot 3 , our pivot is now in its correct spot We swap item from left with are PIVOT
  • 11. Prof. Lili Saghafi proflilisaghafi@gmail.com 11 Characteristic of PIVOT Quicksort is Recursive , we choose larger partition We choose 7 as are pivot and move it to the end So we swap 8 & 6 then we swap 8 and pivot Now we have our PIVOT 7 in the right place We leave Recursion handle the rest We move pivot to the end STOP When item from left has a greater index than item from right
  • 12. Prof. Lili Saghafi proflilisaghafi@gmail.com 12 how do we choose the pivot Choose the median of the array •One popular method is called median of three •In this method we look at the first middle and last elements of the array we sort them properly and choose the middle item as our pivot •We're making the guess that the middle of these three items could be close to the median of the entire array and as you can see it's not too far off
  • 13. Prof. Lili Saghafi proflilisaghafi@gmail.com 13 Why does it work? • On the partition step, algorithm divides thealgorithm divides the array into two partsarray into two parts and every element a from the left part is less or equal than every element b from the right part. • Also a and b satisfy a ≤ pivot ≤ b inequality. • After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above, the whole array is sorted.
  • 14. Prof. Lili Saghafi proflilisaghafi@gmail.com 14 Quick Sort Partition How it works (Quick Sort Partition ) • In the beginning , First element as PIVOT • Element after PIVOT as LOW the last element as HIGH • Is LOW less than PIVOT ? Yes , Then move LOW to next element – If NOT then we move HIGH one to the right since list[high] > pivot, high was moved backward. – Is LOW greater than PIVOT ? Yes , – and IF HIGH is lower than PIVOT then we swap the low and high – If NOT then we move HIGH one to the right since list[high] > pivot, high was moved backward. – REPEAT until LOW and HIGH are equal . – If it is MORE than PIVOT then we move high to the right. – If it is LESS than Pivot then we swap high and pivot – Now the partition is complete and PIVOTE makes the array into two sub arrays for sorting . • The only thing we care about for now is that all these elements to the right of the wall are bigger than the pivot. • No actual order is assumed yet.
  • 15. Prof. Lili Saghafi proflilisaghafi@gmail.com 15 Quick Sort How it works Quick Sort • Pivot is determined • All elements less than PIVOTE in one subarray • All elements more than PIVOTE in one subarray • Recursively apply Quiksort to first subarray until all elements sort • Recursively apply Quiksort to the second subarray until all elements sort • Now the entire array/list is sorted
  • 16. Prof. Lili Saghafi proflilisaghafi@gmail.com 16 Analysis • Each time we repeat the process on the left and right sides of the array. we choose a pivot and do the comparisons and create another level of left and right subarrays. • This recursive call will continue until we reach the end when we've divided up the overall array into just subarrays of length 1. • From there, we know the array is sorted because every element has, at some point, been a pivot. • In other words, for every element, all the numbers to the left are lesser values than PIVOT and all the numbers to the right have greater values than PIVOT.
  • 17. Prof. Lili Saghafi proflilisaghafi@gmail.com 17 Analysis continue… • This method works very well, if the value of the pivot chosen is approximately in the middle range of the list values. • This would mean that, after we move elements around, there are about as many elements to the left of the pivot as there are to the right. • And the dividethe divide--andand--conquerconquer nature of the Quicksort algorithm is then taken full advantage of it . RUNTIME COMPLEXITY • Best case: split in the middle — Θ(n log n) • Worst case: sorted array! — Θ(n2) • Average case: random arrays — Θ(n log n) Considered the method of choice for internal sorting of large files (n ≥ 10000)
  • 18. Prof. Lili Saghafi proflilisaghafi@gmail.com 18 Runtime • This creates a runtime of O (n log n,) the n because we have to do n minus 1 comparisons on each generation, and log n because we have to divide the list log n times. • However, in the worst cases, this algorithm can actually be O (n squared.) • Suppose on each generation, the pivot just so happens to be the smallest or the largest of the numbers we're sorting. • This would mean breaking down the list, n times and making n minus 1 comparisons every single time. Thus, Big O of n squared is runtime.
  • 23. Prof. Lili Saghafi proflilisaghafi@gmail.com 23 References • Algorithms and Data Structures with implementations in Java and C++ • Introduction to Programming with C++ 3/E Y. Daniel Liang, Armstrong State University • C++ Without Fear: A Beginner's Guide That Makes You Feel Smart, 3/E • Problem Solving and Programming Concepts, 9/E , Maureen Sprankle, Jim Hubbard • C++ Language http://www.cplusplus.com/doc/tutorial/ • Cormen, Leiserson, Rivest. Introduction to algorithms. (Theory) • Aho, Ullman, Hopcroft. Data Structures and Algorithms. (Theory) • Robert Lafore. Data Structures and Algorithms in Java. (Practice) • Mark Allen Weiss. Data Structures and Problem Solving Using C++. (Practice)
  • 24. Prof. Lili Saghafi proflilisaghafi@gmail.com 24 Sorting algorithms ANY QUESTION ? Algorithms and Data Structures Quick Sort By : Prof. Lili Saghafi proflilisaghafi@gmail.com @Lili_PLS
  • 25. Prof. Lili Saghafi proflilisaghafi@gmail.com 25 Quicksort • Quicksort has worst case time complexity, a Big O of N squared • If a pivot is chosen properly it can be shown to have an average case of ,Big O and n log N Improvements: 1. better pivot selection: median of three partitioning 2. switch to insertion sort on small subfiles 3. elimination of recursion These combine to 20-25% improvement
  • 26. Prof. Lili Saghafi proflilisaghafi@gmail.com 26 How can we improve the method? • One good way to improve the method is to cut down on the probability that the runtime is ever actually O of n squared. • Remember this worst worst case scenario can only happen when the pivot chosen is always the highest or lowest value in the array. • To ensure this is less likely to happen, we can find the pivot by choosing multiple elements and taking the median value as we described earlier.
  • 27. Prof. Lili Saghafi proflilisaghafi@gmail.com 27 Quicksort • Quicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. • On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes.
  • 28. Prof. Lili Saghafi proflilisaghafi@gmail.com 28 Algorithm • The idea of the algorithm is quite simple • The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: • Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array. • Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts. • Sort both parts. Apply quicksort algorithm recursively to the left and the right parts.
  • 29. Prof. Lili Saghafi proflilisaghafi@gmail.com 29 Complexity analysis • On the average quicksort has O(n log n) complexity • In worst case, quicksort runs O(n2) time • On the most "practical" data it works just fine and outperforms other O(n log n) sorting algorithms.
  • 30. Prof. Lili Saghafi proflilisaghafi@gmail.com 30 30 Worst-Case Time 1. To partition an array of n elements, it takes n comparisons and n moves in the worst case. 2. In the worst case, each time the pivot divides the array into one big subarray with the other empty. 3. The size of the big subarray is one less than the one before divided. 4. The algorithm requires time: )(12...)2()1( 2 nOnn =+++−+− Suppose on each generation, the pivot just so happens to be the smallest or the largest of the numbers we're sorting.This would mean breaking down the list, n times and making n minus 1 comparisons every single time. Thus, Big O of n squared is runtime.
  • 31. Prof. Lili Saghafi proflilisaghafi@gmail.com 31 31 Best-Case Time 1. In the best case, each time the pivot divides the array into two parts of about the same size. 2. Let T(n) denote the time required for sorting an array of elements using quick sort. 3. So, n n T n TnT ++= ) 2 () 2 ()(
  • 32. Prof. Lili Saghafi proflilisaghafi@gmail.com 32 32 Average-Case Time 1. On the average, each time the pivot will not divide the array into two parts of the same size nor one empty part. 2. Statistically, the sizes of the two parts are very close. 3. So the average time is O(nlog n). 4. Be reminded that the exact average-case analysis is beyond the scope of this course. we have to do n minus 1 comparisons on each generation, and log n because we have to divide the list log n times.