SlideShare a Scribd company logo
1 of 40
SORTING
Insertion sort, Merge sort
Insertion sort ,[object Object],[object Object],[object Object],[object Object]
Insertion Sort
Insertion Sort... ,[object Object],[object Object],[object Object],[object Object],see applet http://www.cis.upenn.edu/~matuszek/cse121-2003/Applets/Chap03/Insertion/InsertSort.html
Extended Example To sort the following numbers in increasing order: 34  8  64  51  32  21 P = 1;  Look at first element only, no change. P = 2;  tmp = 8; 34 > tmp, so second element is set to 34. We have reached the front of the list. Thus, 1st position = tmp After second pass:  8  34  64  51  32  21  (first 2 elements are sorted)
P = 3;  tmp = 64; 34 < 64, so stop at 3 rd  position and set 3 rd  position = 64 After third pass:  8  34  64  51  32  21  (first 3 elements are sorted) P = 4;  tmp = 51; 51 < 64, so we have  8  34  64  64  32  21,  34 < 51, so stop at 2nd position, set 3 rd  position = tmp, After fourth pass: 8  34  51  64  32  21 (first 4 elements are sorted) P = 5; tmp = 32,  32 < 64, so 8  34  51  64  64  21,  32  < 51, so 8  34  51  51  64  21,  next 32 < 34,  so 8  34  34, 51  64  21,  next 32 > 8, so stop at 1st position and set 2 nd  position = 32, After fifth pass: 8  32  34  51  64  21 P = 6; tmp = 21,  . . .  After sixth pass:  8  21  32  34  51  64
Analysis: worst-case running time ,[object Object],[object Object],[object Object]
Analysis ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Analysis: best case ,[object Object],[object Object],[object Object],[object Object]
Mergesort ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dividing ,[object Object],[object Object],[object Object],[object Object]
Mergesort ,[object Object],[object Object],[object Object]
see applet http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/MSort.html
How to merge? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Merge
Example: Merge... ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 
Analysis of mergesort ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Analysis: solving recurrence Since N=2 k , we have k=log 2  n
 
An experiment ,[object Object],[object Object]
Heaps and heapsort
Motivating Example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Common Implementations ,[object Object],[object Object],[object Object],[object Object],2) Search Tree (AVL tree, to be covered later) Insert in O(log n) Delete in O(log n) Search tree is an overkill as it does many other operations
Heaps ,[object Object],[object Object],[object Object],Note: we define “complete tree” slightly different from the book.
Binary Trees ,[object Object],[object Object],[object Object],[object Object]
Binary trees A complete tree An almost complete tree A complete binary tree is one where a node can have 0 (for the leaves) or 2 children and all leaves are at the same depth.
Height (depth) of a binary tree ,[object Object],Height = 4
Height of a binary tree ,[object Object],d 2 d Total: 2 d+1  - 1
Proof Prove by induction that number of nodes at depth d is 2 d Total number of nodes of a complete binary tree of depth d is 1 + 2 + 4 +…… 2 d  = 2 d+1  -  1  Thus 2 d+1  -  1  =  N d  = log(N + 1)  - 1 =  O(log N) What is the largest depth of a binary tree of N nodes? N
Coming back to Heap Heap property: the value at each node is less than or equal to that of  its descendants. not a heap A heap 4 2 5 6 1 3 1 2 5 6 4 3
Heap ,[object Object],[object Object],[object Object]
Insertion ,[object Object],[object Object],[object Object]
Insert 2.5 Percolate  up to maintain the heap property 1 2 5 6 4 3 1 2 5 6 4 3 2.5 1 2 2.5 6 4 3 5
 
Insertion  A heap!
DeleteMin: first attempt Delete the root. Compare the two children of the root Make the lesser of the two the root. An empty spot is created. Bring the lesser of the two children of the empty spot to the empty spot. A new empty spot is created. Continue
Heap property is preserved, but completeness is not preserved! 1 2 5 6 4 3 2 5 6 4 3 2 5 6 4 3 2 3 5 6 4
DeleteMin ,[object Object],[object Object]

More Related Content

What's hot

Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
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
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structureTushar Aneyrao
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & DeletionAfaq Mansoor Khan
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sortVasim Pathan
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked ListsJ.T.A.JONES
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 

What's hot (20)

Counting sort
Counting sortCounting sort
Counting sort
 
Lec10
Lec10Lec10
Lec10
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Lec23
Lec23Lec23
Lec23
 
Linked list
Linked listLinked list
Linked list
 
Presentation
PresentationPresentation
Presentation
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
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...
 
Array implementation and linked list as datat structure
Array implementation and linked list as datat structureArray implementation and linked list as datat structure
Array implementation and linked list as datat structure
 
Linked list
Linked listLinked list
Linked list
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Mergesort
MergesortMergesort
Mergesort
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Address calculation-sort
Address calculation-sortAddress calculation-sort
Address calculation-sort
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Viewers also liked

Merge sorting v2
Merge sorting v2Merge sorting v2
Merge sorting v2Miss_Dixon
 
Merge Sort
Merge SortMerge Sort
Merge SortAl Vas
 
Merge sort
Merge sortMerge sort
Merge sortSumedha
 
Bin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. TrinidadBin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. TrinidadLUISITO TRINIDAD
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort animFajar Zain
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil DuttAnil Dutt
 
Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)oDesk
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sortKrish_ver2
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languageBilal Amjad
 
