SlideShare a Scribd company logo
1 of 40
Sorting
-ByVarsha kumari
GLA University - CSE 4070
Sorting : Outline
Bubble Sort
Insertion Sort
Selection Sort
Quick Sort
Two-way Merge sort
Heap Sort
GLA University - CSE 4070
Bubble Sort
It sort the array elements by repeatedly moving
the largest element to the highest index position
of the array[for ascending order].
Consecutive adjacent pair of elements in the
array are compared with each other.
Two elements are interchanged if lower index
value is greater than upper index value.
Process is continued till list of unsorted elements
exhausted.
It is called bubble sort because larger elements
‘bubble’ to the end of the list.
GLA University - CSE 4070
Algorithm
Bubble_sort(a[ ], n)
{
Repeat for i = 0 to n-1
Repeat for j = 0 to n-i-2
if(a[j]>a[j+1])
swap a[j] and a[j+1]
end if
end for
end for
}
GLA University - CSE 4070
Bubble Sort : 1st
pass
30 < 52 no swapping
52 >25 swap 52 and 25
52 < 84 no swapping
84 > 65 swap 84 and 65
Max value 84 moved to
last index
GLA University - CSE 4070
2nd
pass
30 > 25 swap 30 and 25
30 < 52 no swapping
52 < 65 no swapping
2nd
Max value 65 moved
to 2nd
last index
GLA University - CSE 4070
Insertion Sort
Algorithm works by moving the current
data element past the already sorted
values and repeatedly interchanging it
with preceding value until it is it’s correct
place.
less efficient when compared with other
algorithms such as quick sort, heap sort
and merge sort.
GLA University - CSE 4070
Algorithm
Insertion_sort(a[ ], n)
{
Repeat for i = 0 to n-1
set temp = a[i]
set j=i-1
Repeat while temp< a[j] and j>=0
set a[j+1]= a[j]
set j=j-1
end while
set a[j+1] = temp
end for
}
GLA University - CSE 4070
Insertion Sort
GLA University - CSE 4070
Selection Sort
It takes n-1 passes to sort the entire
array and works as follows:
◦ Find smallest value in the array and place it in
it’s position.
◦ Repeat this process untill array is sorted.
GLA University - CSE 4070
Algorithm
Selection_sort(a[ ], n)
{
Repeat for i = 0 to n-1
Repeat for j = i+1 to n-1
if(a[i]>a[j])
swap a[i] and a[j]
end if
end for
end for
}
GLA University - CSE 4070
Selection Sort : 1st
pass
GLA University - CSE 4070
2nd
pass
GLA University - CSE 4070
Quick Sort
Quick sort works s follows:
◦ Select a pivot element from the array
elements.
◦ Rearrange the array elements int the array
such that all the elements less than the pivot
element appears before the pivot and all the
elements greater than the pivot element
comes after the pivot.(this process is called
partition)
◦ Recursively sort two sub arrays thus
obtained.
GLA University - CSE 4070
Algorithm
quickSort(array, first, last)
{
pivot = partition(array, first,last)
quickSort(array,first,pivot-1)
quickSort(array,pivot+1,last)
}
GLA University - CSE 4070
Partition algorith
partition (arr[], first, last)
{
set pivot = first, i = first , j=last
Repeat while i < j
Repeat while arr[i] < arr[pivot]
set i= i+1
end while
Repeat while arr[j] > arr[pivot]
set j= j-1
end while
if i<j
swap arr[i] and arr[j]
end if
End while
Swap arr[pivot] and arr[j]
Return pivot }
GLA University - CSE 4070
quickSort(arr,0,5)
Partition Initialization...
first moves to the right until
Array element > pivot element
last moves to the left until
Array element < pivot element
swap arr[first] and arr[last]
GLA University - CSE 4070
quickSort(arr,0,5)
first moves to the right
until
Array element > pivot
element
last moves to the left
until
Array element < pivot
element
swap arr[first] and arr[last]
pivot=6
partition(arr,0,5)
GLA University - CSE 4070
quickSort(arr,0,5)
repeat right/left scan
UNTIL left & right cross
first & last CROSS!!!
Swap pivot and arr[last]
Return new
location of pivot
to quicksort
function
= return 3
GLA University - CSE 4070
Recursive calls to quickSort()
GLA University - CSE 4070
Recursive calls to quickSort()
first & llast CROSS!!!
1 - Swap pivot element and arr[last]
2 - Return new location of pivot element
to quicksort function
Return 0 GLA University - CSE 4070
Recursive calls to quickSort()
first & llast CROSS!!!
1 - Swap pivot element and arr[last]
2 - Return new location of pivot element
to quicksort function
Return 0 GLA University - CSE 4070
Quicksort
GLA University - CSE 4070
Mergesort
It focuses on divide, conquer and
combine
Divide means partitioning the array to be
sorted into two equal sub-arrays
Conquer means sorting two sub-array
recursively
Combine means merge two sorted sub-
arrays.
GLA University - CSE 4070
Algorithm
MergeSort(arr[], l, r)
{ if r>l
set mid = (l+r)/2
Call mergeSort(arr, l, mid)
Call mergeSort(arr, mid+1, r)
Call merge(arr, l, m, r)
end if
}
GLA University - CSE 4070
Mergesort
GLA University - CSE 4070
Algorithm
Merge(arr[], l, mid, r)
{ compute n1 and n2
copy n1 elements in left[]
copy n2 elements in right[]
set left[n1]= 9999, rightt[n1]= 9999
set i=0,j=0,k=0
for k = l to r
if left[i]<right[j]
set arr[k]= left[i]
i=i+1
else arr[k]=right[j]
j=j+1
end if
end for
}
GLA University - CSE 4070
Merge
array arr[ ]
GLA University - CSE 4070
Merge
GLA University - CSE 4070
GLA University - CSE 4070
Merge
GLA University - CSE 4070
Heap
A heap is a data structure that stores a
collection of keys , and has the following
properties:
◦ Complete Binary tree
◦ Satisfies Heap property which states that
 child key value >= parent key value for min - heap
 child key value <= parent key value for max - heap
