SlideShare a Scribd company logo
Chapter two
Searching and sorting algorithms:
divide and conquer approach
Divide and conquer
• it is the process of dividing a complex problem into two or more sub-
problems of the same type until the sub-problems are easy enough to be
solved directly and recursively,
• and then these solutions are combined to give a solution to the initial problem.
• It is the foundation of efficient algorithms for many different problems,
• including sorting problems and the discrete Fourier transform.
• main steps of the divide and conquer paradigm
1. Divide the problem into two (or more) sub-problems
2. Conquer these sub problems recursively
3. Combine the solutions to the sub problems in order to get a solution to
the original problem.
Advantages of divide and conquer paradigm
• It allows us to solve difficult and often impossible looking problems, such
as the Tower of Hanoi
• it reduces the degree of difficulty since it divides the problem into sub problems
• Runs faster than other algorithms would
• it often plays a part in finding other efficient algorithms, and in fact it was
the central role in finding the quick sort and merge sort algorithms.
• It also uses memory caches effectively.
• when the sub problems become simple enough, they can be solved within a cache,
without having to access the slower main memory
Disadvantages
• The recursion is slow
• It can become more complicated than a basic iterative approach,
• especially in cases with a large n (add a large amount of numbers together)
• sometimes once the problem is broken down into sub problems, the same
sub problem can occur many times.
• these algorithms can be carried out by a non-recursive program that will
store the different sub problems in things called explicit stacks (not call
stacks),
• which gives more freedom in deciding just which order the sub problems should be
solved.
Some algorithms
• Binary Search,
• the Merge Sort Algorithm,
• the Quick sort algorithm,
• Matrix Multiplication,
• and the fast Fourier transform
Searching algorithms: linear search
• Searching is the process of finding some particular element in the list.
• If the element is present in the list,
• then the process is called successful and the process returns the location of that
element,
• otherwise the search is called unsuccessful.
• There are two popular search methods that are widely used
• Linear Search / Sequential Search
• Binary search
Linear Search / Sequential Search
• Linear search, also called as sequential search
• is a very simple method used for searching an array for a particular value.
• It works by comparing the value to be searched with every element of the array one
by one
• mostly used to search an unordered list of elements
• int A[ ] = {10, 8, 1, 21, 7, 32, 5, 11, 0};
LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 0
Step 3: Repeat Step 4
while
I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I=I+1
[END OF LOOP]
Step 5: IF POS = –1
PRINT "VALUE IS NOT PRESENTIN
THE ARRAY"
[END OF IF]
Step 6: EXIT
Write a program to search an element in an array using the linear
search technique.
#include<stdio.h>
void main ()
{
int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9};
int item, i,flag;
printf("nEnter Item which is to be searchedn");
scanf("%d",&item);
for (i = 0; i< 10; i++){
if(a[i] == item){
flag = i+1;
break;}
else
flag = 0;}//end of loop
if(flag != 0){
printf("nItem found at location %dn",flag);
}
else{
printf("nItem not foundn");}
} //end of main method
Advantages of a linear search
• Will perform fast searches of small to medium lists.
• With today's powerful computers, small to medium arrays can be searched
relatively quickly.
• The list does not need to be sorted.
• Unlike a binary search, linear searching does not require an ordered list.
• Not affected by insertions and deletions.
• As the linear search does not require the list to be sorted, additional elements can
be added and deleted.
• As other searching algorithms may have to reorder the list after insertions or
deletions, this may sometimes mean a linear search will be more efficient.
Disadvantages of a linear search
• Slow searching of large lists.
• For example, when searching through a database of everyone in Ethiopia to find a
particular name,
• it might be necessary to search through 120 million names before you found the
one you wanted.
• This speed disadvantage is why other search methods have been developed.
• Space and time complexity
• Space complexity
• It takes up no extra space;
• its space complexity is O(n) for an array of n elements.
Complexity of Linear Search Algorithm
• Best Case Complexity
• The element being searched could be found in the first position.
• In this case, the search ends with a single successful comparison.
• Thus, in the best-case scenario, the linear search algorithm performs O(1)
operations.
• Worst Case Complexity
• The element being searched may be at the last position in the array or not at all.
• The search succeeds in ‘n’ comparisons or the search fails after ‘n’ comparisons.
• Thus, the linear search algorithm performs O(n) operations.
• Average Case Complexity
• When the element to be searched is in the middle of the array, the average case of
the Linear Search Algorithm is O(n).
BINARY SEARCH
• works efficiently on the sorted lists
• follows divide and conquer approach
• the list is divided into two halves and the item is compared with the middle element
of the list.
• If the match is found then,
• the location of middle element is returned otherwise,
• we search into either of the halves depending upon the result produced through the match.
• Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the
location of the item 23 in the array.
• Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the
location of the item 23 in the array.
In 1st step :
1. BEG = 0
2. END = 8
3. MID = 4
4. a[mid] = a[4] = 13 < 23,
therefore
in Second step:
1. Beg = mid +1 = 5
2. End = 8
3. mid = 13/2 = 6
4. a[mid] = a[6] = 20 < 23,
therefore
in third step:
1. beg = mid + 1 = 7
2. End = 8
3. mid = 15/2 = 7
4. a[mid] = a[7]
5. a[7] = 23 = item;
6. therefore, set location
= mid;
7. The location of the item
BINARY_SEARCH(A,lower_bound, upper_bound, VAL)
Step 1: [INITIALIZE] SET BEG = lower_bound
END = upper_bound, POS = - 1
Step 2: Repeat Steps 3 and 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL
SET POS = MID
PRINT POS
Go to Step 6
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1
PRINT “VALUE IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
int binarySearch(int a[], int beg, int end,
int item){
int mid;
if(end >= beg){
mid = (beg + end)/2;
if(a[mid] == item){
return mid+1;}
else if(a[mid] < item){
return binarySearch(a,mid+1,end,item);
}else{
return binarySearch(a,beg,mid-1,item);
}}
return -1;
Advantages
• It eliminates half of the list from further searching by using the result of
each comparison.
• It indicates whether the element being searched is before or after the
current position in the list.
• This information is used to narrow the search.
• For large lists of data, it works significantly better than linear search.
Disadvantages
• It employs recursive approach which requires more stack space.
• Programming binary search algorithm is error prone and difficult.
• The interaction of binary search with memory hierarchy i.e. caching is poor.
(because of its random access nature)
• Complexity of Binary Search Algorithm
• As it disposes off one part of the search case during every step of binary
search, and perform the search operation on the other half,
• this results in a worst case time complexity of O(logN).
• What is the best case and average case time complexity ?
Sequential Search Binary Search
Time complexity is O(n) Time complexity is O(log n)
Finds the key present at first position in
constant time
Finds the key present at center position in
constant time
Sequence of elements in the container does
not affect.
The elements must be sorted in the
Container
Arrays and linked lists can be used to
implement this
It cannot be implemented directly into the
linked list. We need to change the basic
rules of the list to implement this
Algorithm is iterative in nature Algorithm technique is Divide and
Conquer.
Algorithm is easy to implement, and
requires less amount of code.
Algorithm is slightly complex. It takes
more amount of code to implement.
N number of comparisons are required for
worst case.
Log n number of comparisons are
sufficient in worst case.
Sorting Algorithms: Merge-sort
• It is one of the well-known divide-and-conquer algorithms
• is a simple and very efficient sorting algorithm.
• Steps
• Divide: Split Array A into two sub-sequences, each of size roughly n/2 .
• Conquer: Sort each subsequence (by calling MergeSort recursively on each).
• Combine: Merge the two sorted sub-sequences into a single sorted list.
• The dividing process ends when we have split the sub-sequences down to
a single item.
• It works top-down splitting up the list into smaller sub-lists
• The “conquer and combine” phase works bottom-up, merging sorted lists
together into larger sorted lists.
• Algorithm:
1. Divide the array in to two halves.
2. Recursively sort the first n/2 items.
3. Recursively sort the last n/2 items.
4. Merge sorted items (using an auxiliary array).
Sorting and merging phase
Division phase
Merge(L,R, A)
{
nLlength(L)
nRlength(R)
ijk0
while(i<nL && j<nR){
If(L[i]<=R[j]) {
A[k]L[i]
ii+1
}
Else{
A[k]R[j]
Jj+1
}
Kk+1
}
While(i<nL)
{
A[k]L[i]
Ii+1
Kk+1
}
While(j<nR)
{
A[k]R[j]
jj+1
Kk+1
}
}
Merge_sort(A)
{
Nlength(A)
If(N<2)
Return
Midn/2
Leftarray of size(mid)
Rightarray of size(n-mid)
For i0 to mid-1
Left[i]A[i]
For imid to n-1
Right[i-mid]A[i]
Merge_sort(left)
Merge_sort(Right)
Merge(left,right,A)
}
Advantages and disadvantages
• Advantages :
• is best case for sorting slow-access data e.g) tape drive.
• is better at handling sequential - accessed lists.
• Disadvantages:
• Slower comparative to the other sort algorithms for smaller tasks.
• Merge sort algorithm requires additional memory space of 0(n) for the temporary
array .
• It goes through the whole process even if the array is sorted.
• Complexity of Merge Sort
• The running time of merge sort in the average case and the worst case can be given
as O(n logn).
• Although merge sort has an optimal time complexity, it needs an additional space
of O(n) for the temporary array TEMP.
• Applications
• Merge Sort is useful for sorting linked lists in O(nLogn) time.
• Inversion Count Problem
• Used in External Sorting
Questions ?

