SlideShare a Scribd company logo
Sorting & Searching
Searching
Internal Search
The search in which the whole list resides in
the main memory is called internal search.
External Search
The search in which the whole list resides in
the Secondary memory is called external
search.
Types of searching Algorithm
 Linear or Sequential searching
 Binary searching
Algorithm For Linear Search
LINEAR(DATA ,N,ITEM,LOC)
1.Set K:=1 and LOC := 0.
2.Repeat Steps 3 and 4 while LOC = 0 and K<=N.
3.If ITEM = DATA[K] ,then Set LOC:= K.
4.Set K:=K+1.
5.If LOC = 0,then:
Write: ITEM is not in the array DATA.
Else:
Write: LOC is the location of ITEM.
6.Exit.
Advantages
 Not Expensive.
 List/Array may or may not be sorted.
Disadvantages
 Less Efficient.
 Time consuming.
Algorithm For Binary Search
BINARY(DATA , LB , UB, ITEM , LOC)
1. Set BEG:= LB, END:=UB and
MID = INT((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG<=END
and DATA[MID]!=ITEM.
3. If ITEM< DATA[MID] ,then:
Set END := MID-1.
Else:
Set BEG := MID+1.
4. Set MID := INT((BEG+END)/2).
Algo. Cont.
5. If DATA[MID]=ITEM, then:
Set LOC:= MID.
Else:
Set LOC:= NULL.
6. Exit.
Advantages
 Extremely Efficient
 Less Time Consuming
Disadvantages
 Expensive
 List/Array may be sorted.
Introduction to Sorting
 Sorting: An operation that arranges items into
groups according to specified criterion.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
Why Sort and Examples
Consider:
 Sorting Books in Library (Dewey system)
 Sorting Individuals by Height (Feet and
Inches)
 Sorting Movies in Blockbuster
(Alphabetical)
 Sorting Numbers (Sequential)
Types of Sorting Algorithms
There are many different types of sorting
algorithms, but the primary ones are:
 Bubble Sort
 Selection Sort
 Insertion Sort
 Merge Sort
 Quick Sort
 Radix Sort
 Heap Sort
Complexity
 Most of the primary sorting algorithms run
on different space and time complexity.
 Time Complexity is defined to be the time
the computer takes to run a program (or
algorithm in our case).
 Space complexity is defined to be the
amount of memory the computer needs to
run a program.
Complexity (cont.)
Complexity in general, measures the
algorithms efficiency in internal factors such
as the time needed to run an algorithm.
External Factors (not related to complexity):
Size of the input of the algorithm
Speed of the Computer
Quality of the Compiler
O(n), Ω(n), & Θ(n)
 An algorithm or function T(n) is O(f(n))
whenever T(n)'s rate of growth is less than
or equal to f(n)'s rate.
 An algorithm or function T(n) is Ω(f(n))
whenever T(n)'s rate of growth is greater
than or equal to f(n)'s rate.
 An algorithm or function T(n) is Θ(f(n)) if
and only if the rate of growth of T(n) is equal
to f(n).
1
0
Common Big-Oh’s
Time complexity Example
O(1) constant Adding to the front of a linked list
O(log N) log Finding an entry in a sorted array
O(N) linear Finding an entry in an unsorted array
O(N log N) n-log-n Sorting n items by ‘divide-and-conquer’
O(N2
) quadratic Shortest path between two nodes in a graph
O(N3
) cubic Simultaneous linear equations
(Binary)
Finding 8:
9
50
22
21
8
5
1
(Linear)
Finding 8:
9
50
22
21
8
5
1
Front
Initial:
Final:6
3
http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt
Big-Oh to Primary Sorts
● Bubble Sort = n²
● Selection Sort = n²
● Insertion Sort = n²
● Merge Sort = n log(n)
●Quick Sort = n log(n)
Time Efficiency
 How do we improve the time efficiency of a
program?
 The 90/10 Rule
 90% of the execution time of a program is spent in
 executing 10% of the code
 So, how do we locate the critical 10%?
 software metrics tools
 global counters to locate bottlenecks (loop
executions, function calls)
Time Efficiency Improvements
 Possibilities (some better than others!)
 Move code out of loops that does not belong
there (just good programming!)
 Remove any unnecessary I/O operations (I/O
operations are expensive time-wise)
 Code so that the compiled code is more
efficient
Moral - Choose the most appropriate
algorithm(s) BEFORE program implementation
Selection Sort
Min & Loc get swapped
Selection Sort
MIN(A,K,N,LOC)
1. Set MIN = A[K] and LOC = K
2. Repeat for J=K+1, K+2 ……… N
If MIN>A[J], then Set MIN = A[J]
and LOC = J
End of Loop
3. Return
Selection Sort
SELECTION (A,N)
1. Repeat steps 2 and 3 for K=1, 2 ……… N-1
2. Call MIN(A,K,N,LOC)
3. Set TEMP = A[K]
A[K] = A[LOC]
A[LOC] = TEMP
End of Step 1 Loop
4. Exit
Complexity of the Selection Sort
 Worst Case
 O(n2)
 Average Case
 O(n2)
Insertion Sort
Insertion Sort
Element get compared by previous no.s
Insertion Sort
INSERTION (A,N)
1. Set A[0] = - ∞
2. Repeat Steps 3 to 5 for K = 2,3,….N
3. Set TEMP = A[K] and PTR = K-1
4. Repeat while TEMP<A[PTR]
1. Set A[PTR+1] = A[PTR]
2. Set PTR = PTR – 1
End of Loop
5. Set A[PTR+1] = TEMP
End of Step 2 Loop
6. Return
Complexity of the Insertion Sort
 Worst Case
 O(n2)
 Average Case
 O(n2)
BUBBLE SORT
Algorithm for BUBBLE SORT
BUBBLE(DATA , N)
1.Repeat Steps 2 and 3 for K=1 to N-1.
2.Set PTR := 1. // Initialize pass pointer PTR
3.Repeat while PTR<=N-K.
a) If DATA[PTR]>DATA[PTR+1],then
Interchange DATA[PTR] and
DATA[PTR+1]
b) Set PTR:= PTR+1
4. Exit.
Pseudocode:-
29
Merge-Sort
Merge-Sort(cont.)
Merge-Sort (cont’d)
MERGE SORT
MERGE_SORT (A, BEG, END)
1. IF BEG<ENG
SET MID=(BEG+END)/2
CALL MERGE_SORT (A, BEG, MID)
CALL MERGE_SORT (A, MID+1, END)
MERGE(A, BEG, MID, END)
2. END
MERGE(A, BEG, MID, END)
1. SET I = BEG, J=MID+1, INDEX=0
2. REPEAT WHILE (I<=MID) AND (J<=END)
IF A[I]<A[J], THEN
SET TEMP [INDEX] = A[I]
SET I=I+1
ELSE
SET TEMP [INDEX]=A[J]
SET J=J+1
SET INDEX = INDEX +1
3. IF I>MID THEN
REPEAT WHILE J<=END
SET TEMP [INDEX]=A[J]
SET INDEX=INDEX+1
SET J=J+1
ELSE
REPEAT WHILE I<=MID
SET TEMP[INDEX]=A[I]
SET INDEX = INDEX +1
SET I=I+1
4. SET K=0
5. REPEAT WHILE K<INDEX
SET A[K]=TEMP[K]
SET K=K+1
6. END
QUICKSORT
Starting with first element of list do the operations
1. Compare from right to find element less than
selected and interchange
2. Compare from left to find element greater than
selected and interchange
44,33,11,55,77,90,40,60,33,22,88,66
22,33,11,55,77,90,40,60,99,44,88,66
22,33,11,44,77,90,40,60,99,55,88,66
22,33,11,40,77,90,44,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
22,33,11,40,44,90,77,60,99,55,88,66
Two Sublists are created:
Before 44 and After 44
38
Quick Sort Algorithm
QUICK (A, N, BEG, END, LOC)
1. [Initialize] Set LEFT= BEG, RIGHT=END and LOC=BEG
2. [Scan from right to left]
a) Repeat while A[LOC]<=A[RIGHT] and LOC != RIGHT
RIGHT=RIGHT-1
[End of loop]
b) If LOC=RIGHT then : Return
c) If A[LOC]>A[RIGHT] then
i) Interchange A[LOC] and A[RIGHT]
ii) Set LOC=RIGHT
iii) Go to step 3
[End of If structure]
3. [Scan from left to right]
a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC
LEFT=LEFT+1
[End of loop]
b) If LOC=LEFT, then Return
c) If A[LEFT]>A[LOC], then
i) Interchange A[LEFT] and A[LOC]
ii) Set LOC=LEFT
iii) Goto step 2
[End of If structure]
39
(QuickSort) This algorithm sorts an array A with N elements.
1. TOP=NULL
2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N
3. Repeat Steps 4 to 7 while TOP!=NULL
4. Set BEG=LOWER[TOP], END=UPPER[TOP]
TOP=TOP-1
5. Call QUICK(A, N, BEG, END, LOC)
6. If BEG<LOC-1 then
TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1
[End of If structure]
7. If LOC+1<END then
TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END
[End of If Structure]
[End of Step 3 loop]
8. Exit
40
Radix Sort
Radix Sort
Radix Sort (or Bucket Sort)
 It is a non comparative integer sorting