Each node of the binary tree corresponds to an
element of the array. The array is completely
filled on all levels except possibly lowest.
GLA University - CSE 4070
Heap
Max-heap :
A[Parent(i)] ≥ A[i]
Min-heap : A[Parent(i)] ≤ A[i]
PARENT (i) where i is index of any key
        return floor(i/2)
LEFT (i)
        return 2i
RIGHT (i)
        return 2i + 1
GLA University - CSE 4070
Insertion in heap
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
 Let the new rise to its appropriate place, if the
parent of the element is smaller than the
element, then interchange the parent and child.
OR
Restore the min-heap property if violated
 Sink down the new element, if the parent of the
element is larger than the element, then
interchange the parent and child.
GLA University - CSE 4070
Insert 17 in max-heap
maintain the heap property
GLA University - CSE 4070
Delete from heap
Delete max
◦ Replace the last element with root value (delete
last element).
◦ Restore the max heap property by sinking down
the new root value.
Delete min
◦ Replace the last element with root value (delete
last element).
◦ Restore the min heap property by sinking down
the new root value.
GLA University - CSE 4070
Delete from heap
Swap 25
and 7
Swap 7 and 17
Swap 7 and 15
GLA University - CSE 4070
Heap Sort
 Heapify method picks the largest child key and compare
it to the parent key. If parent key is larger than heapify
quits, otherwise it swaps the parent key with the
largest child key. So that the parent is now becomes
larger than its children.
The heap algorithm works as:
◦ Build a heap from the given array using heapify
method
◦ Repeatedly delete the root element using max
heap(for descending order)
◦ Repeatedly delete the root element using min
heap(for ascending order)
GLA University - CSE 4070
Algorithm
Heapsort(A,n)
{
   Buildheap(A)
   for i = 0 to n-1 
call Insert_heap(A,n,A[i])
End for
   Repeat while n>0
call Delete_heap(A,n,value)
set n = n – 1
end while loop
}
// heapify
// Get max value after
deletion (for
descending order)
GLA University - CSE 4070
Complexities
Sortings best case Average
case
Worst
case
Bubble sort O(n ) O(n2
) O(n2
)
Insertion sort O(n ) O(n2
) O(n2
)
Selection sort O(n2
) O(n2
) O(n2
)
Quick sort O(nlogn) O(nlogn) O(n2
)
Merge sort O(nlogn) O(nlogn) O(nlogn)
Heap sort O(nlogn) O(nlogn) O(nlogn)
GLA University - CSE 4070

More Related Content

What's hot

Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityMotaleb Hossen Manik
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examplesBst Ali
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting AlgorithmsRahul Jamwal
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Mahesh Tibrewal
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 

What's hot (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Sorting
SortingSorting
Sorting
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
 
Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!Quicksort Algorithm..simply defined through animations..!!
Quicksort Algorithm..simply defined through animations..!!
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
Quick sort
Quick sortQuick sort
Quick sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 

Similar to Sorting.ppt read only

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
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
 
enhancement of sorting algorithm
enhancement of sorting algorithmenhancement of sorting algorithm
enhancement of sorting algorithmRana assad ali
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
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
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
Sorting and Searching Techniques
Sorting and Searching TechniquesSorting and Searching Techniques
Sorting and Searching TechniquesProf Ansari
 
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.ppt read only (20)

