SlideShare une entreprise Scribd logo
1  sur  26
- PINAK PATEL
Sorting Algorithms
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 in general, measures the algorithms
efficiency in internal factors such as the time needed
to run an algorithm.
Big O Analysis
 When solving a computer science problem there
will usually be more than just one solution.
 These solution often be in the form of different
algorithms.
 Big O analysis will help to measure the efficiency
of the algorithm.
 Big O notation is used to classify algorithms by
how they respond to changes in input size in
terms of time and space.
 Big O only provides the upper bound on the
growth rate of the function.
Bubble sort
 Explanation:
 Compare each element (except last one) with its neighbour to the
right
 If they are out of order, swap them
 This puts the largest element at the very end
 The last element is now in the correct and final phase
 Compare each element (except last one) with its neighbour to the
right
 If they are out of order, swap them
 This puts the second largest element next to last
 The last two elements are now in their correct and final phase
 Compare each element (except last one) with its neighbour to the
right
Bubble sort
 Example:
 7, 2, 8, 5, 4 2, 7, 5, 4, 8 2, 5,
4, 7, 8
 2, 7, 8, 5, 4 2, 5, 7, 4, 8 2, 4,
5, 7, 8
 2, 7, 5, 8, 4 2, 5, 4, 7, 8 done
 2, 7, 5, 4, 8