algorithm
 It sorts data with integer keys by grouping
keys by the individual digits which share the
same significant position and value.
 A positional notation is required, but because
integers can represent strings of characters
(e.g., names or dates) and specially
formatted floating point numbers, Radix sort
is not limited to integers.
Radix Sort Algorithm
Let A be an array with N elements in the memory
 Find the largest element of the array
 Find the total no. of digits ‘NUM’ in the largest
element
 Repeat Steps 4, 5 for PASS = 1 to NUM
 Initialize buckets (0 to 9 for numbers, A to Z for
Characters)
For i = 0 to (N-1)
Set DIGIT = Obtain digit number PASS of A [i]
Put A [i] in bucket number DIGIT
End of for loop
5. Collect all numbers from the bucket in order
6. Exit
45

More Related Content

Similar to Quick sort,bubble sort,heap sort and merge sort (20)

Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
ebinazer1
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
VinayNassa3
 
Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
AlugatiRajitha
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
arnold 7490
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
ankita946617
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Data Structure - Lecture 1 - Introduction.pdf
Data Structure  - Lecture 1 - Introduction.pdfData Structure  - Lecture 1 - Introduction.pdf
Data Structure - Lecture 1 - Introduction.pdf
donotreply20
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
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
RUHULAMINHAZARIKA
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
Kumar
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
Nurjahan Nipa
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
ebinazer1
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
VinayNassa3
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
pppepito86
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
ankita946617
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Data Structure - Lecture 1 - Introduction.pdf
Data Structure  - Lecture 1 - Introduction.pdfData Structure  - Lecture 1 - Introduction.pdf
Data Structure - Lecture 1 - Introduction.pdf
donotreply20
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
Sreedhar Chowdam
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
Tri Merge Sorting Algorithm
Tri Merge Sorting AlgorithmTri Merge Sorting Algorithm
Tri Merge Sorting Algorithm
Ashim Sikder
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
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
RUHULAMINHAZARIKA
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
Kumar
 