Bubble sort
Bubble sort Bubble sort
Bubble sort rmsz786
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting AlgorithmsDamian T. Gordon
 

Viewers also liked (20)

Merge sorting v2
Merge sorting v2Merge sorting v2
Merge sorting v2
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Sorting
SortingSorting
Sorting
 
Bin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. TrinidadBin Sorting And Bubble Sort By Luisito G. Trinidad
Bin Sorting And Bubble Sort By Luisito G. Trinidad
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Sorting bubble-sort anim
Sorting   bubble-sort animSorting   bubble-sort anim
Sorting bubble-sort anim
 
Marge Sort
Marge SortMarge Sort
Marge Sort
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting techniques Anil Dutt
Sorting techniques Anil DuttSorting techniques Anil Dutt
Sorting techniques Anil Dutt
 
Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)Sorting (Bubble,Merge,Selection sort)
Sorting (Bubble,Merge,Selection sort)
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
3.1 bubble sort
3.1 bubble sort3.1 bubble sort
3.1 bubble sort
 
Sorting
SortingSorting
Sorting
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly language
 
Bubble sort
Bubble sort Bubble sort
Bubble sort
 
Advanced Sorting Algorithms
Advanced Sorting AlgorithmsAdvanced Sorting Algorithms
Advanced Sorting Algorithms
 

Similar to SORTING ALGORITHMS

lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannualmaamir farooq
 
Review session2
Review session2Review session2
Review session2NEEDY12345
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docx
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docxJAVA 1 Suppose you are given a sorted list of N elements followed by f.docx
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docxolsenlinnea427
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4Kumar
 
Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithmsGanesh Solanke
 

Similar to SORTING ALGORITHMS (20)

searching.pdf
searching.pdfsearching.pdf
searching.pdf
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Sorting2
Sorting2Sorting2
Sorting2
 
lecture 10
lecture 10lecture 10
lecture 10
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 
Review session2
Review session2Review session2
Review session2
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docx
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docxJAVA 1 Suppose you are given a sorted list of N elements followed by f.docx
JAVA 1 Suppose you are given a sorted list of N elements followed by f.docx
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Data structure lecture 4
Data structure lecture 4Data structure lecture 4
Data structure lecture 4
 
Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithms
 

More from Saurabh Mishra (7)

Searching
SearchingSearching
Searching
 
Presentation1
Presentation1Presentation1
Presentation1
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graphs
GraphsGraphs
Graphs
 
Data structures
Data structuresData structures
Data structures
 
Trees
TreesTrees
Trees
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 

SORTING ALGORITHMS

  • 3.
  • 5.
  • 6. Extended Example To sort the following numbers in increasing order: 34 8 64 51 32 21 P = 1; Look at first element only, no change. P = 2; tmp = 8; 34 > tmp, so second element is set to 34. We have reached the front of the list. Thus, 1st position = tmp After second pass: 8 34 64 51 32 21 (first 2 elements are sorted)
  • 7. P = 3; tmp = 64; 34 < 64, so stop at 3 rd position and set 3 rd position = 64 After third pass: 8 34 64 51 32 21 (first 3 elements are sorted) P = 4; tmp = 51; 51 < 64, so we have 8 34 64 64 32 21, 34 < 51, so stop at 2nd position, set 3 rd position = tmp, After fourth pass: 8 34 51 64 32 21 (first 4 elements are sorted) P = 5; tmp = 32, 32 < 64, so 8 34 51 64 64 21, 32 < 51, so 8 34 51 51 64 21, next 32 < 34, so 8 34 34, 51 64 21, next 32 > 8, so stop at 1st position and set 2 nd position = 32, After fifth pass: 8 32 34 51 64 21 P = 6; tmp = 21, . . . After sixth pass: 8 21 32 34 51 64
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 15.
  • 17.
  • 18.  
  • 19.
  • 20. Analysis: solving recurrence Since N=2 k , we have k=log 2 n
  • 21.  
  • 22.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. Binary trees A complete tree An almost complete tree A complete binary tree is one where a node can have 0 (for the leaves) or 2 children and all leaves are at the same depth.
  • 29.
  • 30.
  • 31. Proof Prove by induction that number of nodes at depth d is 2 d Total number of nodes of a complete binary tree of depth d is 1 + 2 + 4 +…… 2 d = 2 d+1 - 1 Thus 2 d+1 - 1 = N d = log(N + 1) - 1 = O(log N) What is the largest depth of a binary tree of N nodes? N
  • 32. Coming back to Heap Heap property: the value at each node is less than or equal to that of its descendants. not a heap A heap 4 2 5 6 1 3 1 2 5 6 4 3
  • 33.
  • 34.
  • 35. Insert 2.5 Percolate up to maintain the heap property 1 2 5 6 4 3 1 2 5 6 4 3 2.5 1 2 2.5 6 4 3 5
  • 36.  
  • 37. Insertion A heap!
  • 38. DeleteMin: first attempt Delete the root. Compare the two children of the root Make the lesser of the two the root. An empty spot is created. Bring the lesser of the two children of the empty spot to the empty spot. A new empty spot is created. Continue
  • 39. Heap property is preserved, but completeness is not preserved! 1 2 5 6 4 3 2 5 6 4 3 2 5 6 4 3 2 3 5 6 4
  • 40.