Sorting
SortingSorting
Sorting
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
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
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
enhancement of sorting algorithm
enhancement of sorting algorithmenhancement of sorting algorithm
enhancement of sorting algorithm
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
Sorting
SortingSorting
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 print
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Sorting
SortingSorting
Sorting
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Sorting and Searching Techniques
Sorting and Searching TechniquesSorting and Searching Techniques
Sorting and Searching Techniques
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 

More from VARSHAKUMARI49

More from VARSHAKUMARI49 (17)

28,29. procedures subprocedure,type checking functions in VBScript
28,29. procedures  subprocedure,type checking functions in VBScript28,29. procedures  subprocedure,type checking functions in VBScript
28,29. procedures subprocedure,type checking functions in VBScript
 
30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript30,31,32,33. decision and loop statements in vbscript
30,31,32,33. decision and loop statements in vbscript
 
27. mathematical, date and time functions in VB Script
27. mathematical, date and time  functions in VB Script27. mathematical, date and time  functions in VB Script
27. mathematical, date and time functions in VB Script
 
Cascading style sheet
Cascading style sheetCascading style sheet
Cascading style sheet
 
Html
HtmlHtml
Html
 
Introduction to web technology
Introduction to web technologyIntroduction to web technology
Introduction to web technology
 
Database normalization
Database normalizationDatabase normalization
Database normalization
 
Joins
JoinsJoins
Joins
 
Sub queries
Sub queriesSub queries
Sub queries
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Vbscript
VbscriptVbscript
Vbscript
 
Css module1
Css module1Css module1
Css module1
 
Js mod1
Js mod1Js mod1
Js mod1
 
Css mod1
Css mod1Css mod1
Css mod1
 
Html mod1
Html mod1Html mod1
Html mod1
 
Register counters.readonly
Register counters.readonlyRegister counters.readonly
Register counters.readonly
 
Hashing
HashingHashing
Hashing
 

Recently uploaded

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