Recently uploaded (20)

Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdfParticle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
DUSABEMARIYA
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITSDE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
Sridhar191373
 
Supplier_PFMEA_Workshop_rev 22_04_27.pptx
Supplier_PFMEA_Workshop_rev 22_04_27.pptxSupplier_PFMEA_Workshop_rev 22_04_27.pptx
Supplier_PFMEA_Workshop_rev 22_04_27.pptx
dariojaen1977
 
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
[HIFLUX] Lok Fitting&Valve Catalog 2025 (Eng)
하이플럭스 / HIFLUX Co., Ltd.
 
Salesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for EveryoneSalesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for Everyone
ImtiazBinMohiuddin
 
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
VikasNirgude2
 
Dr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivu__Machine Learning-Module 3.pdfDr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivashankar
 
Introduction-to-Prestressed-Concrete.pdf
Introduction-to-Prestressed-Concrete.pdfIntroduction-to-Prestressed-Concrete.pdf
Introduction-to-Prestressed-Concrete.pdf
Bharti Shinde
 
2. CT M35 Grade Concrete Mix design ppt.pdf
2. CT M35 Grade Concrete Mix design  ppt.pdf2. CT M35 Grade Concrete Mix design  ppt.pdf
2. CT M35 Grade Concrete Mix design ppt.pdf
smghumare
 
