SlideShare a Scribd company logo
1 of 47
1
Introduction to Sorting
 Sorting is the fundamental algorithmic problem in
mathematics and computer science.
 It puts elements in a certain order. The most
commonly used orders are numerical and
lexicographical(alphabetical) order.
 Efficient sorting is important to optimize the use
of other algorithms, as it is the first step in most
of them.
 There are many sorting algorithms, but knowing
which one to use depends on the specific
problem at hand.
2
Classification of Sorting algorithms
Sorting algorithms are often classified using different metrics:
 Computational complexity: classification is based on worst, average and best
behavior of sorting a list of size (n).
 For typical sorting algorithms acceptable/good behavior is O(n log n) and
unacceptable/bad behavior is Ω(n^2).
 Ideal behavior for a sort is O(n).
 Memory usage (and use of other computer resources):
 Some sorting algorithms are “in place", such that only O(1) or O(log n) memory is
needed beyond the items being sorted.
 Others need to create auxiliary data structures for data to be temporarily stored.
We’ve seen in class that mergesort needs more memory resources as it is not an
“in place” algorithm, while quicksort and heapsort are “in place”. Radix and
bucket sorts are not “in place”.
 Recursion: some algorithms are either recursive or non-recursive.(e.g., mergesort is
recursive).
 Stability: stable sorting algorithms maintain the relative order of elements/records
with equal keys/values. Radix and bucket sorts are stable.
 General method: classification is based on how sort functions internally.
 Methods used internally include insertion, exchange, selection, merging,
distribution etc. Bubble sort and quicksort are exchange sorts. Heapsort is a
selection sort.
3
Classification of Sorting algorithms…contd
 Comparison sorts: A comparison sort examines elements with a comparison
operator, which usually is the less than or equal to operator(≤). Comparison sorts
include:
 Bubble sort
 Insertion sort
 Selection sort
 Shell sort
 Heapsort
 Mergesort
 Quicksort.
 Non-Comparison sorts: these use other techniques to sort data, rather than using
comparison operations. These include:
 Radix sort (examines individual bits of keys)
 Bucket sort (examines bits of keys)
 Counting sort (indexes using key values)
4
5
Divide and Conquer
1.Base Case, solve the problem
directly if it is small enough
2.Divide the problem into two or
more similar and smaller
subproblems
3.Recursively solve the subproblems
4.Combine solutions to the
subproblems
Quick sort
 A key step in the Quicksort algorithm is
partitioning the array
 We choose some (any) number p in the
array to use as a pivot
 We partition the array into three parts:
p
numbers
less than
p
numbers greater
than or equal to
p
p
Quick Sort(Partitioning )
 Choose an array value (say, the first) to
use as the pivot
 Starting from the left end, find the first
element that is greater than or equal to
the pivot
 Searching backward from the right end,
find the first element that is less than
the pivot
 Interchange (swap) these two elements
 Repeat, searching from where we left
off, until done
Example of partitioning
 choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
 search: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
 swap: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
 search: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
 swap: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
 search: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
 swap: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6
 search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6
(left > right)
 swap with pivot:1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
Quick sort
 To sort a[left...right]:
1. if left < right:
1.1. Partition a[left...right] such that:
all a[left...p-1] are less than a[p], and
all a[p+1...right] are >= a[p]
1.2. Quicksort a[left...p-1]
1.3. Quicksort a[p+1...right]
2. Terminate
Partitioning
 To partition a[left...right]:
1. Set p = a[left], l = left + 1, r = right;
2. while l < r, do
2.1. while l < right && a[l] < p { l = l + 1 }
2.2. while r > left && a[r] >= p { r = r – 1}
2.3. if l < r { swap a[l] and a[r] }
3. a[left] = a[r]; a[r] = p;
4. Terminate
11
12
Merge Sort: Idea
Merge
Recursively
sort
Divide into
two halves
FirstPart SecondPart
FirstPart SecondPart
A
A is sorted!
13
A[middle]A[middle]A[left]A[left]
SortedSorted
FirstPartFirstPart
SortedSorted
SecondPartSecondPart
Merge-Sort: Merge
A[right]A[right]
mergemerge
A:A:
A:A:
SortedSorted
14
6 10 14 223 5 15 28
L:L: R:R:
Temporary ArraysTemporary Arrays
5 15 28 30 6 10 145
Merge-Sort: Merge Example
2 3 7 8 1 4 5 6A:A:
15
Merge-Sort: Merge Example
3 5 15 28 30 6 10 14
L:L:
A:A:
3 15 28 30 6 10 14 22
R:R:
i=
0
j=0
k=0
2 3 7 8 1 4 5 6
1
16
Merge-Sort: Merge Example
1 5 15 28 30 6 10 14
L:L:
A:A:
3 5 15 28 6 10 14 22
R:R:
k=1
2 3 7 8 1 4 5 6
2
i=
0
j=1
17
Merge-Sort: Merge Example
1 2 15 28 30 6 10 14
L:L:
A:A:
6 10 14 22
R:R:
i=
1
k=2
2 3 7 8 1 4 5 6
3
j=1
18
Merge-Sort: Merge Example
1 2 3 6 10 14
L:L:
A:A:
6 10 14 22
R:R:
i=
2
j=1
k=3
2 3 7 8 1 4 5 6
4
19
Merge-Sort: Merge Example
1 2 3 4 6 10 14
L:L:
A:A:
6 10 14 22
R:R:
j=2
k=4
2 3 7 8 1 4 5 6
i=
2
5
20
Merge-Sort: Merge Example
1 2 3 4 5 6 10 14
L:L:
A:A:
6 10 14 22
R:R:
i=
2
j=3
k=5
2 3 7 8 1 4 5 6
6
21
Merge-Sort: Merge Example
1 2 3 4 5 6 14
L:L:
A:A:
6 10 14 22
R:R:
k=6
2 3 7 8 1 4 5 6
7
i=
2
j=4
22
Merge-Sort: Merge Example
1 2 3 4 5 6 7 14
L:L:
A:A:
3 5 15 28 6 10 14 22
R:R:
2 3 7 8 1 4 5 6
8
i=
3
j=4
k=7
23
Merge-Sort: Merge Example
1 2 3 4 5 6 7 8
L:L:
A:A:
3 5 15 28 6 10 14 22
R:R:
2 3 7 8 1 4 5 6
i=
4
j=4
k=8
24
6 2 8 4 3 7 5 16 2 8 4 3 7 5 1
Merge-Sort(A, 0, 7)
Divide
A:
25
6 2 8 4
3 7 5 1
6 2 8 4
Merge-Sort(A, 0,
3)
, divide
A:
Merge-Sort(A, 0, 7)
26
3 7 5 1
8 4
6 26 2
Merge-Sort(A, 0,
1)
, divide
A:
Merge-Sort(A, 0, 7)
27
3 7 5 1
8 4
6
2
Merge-Sort(A, 0, 0), base case
A:
Merge-Sort(A, 0, 7)
28
3 7 5 1
8 4
6 2
Merge-Sort(A, 0, 0), return
A:
Merge-Sort(A, 0, 7)
29
3 7 5 1
8 4
6
2
Merge-Sort(A, 1,
1)
, base case
A:
Merge-Sort(A, 0, 7)
30
3 7 5 1
8 4
6 2
Merge-Sort(A, 1, 1), return
A:
Merge-Sort(A, 0, 7)
31
3 7 5 1
8 4
2 6
Merge(A, 0, 0, 1)
A:
Merge-Sort(A, 0, 7)
32
3 7 5 1
8 42 6
Merge-Sort(A, 0, 1), return
A:
Merge-Sort(A, 0, 7)
33
3 7 5 1
8 4
2 6
Merge-Sort(A, 2,
3)
48
, divide
A:
Merge-Sort(A, 0, 7)
34
3 7 5 1
4
2 6
8
Merge-Sort(A, 2, 2), base case
A:
Merge-Sort(A, 0, 7)
35
3 7 5 1
4
2 6
8
Merge-Sort(A, 2, 2), return
A:
Merge-Sort(A, 0, 7)
36
4
2 6
8
Merge-Sort(A, 3, 3), base case
A:
Merge-Sort(A, 0, 7)
37
3 7 5 1
4
2 6
8
Merge-Sort(A, 3, 3),
returnA:
Merge-Sort(A, 0, 7)
38
3 7 5 1
2 6
4 8
Merge(A, 2, 2, 3)
A:
Merge-Sort(A, 0, 7)
39
3 7 5 1
2 6 4 8
Merge-Sort(A, 2, 3),
returnA:
Merge-Sort(A, 0, 7)
40
3 7 5 1
2 4 6 8
Merge(A, 0, 1, 3)
A:
Merge-Sort(A, 0, 7)
41
3 7 5 12 4 6 8
Merge-Sort(A, 0, 3),
returnA:
Merge-Sort(A, 0, 7)
42
3 7 5 1
2 4 6 8
Merge-Sort(A, 4, 7)
A:
Merge-Sort(A, 0, 7)
43
1 3 5 7
2 4 6 8A:
Merge (A, 4, 5, 7)
Merge-Sort(A, 0, 7)
44
1 3 5 72 4 6 8
Merge-Sort(A, 4, 7),
returnA:
Merge-Sort(A, 0, 7)
45
1 2 3 4 5 6 7 8
Merge(A, 0, 3, 7)
A:
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 7),
done!
46
Merge Sort: Algorithm
Merge-Sort (A, left, right)
if left ≥ right return
else
middle ←(left + right)/2
Merge-Sort(A, left, middle)
Merge-Sort(A, middle+1, right)
Merge(A, left, middle, right)
Recursive Call
47
Merge(A, left, middle, right)
1. n1 ← middle – left + 1
2. n2 ← right – middle
3. create array L[n1], R[n2]
4. for i ← 0 to n1-1 do L[i] ← A[left +i]
5. for j ← 0 to n2-1 do R[j] ← A[middle+j]
6. k ← i ← j ← 0
7. while i < n1 & j < n2
8. if L[i] < R[j]
9. A[k++] ← L[i++]
10. else
11. A[k++] ← R[j++]
12. while i < n1
13. A[k++] ← L[i++]
14. while j < n2
15. A[k++] ← R[j++]
n = n1+n2
Space: n
Time : cn for some constant c