More Related Content

Similar to data_structure_Chapter two_computer.pptx

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
technologygyan
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
AnSHiKa187943
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
babuk110
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
Dr.Shweta
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
Sorting
SortingSorting
Sorting
FahadSaeed39
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
FazalRehman79
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
Prashant Rai
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
Archana Burujwale
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ28
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
reddy19841
 

Similar to data_structure_Chapter two_computer.pptx (20)

advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Data structure and algorithms
Data structure and algorithmsData structure and algorithms
Data structure and algorithms
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Data operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithmsData operatons & searching and sorting algorithms
Data operatons & searching and sorting algorithms
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
SORTING techniques.pptx
SORTING techniques.pptxSORTING techniques.pptx
SORTING techniques.pptx
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Sorting
SortingSorting
Sorting
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Searching techniques
Searching techniquesSearching techniques
Searching techniques
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 

Recently uploaded

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
AnirbanRoy608946
 

Recently uploaded (20)

原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptxData_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
Data_and_Analytics_Essentials_Architect_an_Analytics_Platform.pptx
 

data_structure_Chapter two_computer.pptx

  • 1. Chapter two Searching and sorting algorithms: divide and conquer approach
  • 2. Divide and conquer • it is the process of dividing a complex problem into two or more sub- problems of the same type until the sub-problems are easy enough to be solved directly and recursively, • and then these solutions are combined to give a solution to the initial problem. • It is the foundation of efficient algorithms for many different problems, • including sorting problems and the discrete Fourier transform. • main steps of the divide and conquer paradigm 1. Divide the problem into two (or more) sub-problems 2. Conquer these sub problems recursively 3. Combine the solutions to the sub problems in order to get a solution to the original problem.
  • 3. Advantages of divide and conquer paradigm • It allows us to solve difficult and often impossible looking problems, such as the Tower of Hanoi • it reduces the degree of difficulty since it divides the problem into sub problems • Runs faster than other algorithms would • it often plays a part in finding other efficient algorithms, and in fact it was the central role in finding the quick sort and merge sort algorithms. • It also uses memory caches effectively. • when the sub problems become simple enough, they can be solved within a cache, without having to access the slower main memory
  • 4.
  • 5. Disadvantages • The recursion is slow • It can become more complicated than a basic iterative approach, • especially in cases with a large n (add a large amount of numbers together) • sometimes once the problem is broken down into sub problems, the same sub problem can occur many times. • these algorithms can be carried out by a non-recursive program that will store the different sub problems in things called explicit stacks (not call stacks), • which gives more freedom in deciding just which order the sub problems should be solved.
  • 6. Some algorithms • Binary Search, • the Merge Sort Algorithm, • the Quick sort algorithm, • Matrix Multiplication, • and the fast Fourier transform
  • 7. Searching algorithms: linear search • Searching is the process of finding some particular element in the list. • If the element is present in the list, • then the process is called successful and the process returns the location of that element, • otherwise the search is called unsuccessful. • There are two popular search methods that are widely used • Linear Search / Sequential Search • Binary search
  • 8. Linear Search / Sequential Search • Linear search, also called as sequential search • is a very simple method used for searching an array for a particular value. • It works by comparing the value to be searched with every element of the array one by one • mostly used to search an unordered list of elements • int A[ ] = {10, 8, 1, 21, 7, 32, 5, 11, 0};
  • 9. LINEAR_SEARCH(A, N, VAL) Step 1: [INITIALIZE] SET POS = -1 Step 2: [INITIALIZE] SET I = 0 Step 3: Repeat Step 4 while I<=N Step 4: IF A[I] = VAL SET POS = I PRINT POS Go to Step 6 [END OF IF] SET I=I+1 [END OF LOOP] Step 5: IF POS = –1 PRINT "VALUE IS NOT PRESENTIN THE ARRAY" [END OF IF] Step 6: EXIT Write a program to search an element in an array using the linear search technique. #include<stdio.h> void main () { int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9}; int item, i,flag; printf("nEnter Item which is to be searchedn"); scanf("%d",&item); for (i = 0; i< 10; i++){ if(a[i] == item){ flag = i+1; break;} else flag = 0;}//end of loop if(flag != 0){ printf("nItem found at location %dn",flag); } else{ printf("nItem not foundn");} } //end of main method
  • 10. Advantages of a linear search • Will perform fast searches of small to medium lists. • With today's powerful computers, small to medium arrays can be searched relatively quickly. • The list does not need to be sorted. • Unlike a binary search, linear searching does not require an ordered list. • Not affected by insertions and deletions. • As the linear search does not require the list to be sorted, additional elements can be added and deleted. • As other searching algorithms may have to reorder the list after insertions or deletions, this may sometimes mean a linear search will be more efficient.
  • 11. Disadvantages of a linear search • Slow searching of large lists. • For example, when searching through a database of everyone in Ethiopia to find a particular name, • it might be necessary to search through 120 million names before you found the one you wanted. • This speed disadvantage is why other search methods have been developed. • Space and time complexity • Space complexity • It takes up no extra space; • its space complexity is O(n) for an array of n elements.
  • 12. Complexity of Linear Search Algorithm • Best Case Complexity • The element being searched could be found in the first position. • In this case, the search ends with a single successful comparison. • Thus, in the best-case scenario, the linear search algorithm performs O(1) operations. • Worst Case Complexity • The element being searched may be at the last position in the array or not at all. • The search succeeds in ‘n’ comparisons or the search fails after ‘n’ comparisons. • Thus, the linear search algorithm performs O(n) operations. • Average Case Complexity • When the element to be searched is in the middle of the array, the average case of the Linear Search Algorithm is O(n).
  • 13. BINARY SEARCH • works efficiently on the sorted lists • follows divide and conquer approach • the list is divided into two halves and the item is compared with the middle element of the list. • If the match is found then, • the location of middle element is returned otherwise, • we search into either of the halves depending upon the result produced through the match. • Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array.
  • 14. • Let us consider an array arr = {1, 5, 7, 8, 13, 19, 20, 23, 29}. Find the location of the item 23 in the array. In 1st step : 1. BEG = 0 2. END = 8 3. MID = 4 4. a[mid] = a[4] = 13 < 23, therefore in Second step: 1. Beg = mid +1 = 5 2. End = 8 3. mid = 13/2 = 6 4. a[mid] = a[6] = 20 < 23, therefore in third step: 1. beg = mid + 1 = 7 2. End = 8 3. mid = 15/2 = 7 4. a[mid] = a[7] 5. a[7] = 23 = item; 6. therefore, set location = mid; 7. The location of the item
  • 15.
  • 16. BINARY_SEARCH(A,lower_bound, upper_bound, VAL) Step 1: [INITIALIZE] SET BEG = lower_bound END = upper_bound, POS = - 1 Step 2: Repeat Steps 3 and 4 while BEG <= END Step 3: SET MID = (BEG + END)/2 Step 4: IF A[MID] = VAL SET POS = MID PRINT POS Go to Step 6 ELSE IF A[MID] > VAL SET END = MID - 1 ELSE SET BEG = MID + 1 [END OF IF] [END OF LOOP] Step 5: IF POS = -1 PRINT “VALUE IS NOT PRESENT IN THE ARRAY” [END OF IF] Step 6: EXIT int binarySearch(int a[], int beg, int end, int item){ int mid; if(end >= beg){ mid = (beg + end)/2; if(a[mid] == item){ return mid+1;} else if(a[mid] < item){ return binarySearch(a,mid+1,end,item); }else{ return binarySearch(a,beg,mid-1,item); }} return -1;
  • 17. Advantages • It eliminates half of the list from further searching by using the result of each comparison. • It indicates whether the element being searched is before or after the current position in the list. • This information is used to narrow the search. • For large lists of data, it works significantly better than linear search.
  • 18. Disadvantages • It employs recursive approach which requires more stack space. • Programming binary search algorithm is error prone and difficult. • The interaction of binary search with memory hierarchy i.e. caching is poor. (because of its random access nature) • Complexity of Binary Search Algorithm • As it disposes off one part of the search case during every step of binary search, and perform the search operation on the other half, • this results in a worst case time complexity of O(logN). • What is the best case and average case time complexity ?
  • 19. Sequential Search Binary Search Time complexity is O(n) Time complexity is O(log n) Finds the key present at first position in constant time Finds the key present at center position in constant time Sequence of elements in the container does not affect. The elements must be sorted in the Container Arrays and linked lists can be used to implement this It cannot be implemented directly into the linked list. We need to change the basic rules of the list to implement this Algorithm is iterative in nature Algorithm technique is Divide and Conquer. Algorithm is easy to implement, and requires less amount of code. Algorithm is slightly complex. It takes more amount of code to implement. N number of comparisons are required for worst case. Log n number of comparisons are sufficient in worst case.
  • 20. Sorting Algorithms: Merge-sort • It is one of the well-known divide-and-conquer algorithms • is a simple and very efficient sorting algorithm. • Steps • Divide: Split Array A into two sub-sequences, each of size roughly n/2 . • Conquer: Sort each subsequence (by calling MergeSort recursively on each). • Combine: Merge the two sorted sub-sequences into a single sorted list. • The dividing process ends when we have split the sub-sequences down to a single item. • It works top-down splitting up the list into smaller sub-lists • The “conquer and combine” phase works bottom-up, merging sorted lists together into larger sorted lists.
  • 21. • Algorithm: 1. Divide the array in to two halves. 2. Recursively sort the first n/2 items. 3. Recursively sort the last n/2 items. 4. Merge sorted items (using an auxiliary array).
  • 22. Sorting and merging phase Division phase
  • 23. Merge(L,R, A) { nLlength(L) nRlength(R) ijk0 while(i<nL && j<nR){ If(L[i]<=R[j]) { A[k]L[i] ii+1 } Else{ A[k]R[j] Jj+1 } Kk+1 } While(i<nL) { A[k]L[i] Ii+1 Kk+1 } While(j<nR) { A[k]R[j] jj+1 Kk+1 } } Merge_sort(A) { Nlength(A) If(N<2) Return Midn/2 Leftarray of size(mid) Rightarray of size(n-mid) For i0 to mid-1 Left[i]A[i] For imid to n-1 Right[i-mid]A[i] Merge_sort(left) Merge_sort(Right) Merge(left,right,A) }
  • 24. Advantages and disadvantages • Advantages : • is best case for sorting slow-access data e.g) tape drive. • is better at handling sequential - accessed lists. • Disadvantages: • Slower comparative to the other sort algorithms for smaller tasks. • Merge sort algorithm requires additional memory space of 0(n) for the temporary array . • It goes through the whole process even if the array is sorted.
  • 25. • Complexity of Merge Sort • The running time of merge sort in the average case and the worst case can be given as O(n logn). • Although merge sort has an optimal time complexity, it needs an additional space of O(n) for the temporary array TEMP. • Applications • Merge Sort is useful for sorting linked lists in O(nLogn) time. • Inversion Count Problem • Used in External Sorting