1.2 Need of Object-Oriented Programming.pdf
1.2 Need of Object-Oriented Programming.pdf1.2 Need of Object-Oriented Programming.pdf
1.2 Need of Object-Oriented Programming.pdf
VikasNirgude2
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
Mathias Magdowski
 
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDINGMODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
Attenuation Models for Estimation of Vertical Peak Ground Acceleration Based ...
Attenuation Models for Estimation of Vertical Peak Ground Acceleration Based ...Attenuation Models for Estimation of Vertical Peak Ground Acceleration Based ...
Attenuation Models for Estimation of Vertical Peak Ground Acceleration Based ...
Journal of Soft Computing in Civil Engineering
 
HVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdfHVAC Air Filter Equipment-Catalouge-Final.pdf
HVAC Air Filter Equipment-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Unit 6 Message Digest Message Digest Message Digest
Unit 6  Message Digest  Message Digest  Message DigestUnit 6  Message Digest  Message Digest  Message Digest
Unit 6 Message Digest Message Digest Message Digest
ChatanBawankar
 
Department of Environment (DOE) Mix Design with Fly Ash.
Department of Environment (DOE) Mix Design with Fly Ash.Department of Environment (DOE) Mix Design with Fly Ash.
Department of Environment (DOE) Mix Design with Fly Ash.
MdManikurRahman
 
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITSDIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
Sridhar191373
 
Air Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdfAir Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdfParticle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
Particle Swarm Optimization by Aleksandar Lazinica (Editor) (z-lib.org).pdf
DUSABEMARIYA
 
world subdivision.pdf...................
world subdivision.pdf...................world subdivision.pdf...................
world subdivision.pdf...................
bmmederos10
 
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITSDE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
DE-UNIT-V MEMORY DEVICES AND DIGITAL INTEGRATED CIRCUITS
Sridhar191373
 
Supplier_PFMEA_Workshop_rev 22_04_27.pptx
Supplier_PFMEA_Workshop_rev 22_04_27.pptxSupplier_PFMEA_Workshop_rev 22_04_27.pptx
Supplier_PFMEA_Workshop_rev 22_04_27.pptx
dariojaen1977
 
Salesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for EveryoneSalesforce Hackathon Fun Slide for Everyone
Salesforce Hackathon Fun Slide for Everyone
ImtiazBinMohiuddin
 
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
1.9 Class,Object,Class Scope,Accessing Class members and Controlling access t...
VikasNirgude2
 
Dr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivu__Machine Learning-Module 3.pdfDr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivu__Machine Learning-Module 3.pdf
Dr. Shivashankar
 
Introduction-to-Prestressed-Concrete.pdf
Introduction-to-Prestressed-Concrete.pdfIntroduction-to-Prestressed-Concrete.pdf
Introduction-to-Prestressed-Concrete.pdf
Bharti Shinde
 