Algorithm
Bubble_Sort(int[] a)
{
n=length of a
for i=0 to n-1
{
for j=0 to n-i
{
If(a[j] > a[j+1])
{
temp=a[J];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
Analysis
 Running time:
 Worst case: O(N2)
 Best case: O(N)
Selection sort
 Explanation:
 Given an array of length n;
 Search element 0 through n-1 and select the smallest
 Swap it with the element at location 0;
 Search element 1 through n-1 and select the smallest
 Swap it with the element at location 1;
 Search element 2 through n-1 and select the smallest
 Swap it with the element at location 2;
 Continue until there is nothing left to search.
Selection sort
 Example:
 7, 2, 8, 5, 4
 2, 7, 8, 5, 4
 2, 4, 8, 5, 7
 2, 4, 5, 8, 7
 2, 4, 5, 7, 8
Algorithm
selectionSort(int[] a)
{
n=length(a)
for i=0 to n-1
{
min=i;
for j=i+1 to n
{
if(a[j] < a[min])
{
min=j;
}
}
temp=a[outer];
a[outer]=a[min];
a[min]=temp;
}
}
Analysis
 The outer loop executes n-1 time
 The inner loop executes n/2 times on average.
 Time required is roughly (n-1)*(n/2)
 Time complexity = O(n^2)
Insertion sort
Explanation:
 The basic idea of Insertion Sort algorithm can be described as these
steps:
1. Data elements are grouped into two sections:
a sorted section and an un-sorted section.
2. Take an element from the un-sorted section.
3. Insert the element into the sorted section at the correct position
based on the comparable property.
4. Repeat step 2 and 3 until no more elements left in the un-sorted
section.
 Adding a new element to a sorted list will keep the list sorted if the
element is inserted in the correct place
 A single element list is sorted
 Inserting a second element in the proper place keeps the list sorted
 This is repeated until all the elements have been inserted into the
Insertion sort
Algorithm:
insertionSort(A)
{
n=len(A)
For i=2 to n
Key=a[i];
j=i-1;
While(j>0 && a[j]>key)
{
A[j+1]=a[j];
j=j-1;
}
A[j+1]=key;
}
Analysis
Best case: If A is sorted: O (n)
Worst case:
- The outer loop is always done N – 1 times
- The inner loop does the most work when the next
element is smaller than all of the past elements
- On each pass, the next element is compared to all
earlier elements, giving:
If A is reversed sorted: O (n^2)
Average case: O (n^2)
Merge Sort
 Explanation:
 Merge Sort is a complex and fast sorting algorithm that repeatedly
divides an un-sorted section into two equal sub-sections, sorts them
separately and merges them correctly.
 The basic idea of Merge Sort algorithm can be described as these
steps:
1. Divide the data elements into two sections with equal number of
elements.
2. Sort the two sections separately.
3. Merge the two sorted sections into a single sorted collection.
 Obviously, this is a recursive idea, where a problem is divided into
smaller problems. And the division will be repeated to make the
smaller problems even smaller, until they are smaller enough so that
the solutions are obvious.
Algorithm
MergeSort(A,low,high)
{
If(low<high)
{
Mid=(low+high)/2;
MergeSort(A,low,mid);
MergeSort(A,mid,high);
Merge(A,low,mid,high);
}
}
merge(int arr[],int min,int mid,int max)
{
int tmp[30];
int i,j,k,m;
j=min;
m=mid+1;
for(i=min; j<=mid && m<=max ; i++)
{
if(arr[j]<=arr[m])
{
tmp[i]=arr[j];
j++;
}
else
{
tmp[i]=arr[m];
m++;
}
}
if(j>mid)
{
for(k=m; k<=max; k++)
{
tmp[i]=arr[k];
i++;
}
}
Else
{
for(k=j; k<=mid; k++)
{
tmp[i]=arr[k];
i++;
}
}
for(k=min; k<=max; k++)
{
arr[k]=tmp[k];
}
Analysis
 1. T(1)=1
 2. T(N)= 2 T(N/2) + N
 Time to mergesort two arrays each N/2 elements + time
to merge two arrays each n/2 elements
3. Next we will solve this recurrence relation
Firsy we Divide by N
T(N)/N = T(N/2)/(N/2) + 1
Now,
T(N/2)/(N/2) = T(N/4)/(N/4) + 1 ………… = log N
T(N)= N + N log N
 O(n log n)
Quick Sort
 Explanation:
 Divide and conquer algorithm
 It is divided in three parts
 Elements less than pivot element
 Pivot element
 Elements greater than pivot elements
 Where pivot as the middle element of the large list
 Example:
List= 3 7 8 5 2 1 9 5 4
 Assume 4 as a pivot element, so rewrite list as:
 3 1 2 4 5 8 9 5 7
 So element at the left side of pivot element is less than pivot element
and at the right side greater.
 Recursively sort the sub-list of lesser elements and the sublist of
greater element
Quick Sort
quick_sort(int a[],int low,int high)
{
if(low<high) then
j=partition(a,low,high+1); //position of the pivot
index
quick_sort(a,low,j-1);
quick_sort(a,j+1,high);
}
}
int partition(int a[],int low,int high)
{
int pivot=a[low],i=low, j=high;
while(i<j){
do { i++;
}while(a[i]<pivot);
do{
j--;
} while(a[j]>pivot);
if(i<j){
interchange(a,i,j);
}
}
a[low]=a[j];
a[j]=pivot;
return j;
}
void interchange(int a[20],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Analysis
 The pivot is in the middle
 T(N) = 2 T(N/2) + cN
 T(N)/N = T(N/2)/(N/2) + c
 T(N)= N + N log N = O ( N log N)
THANK YOU

Contenu connexe

Tendances

TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Asymptotic Analysis
Asymptotic AnalysisAsymptotic Analysis
Asymptotic Analysissonugupta
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysisOnkar Nath Sharma
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingTiểu Hổ
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and SortingMuhammadBakri13
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 

Tendances (20)

TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Asymptotic Analysis
Asymptotic AnalysisAsymptotic Analysis
Asymptotic Analysis
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 
01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis01. design & analysis of agorithm intro & complexity analysis
01. design & analysis of agorithm intro & complexity analysis
 
Big o notation
Big o notationBig o notation
Big o notation
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Sorting
SortingSorting
Sorting
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Chapter 14 Searching and Sorting
Chapter 14 Searching and SortingChapter 14 Searching and Sorting
Chapter 14 Searching and Sorting
 
Quicksort
QuicksortQuicksort
Quicksort
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 

En vedette

Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian RevolutionJohn Lynch
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"John Lynch
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2mtaft
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bShahi Raz Akhtar
 
One Long Argument
One Long ArgumentOne Long Argument
One Long ArgumentJohn Lynch
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Andreas Jaffke
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical RevolutionJohn Lynch
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionJohn Lynch
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Saba SEO
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceJohn Lynch
 

En vedette (20)

Was There A Darwinian Revolution
Was There A Darwinian RevolutionWas There A Darwinian Revolution
Was There A Darwinian Revolution
 
Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"Introduction to "Origins, Evolution & Creation"
Introduction to "Origins, Evolution & Creation"
 
Dc parent 14 2
Dc parent 14 2Dc parent 14 2
Dc parent 14 2
 
Introduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_bIntroduction to Information Technology ch 01_b
Introduction to Information Technology ch 01_b
 
Chap04alg
Chap04algChap04alg
Chap04alg
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
One Long Argument
One Long ArgumentOne Long Argument
One Long Argument
 
Google at a glance 1998 - 2008
Google at a glance 1998 - 2008Google at a glance 1998 - 2008
Google at a glance 1998 - 2008
 
Chapter one
Chapter oneChapter one
Chapter one
 
The Chemical Revolution
The Chemical RevolutionThe Chemical Revolution
The Chemical Revolution
 
Google
GoogleGoogle
Google
 
Ancient Ideas of Creation & Evolution
Ancient Ideas of Creation & EvolutionAncient Ideas of Creation & Evolution
Ancient Ideas of Creation & Evolution
 
Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.Google Algorithm Change History - 2k14-2k16.
Google Algorithm Change History - 2k14-2k16.
 
sPen Data Management
sPen Data ManagementsPen Data Management
sPen Data Management
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
CSS 3, Style and Beyond
CSS 3, Style and BeyondCSS 3, Style and Beyond
CSS 3, Style and Beyond
 
Ds 4
Ds 4Ds 4
Ds 4
 
Algorithms - Aaron Bloomfield
Algorithms - Aaron BloomfieldAlgorithms - Aaron Bloomfield
Algorithms - Aaron Bloomfield
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Why Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & ScienceWhy Ben Stein Is Wrong About History & Science
Why Ben Stein Is Wrong About History & Science
 

Similaire à Sorting pnk

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
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmEhsan Ehrari
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sortijfcstjournal
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniquesDharmendra Prasad
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 

Similaire à Sorting pnk (20)

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
 
L1803016468
L1803016468L1803016468
L1803016468
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
my docoment
my docomentmy docoment
my docoment
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Selection sort
Selection sortSelection sort
Selection sort
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Design and analysis of ra sort
Design and analysis of ra sortDesign and analysis of ra sort
Design and analysis of ra sort
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Lecture 2 data structures & algorithms - sorting techniques
Lecture 2  data structures & algorithms - sorting techniquesLecture 2  data structures & algorithms - sorting techniques
Lecture 2 data structures & algorithms - sorting techniques
 
Ada notes
Ada notesAda notes
Ada notes
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Sorting
SortingSorting
Sorting
 
lecture 1
lecture 1lecture 1
lecture 1
 
Lect-2.pptx
Lect-2.pptxLect-2.pptx
Lect-2.pptx
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 

Dernier

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
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
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 

Dernier (20)

Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
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
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 

Sorting pnk

  • 2. 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 in general, measures the algorithms efficiency in internal factors such as the time needed to run an algorithm.
  • 3. Big O Analysis  When solving a computer science problem there will usually be more than just one solution.  These solution often be in the form of different algorithms.  Big O analysis will help to measure the efficiency of the algorithm.  Big O notation is used to classify algorithms by how they respond to changes in input size in terms of time and space.  Big O only provides the upper bound on the growth rate of the function.
  • 4. Bubble sort  Explanation:  Compare each element (except last one) with its neighbour to the right  If they are out of order, swap them  This puts the largest element at the very end  The last element is now in the correct and final phase  Compare each element (except last one) with its neighbour to the right  If they are out of order, swap them  This puts the second largest element next to last  The last two elements are now in their correct and final phase  Compare each element (except last one) with its neighbour to the right
  • 5. Bubble sort  Example:  7, 2, 8, 5, 4 2, 7, 5, 4, 8 2, 5, 4, 7, 8  2, 7, 8, 5, 4 2, 5, 7, 4, 8 2, 4, 5, 7, 8  2, 7, 5, 8, 4 2, 5, 4, 7, 8 done  2, 7, 5, 4, 8
  • 6. Algorithm Bubble_Sort(int[] a) { n=length of a for i=0 to n-1 { for j=0 to n-i { If(a[j] > a[j+1]) { temp=a[J]; a[j]=a[j+1]; a[j+1]=temp; } } } }
  • 7. Analysis  Running time:  Worst case: O(N2)  Best case: O(N)
  • 8. Selection sort  Explanation:  Given an array of length n;  Search element 0 through n-1 and select the smallest  Swap it with the element at location 0;  Search element 1 through n-1 and select the smallest  Swap it with the element at location 1;  Search element 2 through n-1 and select the smallest  Swap it with the element at location 2;  Continue until there is nothing left to search.
  • 9. Selection sort  Example:  7, 2, 8, 5, 4  2, 7, 8, 5, 4  2, 4, 8, 5, 7  2, 4, 5, 8, 7  2, 4, 5, 7, 8
  • 10. Algorithm selectionSort(int[] a) { n=length(a) for i=0 to n-1 { min=i; for j=i+1 to n { if(a[j] < a[min]) { min=j; } } temp=a[outer]; a[outer]=a[min]; a[min]=temp; } }
  • 11. Analysis  The outer loop executes n-1 time  The inner loop executes n/2 times on average.  Time required is roughly (n-1)*(n/2)  Time complexity = O(n^2)
  • 12. Insertion sort Explanation:  The basic idea of Insertion Sort algorithm can be described as these steps: 1. Data elements are grouped into two sections: a sorted section and an un-sorted section. 2. Take an element from the un-sorted section. 3. Insert the element into the sorted section at the correct position based on the comparable property. 4. Repeat step 2 and 3 until no more elements left in the un-sorted section.  Adding a new element to a sorted list will keep the list sorted if the element is inserted in the correct place  A single element list is sorted  Inserting a second element in the proper place keeps the list sorted  This is repeated until all the elements have been inserted into the
  • 14. Algorithm: insertionSort(A) { n=len(A) For i=2 to n Key=a[i]; j=i-1; While(j>0 && a[j]>key) { A[j+1]=a[j]; j=j-1; } A[j+1]=key; }
  • 15. Analysis Best case: If A is sorted: O (n) Worst case: - The outer loop is always done N – 1 times - The inner loop does the most work when the next element is smaller than all of the past elements - On each pass, the next element is compared to all earlier elements, giving: If A is reversed sorted: O (n^2) Average case: O (n^2)
  • 16. Merge Sort  Explanation:  Merge Sort is a complex and fast sorting algorithm that repeatedly divides an un-sorted section into two equal sub-sections, sorts them separately and merges them correctly.  The basic idea of Merge Sort algorithm can be described as these steps: 1. Divide the data elements into two sections with equal number of elements. 2. Sort the two sections separately. 3. Merge the two sorted sections into a single sorted collection.  Obviously, this is a recursive idea, where a problem is divided into smaller problems. And the division will be repeated to make the smaller problems even smaller, until they are smaller enough so that the solutions are obvious.
  • 18. merge(int arr[],int min,int mid,int max) { int tmp[30]; int i,j,k,m; j=min; m=mid+1; for(i=min; j<=mid && m<=max ; i++) { if(arr[j]<=arr[m]) { tmp[i]=arr[j]; j++; } else { tmp[i]=arr[m]; m++; } }
  • 19. if(j>mid) { for(k=m; k<=max; k++) { tmp[i]=arr[k]; i++; } } Else { for(k=j; k<=mid; k++) { tmp[i]=arr[k]; i++; } } for(k=min; k<=max; k++) { arr[k]=tmp[k]; }
  • 20. Analysis  1. T(1)=1  2. T(N)= 2 T(N/2) + N  Time to mergesort two arrays each N/2 elements + time to merge two arrays each n/2 elements 3. Next we will solve this recurrence relation Firsy we Divide by N T(N)/N = T(N/2)/(N/2) + 1 Now, T(N/2)/(N/2) = T(N/4)/(N/4) + 1 ………… = log N T(N)= N + N log N  O(n log n)
  • 21. Quick Sort  Explanation:  Divide and conquer algorithm  It is divided in three parts  Elements less than pivot element  Pivot element  Elements greater than pivot elements  Where pivot as the middle element of the large list  Example: List= 3 7 8 5 2 1 9 5 4  Assume 4 as a pivot element, so rewrite list as:  3 1 2 4 5 8 9 5 7  So element at the left side of pivot element is less than pivot element and at the right side greater.  Recursively sort the sub-list of lesser elements and the sublist of greater element
  • 22. Quick Sort quick_sort(int a[],int low,int high) { if(low<high) then j=partition(a,low,high+1); //position of the pivot index quick_sort(a,low,j-1); quick_sort(a,j+1,high); } }
  • 23. int partition(int a[],int low,int high) { int pivot=a[low],i=low, j=high; while(i<j){ do { i++; }while(a[i]<pivot); do{ j--; } while(a[j]>pivot); if(i<j){ interchange(a,i,j); } } a[low]=a[j]; a[j]=pivot; return j; }
  • 24. void interchange(int a[20],int i,int j) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; }
  • 25. Analysis  The pivot is in the middle  T(N) = 2 T(N/2) + cN  T(N)/N = T(N/2)/(N/2) + c  T(N)= N + N log N = O ( N log N)