SlideShare a Scribd company logo
S O R T I N G A lecture by   ABDUL GHAFFAR  Chapter 3  Of  Reference  #1
Reference Books Data Structures and Algorithm Analysis in C By Mark Allen Weiss Published by Addison Wesley Data Structures (Schaum’s Outline Series) By Seymour Lipschutz  Published by Mc Graw Hill
Contents Selection sort  Insertion sort  Merge sort Quick sort Heap Sort External Sort link2
Selection Sort  ( Ref # 2- 9.4) Find the Largest element in the list and put it in the last position. Find the second largest element in the list and put it in the second last position Repeat the process for all elements
Selection Sort  (continue..)
Insertion Sort  (Ref #1-7.2,  Ref  2- 9.3)
Insertion Sort  (continue..)
Insertion Sort  (continue..)
Insertion Sort  (continue..)
Merge Sort  (Ref # 1- 7.6, Ref #2-8,9.6) In  merge, two sorted sequences are merged into one. Clearly, two sorted sequences each of length  n  can be merged into a sorted sequence of length 2 n  in  O (2 n )= O ( n )  steps. However in order to do this, we need space in which to store the result.
Merge Sort  (continue..) Sorting by merging is a recursive, divide-and-conquer strategy. In the base case, we have a sequence with exactly one element in it. Since such a sequence is already sorted, there is nothing to be done. To sort a sequence of  n> 1 elements:  Divide the sequence into two sequences of length   n/2    and   n/2   ;  recursively sort each of the two subsequences; and then,  merge the sorted subsequences to obtain the final result.
Merge Sort  (continue..)
Merge Sort  (continue..) Implementation in C++
Merge Sort  (continue..) Implementation in C++ Merging The Merge function of the TwoWayMergeSorter<T> class is defined in Program    . Altogether, this function takes four parameters: The first is a reference to the array to be sorted. The remaining three, left, middle, and right, are unsigned integers. It is assumed that  Furthermore, it is assumed that the two subsequences of the array,  and  are both sorted. The Merge routine merges the two sorted subsequences using the temporary array specified by tempArray. It then copies the merged (and sorted) sequence into the array at
Merge Sort  (continue..) Implementation in C++
Merge Sort  (continue..) Implementation in C++
Quick Sort ( Ref #1-7.x, Ref#2-x,y) Quicksort is a  divide-and-conquer  style algorithm. A divide-and-conquer algorithm solves a given problem by splitting it into two or more smaller sub problems, recursively solving each of the sub problems, and then combining the solutions to the smaller problems to obtain a solution to the original one.   To sort the sequence   S={s1,s2,s3,…..,sn}  ,  Quicksort performs the   following steps:  Select one of the elements of  S . The selected element,  p , is called the  pivot  .  Remove  p  from  S  and then partition the remaining elements of  S  into two distinct sequences,  L  and  G , such that every element in  L  is less than or equal to the pivot and every element in  G  is greater than or equal to the pivot. In general, both  L  and  G  are  unsorted .  Rearrange the elements of the sequence as follows:  (continue..)
Quick Sort  ( Continue… ) Notice that the pivot is now in the position in which it belongs in the sorted sequence, since all the elements to the left of the pivot are less than or equal to the pivot and all the elements to the right are greater than or equal to it. Recursively quicksort the unsorted sequences  L  and  G .   The first step of the algorithm is a crucial one. We have not specified how to select the pivot. Fortunately, the sorting algorithm works no matter which element is chosen to be the pivot. However, the pivot selection affects directly the running time of the algorithm. If we choose poorly the running time will be poor.
Quick Sort  ( Continue… ) Next example illustrates the detailed operation of quicksort as it sorts the sequence  { 3,1,4,1,5,9,2,5,4)  . To begin the sort, we select a pivot. In this example, the value 4 in the last array position is chosen.  Next, the remaining elements are partitioned into two sequences, one which contains values less than or equal to 4 ( L={3,1,2,1}  ) , and one which contains values greater than or equal to 4 (  G={5,9,4,6,5} ). Notice that the partitioning is accomplished by exchanging elements. This is why quicksort is considered to be an exchange sort.  Example
Quick Sort  ( Continue… ) Example
Quick Sort  ( Continue… ) Example
Quick Sort  ( Continue… ) void Quicksort( ElementType A[],  int N ) {  Qsort(A, 0, N-1 ); } Implementation in C
Quick Sort  ( Continue… ) Implementation in C
Heap Sort ( Ref #1-7.7,  Ref #2 -9.10) Some Definitions  A tree is said to be   complete  if if all it’s levels, except possibly the last have the maximum number of possible nodes ( 2 for binary tree) Heaps can be implemented using an array. The position of an element in the array relates it to other elements of the HEAP.
Heap Sort  (Continue..) Given an element at position i: Parent of i  =  int[ i / 2 ] ,  Left child of i  = 2i  ,  Right child of i  = 2i+1
Heap Sort  (Continue..) Array representation of Binary Tree
Heap Sort  (Continue..) Inserting into a heap Suppose H is a heap with n elements
Heap Sort  (Continue..) Insert into a heap:  If we want to insert an ITEM into H then we have to follow two steps First adjoin ITEM at the end of the H so that H is still a complete tree, but not necessarily a heap. Then let ITEM raise to its ‘appropriate place’ in H so that |H is finally a heap
Heap Sort  (Continue..) Suppose we want to insert 70 in our heap H, the procedure will follow these steps…
Heap Sort  (Continue..) Final tree after the insertion of element 70, will become like as below,
Heap Sort  (Continue..) Building a heap: Suppose we want to build a heap H from the following list of numbers; 44, 30, 22, 60, 55, 77, 55 This can be build by inserting these number on by one into an empty heap using the insertion procedure, as..
Heap Sort  (Continue..)  Building a heap
Heap Sort  (Continue..)  Building a heap
Heap Sort  (Continue..)  Building a heap The Algorithm
Heap Sort  (Continue..) Deleting the root of a heap:  The procedure to delete the root of a heap H with N elements will follow three steps as, Assign the root R to some variable ITEM. Replace The deleted node R by the last node L of H so that H is still a complete tree, but not necessarily a heap. (Reheap) Let L sink to its appropriate place in H so that H is finally a heap.
Heap Sort  (Continue..) Deleting the Root, an Example
Heap Sort  (Continue..)    Deleting the root (Algorithm)
Heap Sort  (Continue..)    Application algorithm
Heap Sort  (Continue..)  Implementation in C++
Heap Sort  (Continue..)  Implementation in C++
Heap Sort  (Continue..)  Implementation in C++
Heap Sort  (Continue..)  Implementation in C++
Heap Sort  (Continue..)  Implementation in C++
Heap Sort  (Continue..)  Implementation in C++ The Sorting Phase : Once the max heap has been built, heapsort proceeds to the sorting phase.b The sorting phase of heapsort works like this: We repeatedly swap the largest element in the heap (always in position 1) into the next position of the sorted sequence. After each such swap, there is a new value at the root of the heap and this new value is pushed down into the correct  position in the heap using the Percolate  Down routine.
Heap Sort  (Continue..)  Implementation in C++ The Sorting Phase : The next example explains this phase
Heap Sort  (Continue..)  Example
Heap Sort  (Continue..)  Example
Heap Sort  (Continue..)  Example
Heap Sort  (Continue..)  Example
External Sort
External Sort  (continue…)
External Sort  (continue…)

More Related Content

PPT
Data Structures - Searching & sorting
PPTX
Quick Sort
PPTX
Doubly Linked List
DOCX
Bubble sorting lab manual
PPTX
Hashing Technique In Data Structures
PDF
PPT
PDF
Binary Search - Design & Analysis of Algorithms
Data Structures - Searching & sorting
Quick Sort
Doubly Linked List
Bubble sorting lab manual
Hashing Technique In Data Structures
Binary Search - Design & Analysis of Algorithms

What's hot (20)

PPTX
Linked list
PPT
Searching algorithms
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
Stack_Data_Structure.pptx
PPT
PPTX
linked list in Data Structure, Simple and Easy Tutorial
PDF
linear search and binary search
PDF
Array data structure
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
Binary search
PPT
Priority queues
PPTX
Merge sort algorithm power point presentation
PPTX
Searching and sorting
PPTX
Sorting algorithms
PPTX
Quick_sort1.pptx
PPTX
Collision in Hashing.pptx
PPT
Lec 17 heap data structure
PPSX
PPT
PPTX
Ppt bubble sort
Linked list
Searching algorithms
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Stack_Data_Structure.pptx
linked list in Data Structure, Simple and Easy Tutorial
linear search and binary search
Array data structure
SEARCHING AND SORTING ALGORITHMS
Binary search
Priority queues
Merge sort algorithm power point presentation
Searching and sorting
Sorting algorithms
Quick_sort1.pptx
Collision in Hashing.pptx
Lec 17 heap data structure
Ppt bubble sort
Ad

Similar to Sorting (20)

PPT
Advanced s and s algorithm.ppt
PDF
Design and analysis of algorithm final course
PDF
Design and analysis of algorithm final course
PDF
Heap, quick and merge sort
PPTX
data structures and algorithms Unit 3
PDF
Data structures arrays
PPTX
All Searching and Sorting Techniques in Data Structures
PPTX
VCE Unit 04vv.pptx
PDF
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
PPTX
VCE Unit 03vv.pptx
PPTX
Sorting2
DOCX
Sorting
DOC
Selection sort
PDF
PPT
Unit6 C
PPT
lecture-k-sorting.ppt
PPT
Lecture k-sorting
PPTX
Dsa – data structure and algorithms sorting
PPT
Algorithms with-java-advanced-1.0
Advanced s and s algorithm.ppt
Design and analysis of algorithm final course
Design and analysis of algorithm final course
Heap, quick and merge sort
data structures and algorithms Unit 3
Data structures arrays
All Searching and Sorting Techniques in Data Structures
VCE Unit 04vv.pptx
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
VCE Unit 03vv.pptx
Sorting2
Sorting
Selection sort
Unit6 C
lecture-k-sorting.ppt
Lecture k-sorting
Dsa – data structure and algorithms sorting
Algorithms with-java-advanced-1.0
Ad

More from Ghaffar Khan (20)

PPT
World is beautiful ... ...
PPTX
My Presentation On Ajax
PPT
How A Computer Works
PPT
For Loop
PPT
Exponential and Logarthmic funtions
PPT
Exponential and Logarthmic funtions (1)
PPT
Functions
PPT
Quadratic And Polinomial Function
PPT
Quadratic And Polinomial Function
PPT
Exponentioal And Logarthmic Functions
PPT
Internet Protocol
PPT
Introduction to Computer Networks
PPT
Network Layer
PPT
Control Structures
PPT
Input And Output
PPT
Surfaces
PPT
Vector Tools
PPT
Drawing Tools
PPT
Drawing Figures
PPT
Computer Graphics Introduction
World is beautiful ... ...
My Presentation On Ajax
How A Computer Works
For Loop
Exponential and Logarthmic funtions
Exponential and Logarthmic funtions (1)
Functions
Quadratic And Polinomial Function
Quadratic And Polinomial Function
Exponentioal And Logarthmic Functions
Internet Protocol
Introduction to Computer Networks
Network Layer
Control Structures
Input And Output
Surfaces
Vector Tools
Drawing Tools
Drawing Figures
Computer Graphics Introduction

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
Understanding_Digital_Forensics_Presentation.pptx
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity

Sorting

  • 1. S O R T I N G A lecture by ABDUL GHAFFAR Chapter 3 Of Reference #1
  • 2. Reference Books Data Structures and Algorithm Analysis in C By Mark Allen Weiss Published by Addison Wesley Data Structures (Schaum’s Outline Series) By Seymour Lipschutz Published by Mc Graw Hill
  • 3. Contents Selection sort Insertion sort Merge sort Quick sort Heap Sort External Sort link2
  • 4. Selection Sort ( Ref # 2- 9.4) Find the Largest element in the list and put it in the last position. Find the second largest element in the list and put it in the second last position Repeat the process for all elements
  • 5. Selection Sort (continue..)
  • 6. Insertion Sort (Ref #1-7.2, Ref 2- 9.3)
  • 7. Insertion Sort (continue..)
  • 8. Insertion Sort (continue..)
  • 9. Insertion Sort (continue..)
  • 10. Merge Sort (Ref # 1- 7.6, Ref #2-8,9.6) In merge, two sorted sequences are merged into one. Clearly, two sorted sequences each of length n can be merged into a sorted sequence of length 2 n in O (2 n )= O ( n ) steps. However in order to do this, we need space in which to store the result.
  • 11. Merge Sort (continue..) Sorting by merging is a recursive, divide-and-conquer strategy. In the base case, we have a sequence with exactly one element in it. Since such a sequence is already sorted, there is nothing to be done. To sort a sequence of n> 1 elements: Divide the sequence into two sequences of length  n/2  and  n/2  ; recursively sort each of the two subsequences; and then, merge the sorted subsequences to obtain the final result.
  • 12. Merge Sort (continue..)
  • 13. Merge Sort (continue..) Implementation in C++
  • 14. Merge Sort (continue..) Implementation in C++ Merging The Merge function of the TwoWayMergeSorter<T> class is defined in Program  . Altogether, this function takes four parameters: The first is a reference to the array to be sorted. The remaining three, left, middle, and right, are unsigned integers. It is assumed that Furthermore, it is assumed that the two subsequences of the array, and are both sorted. The Merge routine merges the two sorted subsequences using the temporary array specified by tempArray. It then copies the merged (and sorted) sequence into the array at
  • 15. Merge Sort (continue..) Implementation in C++
  • 16. Merge Sort (continue..) Implementation in C++
  • 17. Quick Sort ( Ref #1-7.x, Ref#2-x,y) Quicksort is a divide-and-conquer style algorithm. A divide-and-conquer algorithm solves a given problem by splitting it into two or more smaller sub problems, recursively solving each of the sub problems, and then combining the solutions to the smaller problems to obtain a solution to the original one. To sort the sequence S={s1,s2,s3,…..,sn} , Quicksort performs the following steps: Select one of the elements of S . The selected element, p , is called the pivot  . Remove p from S and then partition the remaining elements of S into two distinct sequences, L and G , such that every element in L is less than or equal to the pivot and every element in G is greater than or equal to the pivot. In general, both L and G are unsorted . Rearrange the elements of the sequence as follows: (continue..)
  • 18. Quick Sort ( Continue… ) Notice that the pivot is now in the position in which it belongs in the sorted sequence, since all the elements to the left of the pivot are less than or equal to the pivot and all the elements to the right are greater than or equal to it. Recursively quicksort the unsorted sequences L and G . The first step of the algorithm is a crucial one. We have not specified how to select the pivot. Fortunately, the sorting algorithm works no matter which element is chosen to be the pivot. However, the pivot selection affects directly the running time of the algorithm. If we choose poorly the running time will be poor.
  • 19. Quick Sort ( Continue… ) Next example illustrates the detailed operation of quicksort as it sorts the sequence { 3,1,4,1,5,9,2,5,4) . To begin the sort, we select a pivot. In this example, the value 4 in the last array position is chosen. Next, the remaining elements are partitioned into two sequences, one which contains values less than or equal to 4 ( L={3,1,2,1} ) , and one which contains values greater than or equal to 4 ( G={5,9,4,6,5} ). Notice that the partitioning is accomplished by exchanging elements. This is why quicksort is considered to be an exchange sort. Example
  • 20. Quick Sort ( Continue… ) Example
  • 21. Quick Sort ( Continue… ) Example
  • 22. Quick Sort ( Continue… ) void Quicksort( ElementType A[], int N ) { Qsort(A, 0, N-1 ); } Implementation in C
  • 23. Quick Sort ( Continue… ) Implementation in C
  • 24. Heap Sort ( Ref #1-7.7, Ref #2 -9.10) Some Definitions A tree is said to be complete if if all it’s levels, except possibly the last have the maximum number of possible nodes ( 2 for binary tree) Heaps can be implemented using an array. The position of an element in the array relates it to other elements of the HEAP.
  • 25. Heap Sort (Continue..) Given an element at position i: Parent of i = int[ i / 2 ] , Left child of i = 2i , Right child of i = 2i+1
  • 26. Heap Sort (Continue..) Array representation of Binary Tree
  • 27. Heap Sort (Continue..) Inserting into a heap Suppose H is a heap with n elements
  • 28. Heap Sort (Continue..) Insert into a heap: If we want to insert an ITEM into H then we have to follow two steps First adjoin ITEM at the end of the H so that H is still a complete tree, but not necessarily a heap. Then let ITEM raise to its ‘appropriate place’ in H so that |H is finally a heap
  • 29. Heap Sort (Continue..) Suppose we want to insert 70 in our heap H, the procedure will follow these steps…
  • 30. Heap Sort (Continue..) Final tree after the insertion of element 70, will become like as below,
  • 31. Heap Sort (Continue..) Building a heap: Suppose we want to build a heap H from the following list of numbers; 44, 30, 22, 60, 55, 77, 55 This can be build by inserting these number on by one into an empty heap using the insertion procedure, as..
  • 32. Heap Sort (Continue..) Building a heap
  • 33. Heap Sort (Continue..) Building a heap
  • 34. Heap Sort (Continue..) Building a heap The Algorithm
  • 35. Heap Sort (Continue..) Deleting the root of a heap: The procedure to delete the root of a heap H with N elements will follow three steps as, Assign the root R to some variable ITEM. Replace The deleted node R by the last node L of H so that H is still a complete tree, but not necessarily a heap. (Reheap) Let L sink to its appropriate place in H so that H is finally a heap.
  • 36. Heap Sort (Continue..) Deleting the Root, an Example
  • 37. Heap Sort (Continue..) Deleting the root (Algorithm)
  • 38. Heap Sort (Continue..) Application algorithm
  • 39. Heap Sort (Continue..) Implementation in C++
  • 40. Heap Sort (Continue..) Implementation in C++
  • 41. Heap Sort (Continue..) Implementation in C++
  • 42. Heap Sort (Continue..) Implementation in C++
  • 43. Heap Sort (Continue..) Implementation in C++
  • 44. Heap Sort (Continue..) Implementation in C++ The Sorting Phase : Once the max heap has been built, heapsort proceeds to the sorting phase.b The sorting phase of heapsort works like this: We repeatedly swap the largest element in the heap (always in position 1) into the next position of the sorted sequence. After each such swap, there is a new value at the root of the heap and this new value is pushed down into the correct position in the heap using the Percolate Down routine.
  • 45. Heap Sort (Continue..) Implementation in C++ The Sorting Phase : The next example explains this phase
  • 46. Heap Sort (Continue..) Example
  • 47. Heap Sort (Continue..) Example
  • 48. Heap Sort (Continue..) Example
  • 49. Heap Sort (Continue..) Example
  • 51. External Sort (continue…)
  • 52. External Sort (continue…)