2. CT M35 Grade Concrete Mix design ppt.pdf
2. CT M35 Grade Concrete Mix design  ppt.pdf2. CT M35 Grade Concrete Mix design  ppt.pdf
2. CT M35 Grade Concrete Mix design ppt.pdf
smghumare
 
1.2 Need of Object-Oriented Programming.pdf
1.2 Need of Object-Oriented Programming.pdf1.2 Need of Object-Oriented Programming.pdf
1.2 Need of Object-Oriented Programming.pdf
VikasNirgude2
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
Mathias Magdowski
 
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDINGMODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
MODULE 5 BUILDING PLANNING AND DESIGN SY BTECH ACOUSTICS SYSTEM IN BUILDING
Dr. BASWESHWAR JIRWANKAR
 
Unit 6 Message Digest Message Digest Message Digest
Unit 6  Message Digest  Message Digest  Message DigestUnit 6  Message Digest  Message Digest  Message Digest
Unit 6 Message Digest Message Digest Message Digest
ChatanBawankar
 
Department of Environment (DOE) Mix Design with Fly Ash.
Department of Environment (DOE) Mix Design with Fly Ash.Department of Environment (DOE) Mix Design with Fly Ash.
Department of Environment (DOE) Mix Design with Fly Ash.
MdManikurRahman
 
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITSDIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
DIGITAL ELECTRONICS: UNIT-III SYNCHRONOUS SEQUENTIAL CIRCUITS
Sridhar191373
 