More Related Content

What's hot

358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14sumitbardhan
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhithRj Juhith
 
Divide-and-conquer
Divide-and-conquerDivide-and-conquer
Divide-and-conquerMrunal Patil
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)Arvind Devaraj
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Typesamberkhan59
 
Selection sort
Selection sortSelection sort
Selection sortamna izzat
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...ijfls
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1asmhemu
 
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...Wireilla
 

What's hot (20)

358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Sorting
SortingSorting
Sorting
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Rahat &amp; juhith
Rahat &amp; juhithRahat &amp; juhith
Rahat &amp; juhith
 
Divide-and-conquer
Divide-and-conquerDivide-and-conquer
Divide-and-conquer
 
Sorting (introduction)
 Sorting (introduction) Sorting (introduction)
Sorting (introduction)
 
Searching algorithm
Searching algorithmSearching algorithm
Searching algorithm
 
Sorting and Its Types
Sorting and Its TypesSorting and Its Types
Sorting and Its Types
 
Selection sort
Selection sortSelection sort
Selection sort
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
 
Presentation Of Analysis
Presentation Of Analysis  Presentation Of Analysis
Presentation Of Analysis
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
STATISTICAL ANALYSIS OF FUZZY LINEAR REGRESSION MODEL BASED ON DIFFERENT DIST...
 