Sorting.ppt read only

  • 2. Sorting : Outline Bubble Sort Insertion Sort Selection Sort Quick Sort Two-way Merge sort Heap Sort GLA University - CSE 4070
  • 3. Bubble Sort It sort the array elements by repeatedly moving the largest element to the highest index position of the array[for ascending order]. Consecutive adjacent pair of elements in the array are compared with each other. Two elements are interchanged if lower index value is greater than upper index value. Process is continued till list of unsorted elements exhausted. It is called bubble sort because larger elements ‘bubble’ to the end of the list. GLA University - CSE 4070
  • 4. Algorithm Bubble_sort(a[ ], n) { Repeat for i = 0 to n-1 Repeat for j = 0 to n-i-2 if(a[j]>a[j+1]) swap a[j] and a[j+1] end if end for end for } GLA University - CSE 4070
  • 5. Bubble Sort : 1st pass 30 < 52 no swapping 52 >25 swap 52 and 25 52 < 84 no swapping 84 > 65 swap 84 and 65 Max value 84 moved to last index GLA University - CSE 4070
  • 6. 2nd pass 30 > 25 swap 30 and 25 30 < 52 no swapping 52 < 65 no swapping 2nd Max value 65 moved to 2nd last index GLA University - CSE 4070
  • 7. Insertion Sort Algorithm works by moving the current data element past the already sorted values and repeatedly interchanging it with preceding value until it is it’s correct place. less efficient when compared with other algorithms such as quick sort, heap sort and merge sort. GLA University - CSE 4070
  • 8. Algorithm Insertion_sort(a[ ], n) { Repeat for i = 0 to n-1 set temp = a[i] set j=i-1 Repeat while temp< a[j] and j>=0 set a[j+1]= a[j] set j=j-1 end while set a[j+1] = temp end for } GLA University - CSE 4070
  • 10. Selection Sort It takes n-1 passes to sort the entire array and works as follows: ◦ Find smallest value in the array and place it in it’s position. ◦ Repeat this process untill array is sorted. GLA University - CSE 4070
  • 11. Algorithm Selection_sort(a[ ], n) { Repeat for i = 0 to n-1 Repeat for j = i+1 to n-1 if(a[i]>a[j]) swap a[i] and a[j] end if end for end for } GLA University - CSE 4070
  • 12. Selection Sort : 1st pass GLA University - CSE 4070
  • 14. Quick Sort Quick sort works s follows: ◦ Select a pivot element from the array elements. ◦ Rearrange the array elements int the array such that all the elements less than the pivot element appears before the pivot and all the elements greater than the pivot element comes after the pivot.(this process is called partition) ◦ Recursively sort two sub arrays thus obtained. GLA University - CSE 4070
  • 15. Algorithm quickSort(array, first, last) { pivot = partition(array, first,last) quickSort(array,first,pivot-1) quickSort(array,pivot+1,last) } GLA University - CSE 4070
  • 16. Partition algorith partition (arr[], first, last) { set pivot = first, i = first , j=last Repeat while i < j Repeat while arr[i] < arr[pivot] set i= i+1 end while Repeat while arr[j] > arr[pivot] set j= j-1 end while if i<j swap arr[i] and arr[j] end if End while Swap arr[pivot] and arr[j] Return pivot } GLA University - CSE 4070
  • 17. quickSort(arr,0,5) Partition Initialization... first moves to the right until Array element > pivot element last moves to the left until Array element < pivot element swap arr[first] and arr[last] GLA University - CSE 4070
  • 18. quickSort(arr,0,5) first moves to the right until Array element > pivot element last moves to the left until Array element < pivot element swap arr[first] and arr[last] pivot=6 partition(arr,0,5) GLA University - CSE 4070
  • 19. quickSort(arr,0,5) repeat right/left scan UNTIL left & right cross first & last CROSS!!! Swap pivot and arr[last] Return new location of pivot to quicksort function = return 3 GLA University - CSE 4070
  • 20. Recursive calls to quickSort() GLA University - CSE 4070
  • 21. Recursive calls to quickSort() first & llast CROSS!!! 1 - Swap pivot element and arr[last] 2 - Return new location of pivot element to quicksort function Return 0 GLA University - CSE 4070
  • 22. Recursive calls to quickSort() first & llast CROSS!!! 1 - Swap pivot element and arr[last] 2 - Return new location of pivot element to quicksort function Return 0 GLA University - CSE 4070
  • 24. Mergesort It focuses on divide, conquer and combine Divide means partitioning the array to be sorted into two equal sub-arrays Conquer means sorting two sub-array recursively Combine means merge two sorted sub- arrays. GLA University - CSE 4070
  • 25. Algorithm MergeSort(arr[], l, r) { if r>l set mid = (l+r)/2 Call mergeSort(arr, l, mid) Call mergeSort(arr, mid+1, r) Call merge(arr, l, m, r) end if } GLA University - CSE 4070
  • 27. Algorithm Merge(arr[], l, mid, r) { compute n1 and n2 copy n1 elements in left[] copy n2 elements in right[] set left[n1]= 9999, rightt[n1]= 9999 set i=0,j=0,k=0 for k = l to r if left[i]<right[j] set arr[k]= left[i] i=i+1 else arr[k]=right[j] j=j+1 end if end for } GLA University - CSE 4070
  • 28. Merge array arr[ ] GLA University - CSE 4070
  • 30. GLA University - CSE 4070
  • 32. Heap A heap is a data structure that stores a collection of keys , and has the following properties: ◦ Complete Binary tree ◦ Satisfies Heap property which states that  child key value >= parent key value for min - heap  child key value <= parent key value for max - heap Each node of the binary tree corresponds to an element of the array. The array is completely filled on all levels except possibly lowest. GLA University - CSE 4070
  • 33. Heap Max-heap : A[Parent(i)] ≥ A[i] Min-heap : A[Parent(i)] ≤ A[i] PARENT (i) where i is index of any key         return floor(i/2) LEFT (i)         return 2i RIGHT (i)         return 2i + 1 GLA University - CSE 4070
  • 34. Insertion in heap 1. Add the new element to the next available position at the lowest level 2. Restore the max-heap property if violated  Let the new rise to its appropriate place, if the parent of the element is smaller than the element, then interchange the parent and child. OR Restore the min-heap property if violated  Sink down the new element, if the parent of the element is larger than the element, then interchange the parent and child. GLA University - CSE 4070
  • 35. Insert 17 in max-heap maintain the heap property GLA University - CSE 4070
  • 36. Delete from heap Delete max ◦ Replace the last element with root value (delete last element). ◦ Restore the max heap property by sinking down the new root value. Delete min ◦ Replace the last element with root value (delete last element). ◦ Restore the min heap property by sinking down the new root value. GLA University - CSE 4070
  • 37. Delete from heap Swap 25 and 7 Swap 7 and 17 Swap 7 and 15 GLA University - CSE 4070
  • 38. Heap Sort  Heapify method picks the largest child key and compare it to the parent key. If parent key is larger than heapify quits, otherwise it swaps the parent key with the largest child key. So that the parent is now becomes larger than its children. The heap algorithm works as: ◦ Build a heap from the given array using heapify method ◦ Repeatedly delete the root element using max heap(for descending order) ◦ Repeatedly delete the root element using min heap(for ascending order) GLA University - CSE 4070
  • 40. Complexities Sortings best case Average case Worst case Bubble sort O(n ) O(n2 ) O(n2 ) Insertion sort O(n ) O(n2 ) O(n2 ) Selection sort O(n2 ) O(n2 ) O(n2 ) Quick sort O(nlogn) O(nlogn) O(n2 ) Merge sort O(nlogn) O(nlogn) O(nlogn) Heap sort O(nlogn) O(nlogn) O(nlogn) GLA University - CSE 4070