Quick sort,bubble sort,heap sort and merge sort

  • 2. Searching Internal Search The search in which the whole list resides in the main memory is called internal search. External Search The search in which the whole list resides in the Secondary memory is called external search.
  • 3. Types of searching Algorithm  Linear or Sequential searching  Binary searching
  • 4. Algorithm For Linear Search LINEAR(DATA ,N,ITEM,LOC) 1.Set K:=1 and LOC := 0. 2.Repeat Steps 3 and 4 while LOC = 0 and K<=N. 3.If ITEM = DATA[K] ,then Set LOC:= K. 4.Set K:=K+1. 5.If LOC = 0,then: Write: ITEM is not in the array DATA. Else: Write: LOC is the location of ITEM. 6.Exit.
  • 5. Advantages  Not Expensive.  List/Array may or may not be sorted. Disadvantages  Less Efficient.  Time consuming.
  • 6. Algorithm For Binary Search BINARY(DATA , LB , UB, ITEM , LOC) 1. Set BEG:= LB, END:=UB and MID = INT((BEG+END)/2). 2. Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM. 3. If ITEM< DATA[MID] ,then: Set END := MID-1. Else: Set BEG := MID+1. 4. Set MID := INT((BEG+END)/2).
  • 7. Algo. Cont. 5. If DATA[MID]=ITEM, then: Set LOC:= MID. Else: Set LOC:= NULL. 6. Exit.
  • 8. Advantages  Extremely Efficient  Less Time Consuming Disadvantages  Expensive  List/Array may be sorted.
  • 9. Introduction to Sorting  Sorting: An operation that arranges items into groups according to specified criterion. A = { 3 1 6 2 1 3 4 5 9 0 } A = { 0 1 1 2 3 3 4 5 6 9 }
  • 10. Why Sort and Examples Consider:  Sorting Books in Library (Dewey system)  Sorting Individuals by Height (Feet and Inches)  Sorting Movies in Blockbuster (Alphabetical)  Sorting Numbers (Sequential)
  • 11. Types of Sorting Algorithms There are many different types of sorting algorithms, but the primary ones are:  Bubble Sort  Selection Sort  Insertion Sort  Merge Sort  Quick Sort  Radix Sort  Heap Sort
  • 12. Complexity  Most of the primary sorting algorithms run on different space and time complexity.  Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case).  Space complexity is defined to be the amount of memory the computer needs to run a program.
  • 13. Complexity (cont.) Complexity in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm. External Factors (not related to complexity): Size of the input of the algorithm Speed of the Computer Quality of the Compiler
  • 14. O(n), Ω(n), & Θ(n)  An algorithm or function T(n) is O(f(n)) whenever T(n)'s rate of growth is less than or equal to f(n)'s rate.  An algorithm or function T(n) is Ω(f(n)) whenever T(n)'s rate of growth is greater than or equal to f(n)'s rate.  An algorithm or function T(n) is Θ(f(n)) if and only if the rate of growth of T(n) is equal to f(n).
  • 15. 1 0 Common Big-Oh’s Time complexity Example O(1) constant Adding to the front of a linked list O(log N) log Finding an entry in a sorted array O(N) linear Finding an entry in an unsorted array O(N log N) n-log-n Sorting n items by ‘divide-and-conquer’ O(N2 ) quadratic Shortest path between two nodes in a graph O(N3 ) cubic Simultaneous linear equations (Binary) Finding 8: 9 50 22 21 8 5 1 (Linear) Finding 8: 9 50 22 21 8 5 1 Front Initial: Final:6 3 http://www.cs.sjsu.edu/faculty/lee/cs146/23FL3Complexity.ppt
  • 16. Big-Oh to Primary Sorts ● Bubble Sort = n² ● Selection Sort = n² ● Insertion Sort = n² ● Merge Sort = n log(n) ●Quick Sort = n log(n)
  • 17. Time Efficiency  How do we improve the time efficiency of a program?  The 90/10 Rule  90% of the execution time of a program is spent in  executing 10% of the code  So, how do we locate the critical 10%?  software metrics tools  global counters to locate bottlenecks (loop executions, function calls)
  • 18. Time Efficiency Improvements  Possibilities (some better than others!)  Move code out of loops that does not belong there (just good programming!)  Remove any unnecessary I/O operations (I/O operations are expensive time-wise)  Code so that the compiled code is more efficient Moral - Choose the most appropriate algorithm(s) BEFORE program implementation
  • 19. Selection Sort Min & Loc get swapped
  • 20. Selection Sort MIN(A,K,N,LOC) 1. Set MIN = A[K] and LOC = K 2. Repeat for J=K+1, K+2 ……… N If MIN>A[J], then Set MIN = A[J] and LOC = J End of Loop 3. Return
  • 21. Selection Sort SELECTION (A,N) 1. Repeat steps 2 and 3 for K=1, 2 ……… N-1 2. Call MIN(A,K,N,LOC) 3. Set TEMP = A[K] A[K] = A[LOC] A[LOC] = TEMP End of Step 1 Loop 4. Exit
  • 22. Complexity of the Selection Sort  Worst Case  O(n2)  Average Case  O(n2)
  • 24. Insertion Sort Element get compared by previous no.s
  • 25. Insertion Sort INSERTION (A,N) 1. Set A[0] = - ∞ 2. Repeat Steps 3 to 5 for K = 2,3,….N 3. Set TEMP = A[K] and PTR = K-1 4. Repeat while TEMP<A[PTR] 1. Set A[PTR+1] = A[PTR] 2. Set PTR = PTR – 1 End of Loop 5. Set A[PTR+1] = TEMP End of Step 2 Loop 6. Return
  • 26. Complexity of the Insertion Sort  Worst Case  O(n2)  Average Case  O(n2)
  • 28. Algorithm for BUBBLE SORT BUBBLE(DATA , N) 1.Repeat Steps 2 and 3 for K=1 to N-1. 2.Set PTR := 1. // Initialize pass pointer PTR 3.Repeat while PTR<=N-K. a) If DATA[PTR]>DATA[PTR+1],then Interchange DATA[PTR] and DATA[PTR+1] b) Set PTR:= PTR+1 4. Exit.
  • 33. MERGE SORT MERGE_SORT (A, BEG, END) 1. IF BEG<ENG SET MID=(BEG+END)/2 CALL MERGE_SORT (A, BEG, MID) CALL MERGE_SORT (A, MID+1, END) MERGE(A, BEG, MID, END) 2. END
  • 34. MERGE(A, BEG, MID, END) 1. SET I = BEG, J=MID+1, INDEX=0 2. REPEAT WHILE (I<=MID) AND (J<=END) IF A[I]<A[J], THEN SET TEMP [INDEX] = A[I] SET I=I+1 ELSE SET TEMP [INDEX]=A[J] SET J=J+1 SET INDEX = INDEX +1
  • 35. 3. IF I>MID THEN REPEAT WHILE J<=END SET TEMP [INDEX]=A[J] SET INDEX=INDEX+1 SET J=J+1 ELSE REPEAT WHILE I<=MID SET TEMP[INDEX]=A[I] SET INDEX = INDEX +1 SET I=I+1
  • 36. 4. SET K=0 5. REPEAT WHILE K<INDEX SET A[K]=TEMP[K] SET K=K+1 6. END
  • 37. QUICKSORT Starting with first element of list do the operations 1. Compare from right to find element less than selected and interchange 2. Compare from left to find element greater than selected and interchange 44,33,11,55,77,90,40,60,33,22,88,66 22,33,11,55,77,90,40,60,99,44,88,66 22,33,11,44,77,90,40,60,99,55,88,66 22,33,11,40,77,90,44,60,99,55,88,66 22,33,11,40,44,90,77,60,99,55,88,66 22,33,11,40,44,90,77,60,99,55,88,66 Two Sublists are created: Before 44 and After 44
  • 38. 38 Quick Sort Algorithm QUICK (A, N, BEG, END, LOC) 1. [Initialize] Set LEFT= BEG, RIGHT=END and LOC=BEG 2. [Scan from right to left] a) Repeat while A[LOC]<=A[RIGHT] and LOC != RIGHT RIGHT=RIGHT-1 [End of loop] b) If LOC=RIGHT then : Return c) If A[LOC]>A[RIGHT] then i) Interchange A[LOC] and A[RIGHT] ii) Set LOC=RIGHT iii) Go to step 3 [End of If structure]
  • 39. 3. [Scan from left to right] a) Repeat while A[LEFT]<=A[LOC] and LEFT!=LOC LEFT=LEFT+1 [End of loop] b) If LOC=LEFT, then Return c) If A[LEFT]>A[LOC], then i) Interchange A[LEFT] and A[LOC] ii) Set LOC=LEFT iii) Goto step 2 [End of If structure] 39
  • 40. (QuickSort) This algorithm sorts an array A with N elements. 1. TOP=NULL 2. If N>1 then TOP=TOP+1, LOWER[1]=1, UPPER[1]=N 3. Repeat Steps 4 to 7 while TOP!=NULL 4. Set BEG=LOWER[TOP], END=UPPER[TOP] TOP=TOP-1 5. Call QUICK(A, N, BEG, END, LOC) 6. If BEG<LOC-1 then TOP=TOP+1, LOWER[TOP]=BEG, UPPER[TOP]=LOC-1 [End of If structure] 7. If LOC+1<END then TOP=TOP+1, LOWER[TOP]=LOC+1, UPPER[TOP]=END [End of If Structure] [End of Step 3 loop] 8. Exit 40
  • 43. Radix Sort (or Bucket Sort)  It is a non comparative integer sorting algorithm  It sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value.  A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, Radix sort is not limited to integers.
  • 44. Radix Sort Algorithm Let A be an array with N elements in the memory  Find the largest element of the array  Find the total no. of digits ‘NUM’ in the largest element  Repeat Steps 4, 5 for PASS = 1 to NUM  Initialize buckets (0 to 9 for numbers, A to Z for Characters) For i = 0 to (N-1) Set DIGIT = Obtain digit number PASS of A [i] Put A [i] in bucket number DIGIT End of for loop 5. Collect all numbers from the bucket in order 6. Exit
  • 45. 45