Similar to Sorting

Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problemsSumita Das
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1subhashchandra197
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxLakshmiSamivel
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Merge sort
Merge sortMerge sort
Merge sortKumar
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Flynce Miguel
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxnikshaikh786
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesortecomputernotes
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 

Similar to Sorting (20)

Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Presentation on binary search, quick sort, merge sort and problems
Presentation on binary search, quick sort, merge sort  and problemsPresentation on binary search, quick sort, merge sort  and problems
Presentation on binary search, quick sort, merge sort and problems
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptxDIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
DIVIDE AND CONQUERMETHOD IN DATASTRUCTURE.pptx
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Merge sort
Merge sortMerge sort
Merge sort
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Computer notes - Mergesort
Computer notes - MergesortComputer notes - Mergesort
Computer notes - Mergesort
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 

More from invertis university (10)

Data link control notes
Data link control notesData link control notes
Data link control notes
 
Tree
TreeTree
Tree
 
Program listds
Program listdsProgram listds
Program listds
 
Heaps
HeapsHeaps
Heaps
 
Hashing
HashingHashing
Hashing
 
Dsa book
Dsa bookDsa book
Dsa book
 
B trees
B treesB trees
B trees
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
System security
System securitySystem security
System security
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
_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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
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
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 

Recently uploaded (20)

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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
 
_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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
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
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
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
 
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 🔝✔️✔️
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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
 

