SlideShare a Scribd company logo
1 of 26
CHAPTER-2
Simple Sorting and
Searching Algorithms
CHAPTER OVERVIEW
 Simple Sorting algorithms
o Selection sort
o Bubble sort
o Insertion sort
 Simple searching algorithms
o Linear/sequential searching
o Binary searching
2
20 October 2023
SORTING ALGORITHMS
 Sorting
o process of reordering a list of items in either increasing or
decreasing order.
o efficiency of sorting algorithm is measured using
o the number of comparisons and
o the number of data movements made by the algorithms.
o Sorting algorithms are categorized as:
o simple/elementary and
o advanced.
o Simple sorting algorithms, like Selection sort, Bubble sort and
Insertion sort, are only used to sort small-sized list of items.
3
20 October 2023
1. SELECTION SORT
 Given an array of length n,
 Search elements 0 through n-1 and select the smallest
 Swap it with the element in location 0
 Search elements 1 through n-1 and select the smallest
 Swap it with the element in location 1
 Search elements 2 through n-1 and select the smallest
 Swap it with the element in location 2
 Search elements 3 through n-1 and select the smallest
 Swap it with the element in location 3
 Continue in this fashion until there’s nothing left to
search
4
20 October 2023
SELECTION SORT …CONT’D
 i.e. the basic idea is
 Loop through the array from i=0 to n-1.
 Select the smallest element in the array from i to n
 Swap this value with value at position i.
 we repeatedly find the next largest (or smallest)
element in the array and move it to its final position
in the sorted array.
 Note: the list/array is divided into two parts:
 the sub-list of items already sorted and
 the sub-list of items remaining to be sorted
5
20 October 2023
SELECTION SORT EXAMPLE …CONT’D
 Analysis:
 The outer loop executes n-1 times
 The inner loop executes about n(n-1)/2
times on average (from n to 2 times)
 Work done in the inner loop is constant
(swap two array elements)
 Time required is roughly (n-1)*[n(n-1)/2]
 You should recognize this as O(n2)
 i.e.
 How many comparisons?
 (n-1)+(n-2)+…+1 = O(n2)
 How many swaps?
 n = O(n)
6
20 October 2023
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
CODE FOR SELECTION SORT …CONT’D
void selectionSort(int[] a)
{
int outer, inner, min;
for (outer = 0; outer < a.length - 1; outer++)
{
min = outer;
for (inner = outer + 1; inner < a.length; inner++)
{
if (a[inner] < a[min])
{
min = inner;
}
}
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
}
}//end of function
7
20 October 2023
2. BUBBLE SORT
 Also called Exchange sort
 simplest algorithm to implement and the slowest
algorithm on very large inputs.
 Basic Idea:
 Loop through array from i=0 to n and swap adjacent
elements if they are out of order.
 repeatedly compares adjacent elements of an array.
 Compare each element (except the last one) with its neighbor
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 place
 Compare each element (except the last two) with its neighbor
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 places
 Compare each element (except the last three) with its
neighbor to the right
 Continue as above until you have no unsorted elements on the left 8
20 October 2023
BUBBLE SORT EXAMPLE ...CONT’D
9
20 October 2023
2 7 5 4 8
7 2 8 5 4
2 7 8 5 4
2 7 8 5 4
2 7 5 8 4
2 7 5 4 8
2 5 7 4 8
2 5 4 7 8
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 5 4 7 8
2 4 5 7 8
2 4 5 7 8
CODE FOR BUBBLE SORT ...CONT’D
void bubbleSort(int[] a)
{
int outer, inner;
for (outer = a.length - 1; outer > 0; outer--) //counts down
{
for (inner = 0; inner < outer; inner++)
{
if (a[inner] > a[inner + 1])
{
int temp = a[inner];
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
}
10
20 October 2023
ANALYSIS FOR BUBBLE SORT ...CONT’D
 Let n = a.length = size of the array
 The outer loop is executed n-1 times
 Each time the outer loop is executed, the inner loop is executed
 Inner loop executes n-1 times at first, linearly dropping to just
once
 On average, inner loop executes about n(n-1)/2 times for
each execution of the outer loop
 In the inner loop, the comparison is always done (constant
time), the swap might be done (also constant time)
 Result is (n-1) * [ n(n-1)/2 ] + k, that is, O(n2)
 i.e.
 How many comparisons?
 (n-1)+(n-2)+…+1= O(n2)
 How many swaps?
 (n-1)+(n-2)+…+1= O(n2)
11
20 October 2023
3. INSERTION SORT
 it inserts each item into its proper place in the final list.
 The simplest implementation of this requires two list
structures – the source list and the list into which sorted
items are inserted.
 Basic Idea:
 Find the location for an element and move all others up, and
insert the element.
 The approach is the same approach that we use for
sorting a set of cards in our hand.
 While playing cards, we pick up a card, start at the
beginning of our hand and find the place to insert the
new card, insert it and move all the others up one place.
12
20 October 2023
INSERTION SORT ...CONT’D
 The algorithm is as follows
1. The left most value can be said to be sorted relative to
itself. Thus, we don’t need to do anything.
2. Check to see if the second value is smaller than the
first one. If it is, swap these two values. The first two
values are now relatively sorted.
3. Next, we need to insert the third value in to the relatively
sorted portion so that after insertion, the portion will still
be relatively sorted.
4. Remove the third value first. Slide the second value to
make room for insertion. Insert the value in the
appropriate position.
5. Now the first three are relatively sorted.
6. Do the same for the remaining items in the list.
13
20 October 2023
INSERTION SORT EXAMPLE ...CONT’D
14
20 October 2023
 Sort: 34 8 64 51 32 21
 34 8 64 51 32 21
 The algorithm sees that 8 is smaller than 34 so it swaps.
 8 34 64 51 32 21
 51 is smaller than 64, so they swap.
 8 34 51 64 32 21
 The algorithm sees 32 as another smaller number and moves
it to its appropriate location between 8 and 34.
 8 32 34 51 64 21
 The algorithm sees 21 as another smaller number and moves
into between 8 and 32.
 Final sorted numbers:
 8 21 32 34 51 64
CODE FOR INSERTION SORT ...CONT’D
void insertionSort(int[] array)
{
int inner, outer;
for (outer = 1; outer < array.length; outer++)
{
int temp = array[outer];
inner = outer;
while (inner > 0 && array[inner - 1] >= temp)
{
array[inner] = array[inner - 1];
inner--;
}
array[inner] = temp;
}
} 15
20 October 2023
ANALYSIS OF INSERTION SORT ...CONT’D
 We run once through the outer loop, inserting each of n
elements; this is a factor of n
 On average, there are n/2 elements already sorted
 The inner loop looks at (and moves) half of these
 This gives a second factor of n/4
 Hence, the time required for an insertion sort of an array of
n elements is proportional to n2/4
 Discarding constants, we find that insertion sort is O(n2)
 i.e.
 How many comparisons?
 1+2+3+…+(n-1)= O(n2)
 How many swaps?
 1+2+3+…+(n-1)= O(n2)
16
20 October 2023
SUMMARY OF SORTING ALGORITHMS
 Bubble Sort, Selection Sort, and Insertion Sort are
all O(n2)
 Within O(n2),
 Bubble Sort is very slow, and should probably never be
used for anything
 Selection Sort is intermediate in speed
 Insertion Sort is usually the fastest of the three--in fact, for
small arrays (like 10 or 15 elements), insertion sort is faster
than more complicated sorting algorithms
 Selection Sort and Insertion Sort are “good enough”
for small arrays
17
20 October 2023
SEARCHING ALGORITHMS
 Searching is a process of looking for a specific element
in a list of items or determining that the item is not in the
list.
 Two simple searching algorithms:
 Sequential / Linear Search, and
 Binary Search
18
20 October 2023
1. LINEAR SEARCHING
 Also called sequential searching
 Simplest type of searching process
 Easy to implement
 Can be used on very small data sets
 Not practical for searching large collections
 The idea is:
 Loop through the array starting at the first/last element until
the value of target matches one of the array elements or until
all elements are visited.
 If a match is not found, return –1
 Analysis:
 Time is proportional to the size of input n
 time complexity O(n)
19
20 October 2023
LINEAR SEARCHING …CONT’D
 Algorithm for Sequential/Linear Search
1. Initialize searcharray, key/number to be searched,
length
2. Initialize index=0,
3. Repeat step 4 till index<=length.
4. if searcharray[index]=key
return index
else
increment index by 1.
20
20 October 2023
IMPLEMENTATION OF LINEAR SEARCHING …CONT’D
int linearSearch(int list[ ], int key)
{
int index=0;
int found=0;
do
{
if(key==list[index])
found=1;
else
index++;
}while(found==0&&index<n);
if(found==0)
index=-1;
return index;
}
21
20 October 2023
2. BINARY SEARCHING
 This searching algorithms works only on an ordered list.
 It uses principle of divide and conquer
 Though additional cost has to do with keeping list in order, it is
more efficient than linear search
 The basic idea is:
1. Locate midpoint of array to search
2. Determine if target is in lower half or upper half of an array.
 If in lower half, make this half the array to search
 If in the upper half, make this half the array to search
3. Loop back to step 1 until the size of the array to search is
one, and this element does not match, in which case return
–1.
22
20 October 2023
BINARY SEARCHING …CONT’D
 Analysis:
 computational time for this algorithm is proportional to log2n
 Therefore the time complexity is O(log n)
 Algorithm for Binary Search
1. Initialize an ordered array, searcharray, key, length.
2. Initialize left=0 and right=length
3. Repeat step 4 till left<=right
4. Middle =(left + right) / 2
5. if searcharray[middle]=key
Search is successful
return middle.
else
if key<searcharray[middle]
right=middle - 1
else
left=middle + 1.
23
20 October 2023
IMPLEMENTATION OF BINARY SEARCHING …CONT’D
int Binary_Search(int list[ ],int k)
{
int left=0;
int right=n-1;
int found=0;
do{
mid=(left+right)/2;
if(key==list[mid])
found=1;
else{
if(key<list[mid])
right=mid-1;
else
left=mid+1;
}
}while(found==0&&left<right);
if(found==0)
index=-1;
else
index=mid;
return index;
} 24
20 October 2023
ANALYSIS OF GROWTH FUNCTIONS
25
20 October 2023
n
nlogn
THANK YOU
ANY Q?
26
20 October 2023

More Related Content

Similar to CHAPTER-2 Simple Sorting and Searching Algorithms

Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxdudelover
 
Chapter Two.pdf
Chapter Two.pdfChapter Two.pdf
Chapter Two.pdfabay golla
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmEhsan Ehrari
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 

Similar to CHAPTER-2 Simple Sorting and Searching Algorithms (20)

14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
14-sorting.ppt
14-sorting.ppt14-sorting.ppt
14-sorting.ppt
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
sorting_part1.ppt
sorting_part1.pptsorting_part1.ppt
sorting_part1.ppt
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
n-squared_sorts
n-squared_sortsn-squared_sorts
n-squared_sorts
 
Chapter Two.pdf
Chapter Two.pdfChapter Two.pdf
Chapter Two.pdf
 
my docoment
my docomentmy docoment
my docoment
 
Sorting
SortingSorting
Sorting
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
L1803016468
L1803016468L1803016468
L1803016468
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 

Recently uploaded

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 

Recently uploaded (20)

Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

CHAPTER-2 Simple Sorting and Searching Algorithms

  • 2. CHAPTER OVERVIEW  Simple Sorting algorithms o Selection sort o Bubble sort o Insertion sort  Simple searching algorithms o Linear/sequential searching o Binary searching 2 20 October 2023
  • 3. SORTING ALGORITHMS  Sorting o process of reordering a list of items in either increasing or decreasing order. o efficiency of sorting algorithm is measured using o the number of comparisons and o the number of data movements made by the algorithms. o Sorting algorithms are categorized as: o simple/elementary and o advanced. o Simple sorting algorithms, like Selection sort, Bubble sort and Insertion sort, are only used to sort small-sized list of items. 3 20 October 2023
  • 4. 1. SELECTION SORT  Given an array of length n,  Search elements 0 through n-1 and select the smallest  Swap it with the element in location 0  Search elements 1 through n-1 and select the smallest  Swap it with the element in location 1  Search elements 2 through n-1 and select the smallest  Swap it with the element in location 2  Search elements 3 through n-1 and select the smallest  Swap it with the element in location 3  Continue in this fashion until there’s nothing left to search 4 20 October 2023
  • 5. SELECTION SORT …CONT’D  i.e. the basic idea is  Loop through the array from i=0 to n-1.  Select the smallest element in the array from i to n  Swap this value with value at position i.  we repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array.  Note: the list/array is divided into two parts:  the sub-list of items already sorted and  the sub-list of items remaining to be sorted 5 20 October 2023
  • 6. SELECTION SORT EXAMPLE …CONT’D  Analysis:  The outer loop executes n-1 times  The inner loop executes about n(n-1)/2 times on average (from n to 2 times)  Work done in the inner loop is constant (swap two array elements)  Time required is roughly (n-1)*[n(n-1)/2]  You should recognize this as O(n2)  i.e.  How many comparisons?  (n-1)+(n-2)+…+1 = O(n2)  How many swaps?  n = O(n) 6 20 October 2023 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
  • 7. CODE FOR SELECTION SORT …CONT’D void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; } }//end of function 7 20 October 2023
  • 8. 2. BUBBLE SORT  Also called Exchange sort  simplest algorithm to implement and the slowest algorithm on very large inputs.  Basic Idea:  Loop through array from i=0 to n and swap adjacent elements if they are out of order.  repeatedly compares adjacent elements of an array.  Compare each element (except the last one) with its neighbor 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 place  Compare each element (except the last two) with its neighbor 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 places  Compare each element (except the last three) with its neighbor to the right  Continue as above until you have no unsorted elements on the left 8 20 October 2023
  • 9. BUBBLE SORT EXAMPLE ...CONT’D 9 20 October 2023 2 7 5 4 8 7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8
  • 10. CODE FOR BUBBLE SORT ...CONT’D void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) //counts down { for (inner = 0; inner < outer; inner++) { if (a[inner] > a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } } 10 20 October 2023
  • 11. ANALYSIS FOR BUBBLE SORT ...CONT’D  Let n = a.length = size of the array  The outer loop is executed n-1 times  Each time the outer loop is executed, the inner loop is executed  Inner loop executes n-1 times at first, linearly dropping to just once  On average, inner loop executes about n(n-1)/2 times for each execution of the outer loop  In the inner loop, the comparison is always done (constant time), the swap might be done (also constant time)  Result is (n-1) * [ n(n-1)/2 ] + k, that is, O(n2)  i.e.  How many comparisons?  (n-1)+(n-2)+…+1= O(n2)  How many swaps?  (n-1)+(n-2)+…+1= O(n2) 11 20 October 2023
  • 12. 3. INSERTION SORT  it inserts each item into its proper place in the final list.  The simplest implementation of this requires two list structures – the source list and the list into which sorted items are inserted.  Basic Idea:  Find the location for an element and move all others up, and insert the element.  The approach is the same approach that we use for sorting a set of cards in our hand.  While playing cards, we pick up a card, start at the beginning of our hand and find the place to insert the new card, insert it and move all the others up one place. 12 20 October 2023
  • 13. INSERTION SORT ...CONT’D  The algorithm is as follows 1. The left most value can be said to be sorted relative to itself. Thus, we don’t need to do anything. 2. Check to see if the second value is smaller than the first one. If it is, swap these two values. The first two values are now relatively sorted. 3. Next, we need to insert the third value in to the relatively sorted portion so that after insertion, the portion will still be relatively sorted. 4. Remove the third value first. Slide the second value to make room for insertion. Insert the value in the appropriate position. 5. Now the first three are relatively sorted. 6. Do the same for the remaining items in the list. 13 20 October 2023
  • 14. INSERTION SORT EXAMPLE ...CONT’D 14 20 October 2023  Sort: 34 8 64 51 32 21  34 8 64 51 32 21  The algorithm sees that 8 is smaller than 34 so it swaps.  8 34 64 51 32 21  51 is smaller than 64, so they swap.  8 34 51 64 32 21  The algorithm sees 32 as another smaller number and moves it to its appropriate location between 8 and 34.  8 32 34 51 64 21  The algorithm sees 21 as another smaller number and moves into between 8 and 32.  Final sorted numbers:  8 21 32 34 51 64
  • 15. CODE FOR INSERTION SORT ...CONT’D void insertionSort(int[] array) { int inner, outer; for (outer = 1; outer < array.length; outer++) { int temp = array[outer]; inner = outer; while (inner > 0 && array[inner - 1] >= temp) { array[inner] = array[inner - 1]; inner--; } array[inner] = temp; } } 15 20 October 2023
  • 16. ANALYSIS OF INSERTION SORT ...CONT’D  We run once through the outer loop, inserting each of n elements; this is a factor of n  On average, there are n/2 elements already sorted  The inner loop looks at (and moves) half of these  This gives a second factor of n/4  Hence, the time required for an insertion sort of an array of n elements is proportional to n2/4  Discarding constants, we find that insertion sort is O(n2)  i.e.  How many comparisons?  1+2+3+…+(n-1)= O(n2)  How many swaps?  1+2+3+…+(n-1)= O(n2) 16 20 October 2023
  • 17. SUMMARY OF SORTING ALGORITHMS  Bubble Sort, Selection Sort, and Insertion Sort are all O(n2)  Within O(n2),  Bubble Sort is very slow, and should probably never be used for anything  Selection Sort is intermediate in speed  Insertion Sort is usually the fastest of the three--in fact, for small arrays (like 10 or 15 elements), insertion sort is faster than more complicated sorting algorithms  Selection Sort and Insertion Sort are “good enough” for small arrays 17 20 October 2023
  • 18. SEARCHING ALGORITHMS  Searching is a process of looking for a specific element in a list of items or determining that the item is not in the list.  Two simple searching algorithms:  Sequential / Linear Search, and  Binary Search 18 20 October 2023
  • 19. 1. LINEAR SEARCHING  Also called sequential searching  Simplest type of searching process  Easy to implement  Can be used on very small data sets  Not practical for searching large collections  The idea is:  Loop through the array starting at the first/last element until the value of target matches one of the array elements or until all elements are visited.  If a match is not found, return –1  Analysis:  Time is proportional to the size of input n  time complexity O(n) 19 20 October 2023
  • 20. LINEAR SEARCHING …CONT’D  Algorithm for Sequential/Linear Search 1. Initialize searcharray, key/number to be searched, length 2. Initialize index=0, 3. Repeat step 4 till index<=length. 4. if searcharray[index]=key return index else increment index by 1. 20 20 October 2023
  • 21. IMPLEMENTATION OF LINEAR SEARCHING …CONT’D int linearSearch(int list[ ], int key) { int index=0; int found=0; do { if(key==list[index]) found=1; else index++; }while(found==0&&index<n); if(found==0) index=-1; return index; } 21 20 October 2023
  • 22. 2. BINARY SEARCHING  This searching algorithms works only on an ordered list.  It uses principle of divide and conquer  Though additional cost has to do with keeping list in order, it is more efficient than linear search  The basic idea is: 1. Locate midpoint of array to search 2. Determine if target is in lower half or upper half of an array.  If in lower half, make this half the array to search  If in the upper half, make this half the array to search 3. Loop back to step 1 until the size of the array to search is one, and this element does not match, in which case return –1. 22 20 October 2023
  • 23. BINARY SEARCHING …CONT’D  Analysis:  computational time for this algorithm is proportional to log2n  Therefore the time complexity is O(log n)  Algorithm for Binary Search 1. Initialize an ordered array, searcharray, key, length. 2. Initialize left=0 and right=length 3. Repeat step 4 till left<=right 4. Middle =(left + right) / 2 5. if searcharray[middle]=key Search is successful return middle. else if key<searcharray[middle] right=middle - 1 else left=middle + 1. 23 20 October 2023
  • 24. IMPLEMENTATION OF BINARY SEARCHING …CONT’D int Binary_Search(int list[ ],int k) { int left=0; int right=n-1; int found=0; do{ mid=(left+right)/2; if(key==list[mid]) found=1; else{ if(key<list[mid]) right=mid-1; else left=mid+1; } }while(found==0&&left<right); if(found==0) index=-1; else index=mid; return index; } 24 20 October 2023
  • 25. ANALYSIS OF GROWTH FUNCTIONS 25 20 October 2023 n nlogn
  • 26. THANK YOU ANY Q? 26 20 October 2023