Sorting

  • 1. 1 Introduction to Sorting  Sorting is the fundamental algorithmic problem in mathematics and computer science.  It puts elements in a certain order. The most commonly used orders are numerical and lexicographical(alphabetical) order.  Efficient sorting is important to optimize the use of other algorithms, as it is the first step in most of them.  There are many sorting algorithms, but knowing which one to use depends on the specific problem at hand.
  • 2. 2 Classification of Sorting algorithms Sorting algorithms are often classified using different metrics:  Computational complexity: classification is based on worst, average and best behavior of sorting a list of size (n).  For typical sorting algorithms acceptable/good behavior is O(n log n) and unacceptable/bad behavior is Ω(n^2).  Ideal behavior for a sort is O(n).  Memory usage (and use of other computer resources):  Some sorting algorithms are “in place", such that only O(1) or O(log n) memory is needed beyond the items being sorted.  Others need to create auxiliary data structures for data to be temporarily stored. We’ve seen in class that mergesort needs more memory resources as it is not an “in place” algorithm, while quicksort and heapsort are “in place”. Radix and bucket sorts are not “in place”.  Recursion: some algorithms are either recursive or non-recursive.(e.g., mergesort is recursive).  Stability: stable sorting algorithms maintain the relative order of elements/records with equal keys/values. Radix and bucket sorts are stable.  General method: classification is based on how sort functions internally.  Methods used internally include insertion, exchange, selection, merging, distribution etc. Bubble sort and quicksort are exchange sorts. Heapsort is a selection sort.
  • 3. 3 Classification of Sorting algorithms…contd  Comparison sorts: A comparison sort examines elements with a comparison operator, which usually is the less than or equal to operator(≤). Comparison sorts include:  Bubble sort  Insertion sort  Selection sort  Shell sort  Heapsort  Mergesort  Quicksort.  Non-Comparison sorts: these use other techniques to sort data, rather than using comparison operations. These include:  Radix sort (examines individual bits of keys)  Bucket sort (examines bits of keys)  Counting sort (indexes using key values)
  • 4. 4
  • 5. 5 Divide and Conquer 1.Base Case, solve the problem directly if it is small enough 2.Divide the problem into two or more similar and smaller subproblems 3.Recursively solve the subproblems 4.Combine solutions to the subproblems
  • 6. Quick sort  A key step in the Quicksort algorithm is partitioning the array  We choose some (any) number p in the array to use as a pivot  We partition the array into three parts: p numbers less than p numbers greater than or equal to p p
  • 7. Quick Sort(Partitioning )  Choose an array value (say, the first) to use as the pivot  Starting from the left end, find the first element that is greater than or equal to the pivot  Searching backward from the right end, find the first element that is less than the pivot  Interchange (swap) these two elements  Repeat, searching from where we left off, until done
  • 8. Example of partitioning  choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6  search: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6  swap: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6  search: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6  swap: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6  search: 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6  swap: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6  search: 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (left > right)  swap with pivot:1 3 3 1 2 2 3 4 4 9 8 9 6 5 6
  • 9. Quick sort  To sort a[left...right]: 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right] 2. Terminate
  • 10. Partitioning  To partition a[left...right]: 1. Set p = a[left], l = left + 1, r = right; 2. while l < r, do 2.1. while l < right && a[l] < p { l = l + 1 } 2.2. while r > left && a[r] >= p { r = r – 1} 2.3. if l < r { swap a[l] and a[r] } 3. a[left] = a[r]; a[r] = p; 4. Terminate
  • 11. 11
  • 12. 12 Merge Sort: Idea Merge Recursively sort Divide into two halves FirstPart SecondPart FirstPart SecondPart A A is sorted!
  • 14. 14 6 10 14 223 5 15 28 L:L: R:R: Temporary ArraysTemporary Arrays 5 15 28 30 6 10 145 Merge-Sort: Merge Example 2 3 7 8 1 4 5 6A:A:
  • 15. 15 Merge-Sort: Merge Example 3 5 15 28 30 6 10 14 L:L: A:A: 3 15 28 30 6 10 14 22 R:R: i= 0 j=0 k=0 2 3 7 8 1 4 5 6 1
  • 16. 16 Merge-Sort: Merge Example 1 5 15 28 30 6 10 14 L:L: A:A: 3 5 15 28 6 10 14 22 R:R: k=1 2 3 7 8 1 4 5 6 2 i= 0 j=1
  • 17. 17 Merge-Sort: Merge Example 1 2 15 28 30 6 10 14 L:L: A:A: 6 10 14 22 R:R: i= 1 k=2 2 3 7 8 1 4 5 6 3 j=1
  • 18. 18 Merge-Sort: Merge Example 1 2 3 6 10 14 L:L: A:A: 6 10 14 22 R:R: i= 2 j=1 k=3 2 3 7 8 1 4 5 6 4
  • 19. 19 Merge-Sort: Merge Example 1 2 3 4 6 10 14 L:L: A:A: 6 10 14 22 R:R: j=2 k=4 2 3 7 8 1 4 5 6 i= 2 5
  • 20. 20 Merge-Sort: Merge Example 1 2 3 4 5 6 10 14 L:L: A:A: 6 10 14 22 R:R: i= 2 j=3 k=5 2 3 7 8 1 4 5 6 6
  • 21. 21 Merge-Sort: Merge Example 1 2 3 4 5 6 14 L:L: A:A: 6 10 14 22 R:R: k=6 2 3 7 8 1 4 5 6 7 i= 2 j=4
  • 22. 22 Merge-Sort: Merge Example 1 2 3 4 5 6 7 14 L:L: A:A: 3 5 15 28 6 10 14 22 R:R: 2 3 7 8 1 4 5 6 8 i= 3 j=4 k=7
  • 23. 23 Merge-Sort: Merge Example 1 2 3 4 5 6 7 8 L:L: A:A: 3 5 15 28 6 10 14 22 R:R: 2 3 7 8 1 4 5 6 i= 4 j=4 k=8
  • 24. 24 6 2 8 4 3 7 5 16 2 8 4 3 7 5 1 Merge-Sort(A, 0, 7) Divide A:
  • 25. 25 6 2 8 4 3 7 5 1 6 2 8 4 Merge-Sort(A, 0, 3) , divide A: Merge-Sort(A, 0, 7)
  • 26. 26 3 7 5 1 8 4 6 26 2 Merge-Sort(A, 0, 1) , divide A: Merge-Sort(A, 0, 7)
  • 27. 27 3 7 5 1 8 4 6 2 Merge-Sort(A, 0, 0), base case A: Merge-Sort(A, 0, 7)
  • 28. 28 3 7 5 1 8 4 6 2 Merge-Sort(A, 0, 0), return A: Merge-Sort(A, 0, 7)
  • 29. 29 3 7 5 1 8 4 6 2 Merge-Sort(A, 1, 1) , base case A: Merge-Sort(A, 0, 7)
  • 30. 30 3 7 5 1 8 4 6 2 Merge-Sort(A, 1, 1), return A: Merge-Sort(A, 0, 7)
  • 31. 31 3 7 5 1 8 4 2 6 Merge(A, 0, 0, 1) A: Merge-Sort(A, 0, 7)
  • 32. 32 3 7 5 1 8 42 6 Merge-Sort(A, 0, 1), return A: Merge-Sort(A, 0, 7)
  • 33. 33 3 7 5 1 8 4 2 6 Merge-Sort(A, 2, 3) 48 , divide A: Merge-Sort(A, 0, 7)
  • 34. 34 3 7 5 1 4 2 6 8 Merge-Sort(A, 2, 2), base case A: Merge-Sort(A, 0, 7)
  • 35. 35 3 7 5 1 4 2 6 8 Merge-Sort(A, 2, 2), return A: Merge-Sort(A, 0, 7)
  • 36. 36 4 2 6 8 Merge-Sort(A, 3, 3), base case A: Merge-Sort(A, 0, 7)
  • 37. 37 3 7 5 1 4 2 6 8 Merge-Sort(A, 3, 3), returnA: Merge-Sort(A, 0, 7)
  • 38. 38 3 7 5 1 2 6 4 8 Merge(A, 2, 2, 3) A: Merge-Sort(A, 0, 7)
  • 39. 39 3 7 5 1 2 6 4 8 Merge-Sort(A, 2, 3), returnA: Merge-Sort(A, 0, 7)
  • 40. 40 3 7 5 1 2 4 6 8 Merge(A, 0, 1, 3) A: Merge-Sort(A, 0, 7)
  • 41. 41 3 7 5 12 4 6 8 Merge-Sort(A, 0, 3), returnA: Merge-Sort(A, 0, 7)
  • 42. 42 3 7 5 1 2 4 6 8 Merge-Sort(A, 4, 7) A: Merge-Sort(A, 0, 7)
  • 43. 43 1 3 5 7 2 4 6 8A: Merge (A, 4, 5, 7) Merge-Sort(A, 0, 7)
  • 44. 44 1 3 5 72 4 6 8 Merge-Sort(A, 4, 7), returnA: Merge-Sort(A, 0, 7)
  • 45. 45 1 2 3 4 5 6 7 8 Merge(A, 0, 3, 7) A: Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 7), done!
  • 46. 46 Merge Sort: Algorithm Merge-Sort (A, left, right) if left ≥ right return else middle ←(left + right)/2 Merge-Sort(A, left, middle) Merge-Sort(A, middle+1, right) Merge(A, left, middle, right) Recursive Call
  • 47. 47 Merge(A, left, middle, right) 1. n1 ← middle – left + 1 2. n2 ← right – middle 3. create array L[n1], R[n2] 4. for i ← 0 to n1-1 do L[i] ← A[left +i] 5. for j ← 0 to n2-1 do R[j] ← A[middle+j] 6. k ← i ← j ← 0 7. while i < n1 & j < n2 8. if L[i] < R[j] 9. A[k++] ← L[i++] 10. else 11. A[k++] ← R[j++] 12. while i < n1 13. A[k++] ← L[i++] 14. while j < n2 15. A[k++] ← R[j++] n = n1+n2 Space: n Time : cn for some constant c

Editor's Notes

  1. Divide: if the initial array A has at least two elements (nothing needs to be done if A has zero or one elements), divide A into two subarrays each containing about half of the elements of A. Conquer: sort the two subarrays using Merge Sort. Combine: merging the two sorted subarray into one sorted array