SlideShare a Scribd company logo
Unit 2 Example
Linear and Binary Search
Prof. D.R. Dhotre
Searching Techniques
 Linear search
small arrays
unsorted arrays
 Binary search
large arrays
sorted arrays
Linear Search Algorithm
Start at first element of array.
Compare value to value (key) for which you are searching
Continue with next element of the array until you find a
match or reach the last element in the array.
Note: On the average you will have to compare the search
key with half the elements in the array.
Linear Search Code
const int arraySize = 100;
int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87};
int key = 88;
bool found = false;
for (int i = 0; i < arraySize; i++)
{
if (a[i] = = key)
{
cout << “Found it at array subscript “ << i << endl;
found = true;
break;
}
}
if (! found)
cout << “Could not find element “ << key << “ in array a” << endl;
Binary Search Algorithm
1. May only be used on a sorted array.
2. Eliminates one half of the elements after each comparison.
3. Locate the middle of the array
4. Compare the value at that location with the search key.
5. If they are equal - done!
6. Otherwise, decide which half of the array contains the search
key.
7. Repeat the search on that half of the array and ignore the other
half.
8. The search continues until the key is matched or no elements
remain to be searched.
Binary Search Example
1
0
1
2
3
4
5
6
7
8
9
10
11
12
5
15
19
25
27
29
31
33
45
55
88
100
middle of the array
compare a[6] and 19
19 is smaller than 29 so the next
search will use the lower half of the array
search key = 19
a
Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 19
use this as the middle of the array
Compare a[2] with 19
15 is smaller than 19 so use the top
half for the next pass
a
Binary Search Pass 3
3
4
5
25
27
search key = 19
use this as the middle of the array
Compare a[4] with 19
25 is bigger than 19 so use the bottom
half
a
19
Binary Search Pass 4
3
search key = 19
use this as the middle of the array
Compare a[3] with 19
Found!!
a
19
Binary Search Example
1
0
1
2
3
4
5
6
7
8
9
10
11
12
5
15
19
25
27
29
31
33
45
55
88
100
middle of the array
compare a[6] and 18
18 is smaller than 29 so the next
search will use the lower half of the array
search key = 18
a
Binary Search Pass 2
1
0
1
2
3
4
5
5
15
19
25
27
search key = 18
use this as the middle of the array
Compare a[2] with 18
15 is smaller than 18 so use the top
half for the next pass
a
Binary Search Pass 3
3
4
5
25
27
search key = 18
use this as the middle of the array
Compare a[4] with 18
25 is bigger than 18 so use the bottom
half
a
19
Binary Search Pass 4
3
search key = 18
use this as the middle of the array
Compare a[3] with 18
Does not match and no more elements
to compare.
Not Found!!
a
19
const int arraySize = 13;
int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 };
int key = 23 , low = 0, middle, high = (arraySize - 1);
while (low <= high)
{
middle = (low + high) / 2;
if (key = = a[middle])
{
cout << “Found element “ << key << “ at index “ << middle << endl;
break;
}
else if (key < a [middle])
high = middle -1; // search low end of array
else
low = middle + 1; // search high end of array
}
Efficiency
Searching an array of 1024 elements will take at most
10 passes to find a match or determine that the
element does not exist in the array.
512, 256, 128, 64, 32, 16, 8, 4, 2, 1
An array of one billion elements takes a maximum of
30 comparisons.
The bigger the array the better a binary search is as
compared to a linear search
Exercises
1) Write a function prototype for a function that does a bubble
sort named BubbleSort.
2) Write a function prototype for a function that does a binary
search named BinarySearch. The return value for BinarySearch
is of type int. If the function successfully finds the key for which
it is searching the return value contains the index in the array of
the element that matches the key.
If the function does not find the key for which it is searching the
return value contains a -1
3) Write a main program that initializes an array with data that is
out of order, calls the BubbleSort function to sort it and then
calls the BinarySearch function to find an element in the array.

More Related Content

Similar to 1 class linear and Binary search (3).ppt

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
B.Kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
Tribhuvan University
 
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
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
SpammemeLmao
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
mbadhi barnabas
 
Searching
Searching Searching
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
maamir farooq
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
Durga Devi
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
Abdul Khan
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
searching
searchingsearching
searching
A. S. M. Shafi
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
Trupti Agrawal
 

Similar to 1 class linear and Binary search (3).ppt (20)

Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Searching and sorting by B kirron Reddi
Searching and sorting by B kirron ReddiSearching and sorting by B kirron Reddi
Searching and sorting by B kirron Reddi
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Algorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptxAlgorithm 8th lecture linear & binary search(2).pptx
Algorithm 8th lecture linear & binary search(2).pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Binary search Algorithm
Binary search AlgorithmBinary search Algorithm
Binary search Algorithm
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Searching
Searching Searching
Searching
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
searching
searchingsearching
searching
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Searching algorithms
Searching algorithmsSearching algorithms
Searching algorithms
 

Recently uploaded

Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
TaghreedAltamimi
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 

Recently uploaded (20)

Software Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.pptSoftware Quality Assurance-se412-v11.ppt
Software Quality Assurance-se412-v11.ppt
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 

1 class linear and Binary search (3).ppt

  • 1. Unit 2 Example Linear and Binary Search Prof. D.R. Dhotre
  • 2. Searching Techniques  Linear search small arrays unsorted arrays  Binary search large arrays sorted arrays
  • 3. Linear Search Algorithm Start at first element of array. Compare value to value (key) for which you are searching Continue with next element of the array until you find a match or reach the last element in the array. Note: On the average you will have to compare the search key with half the elements in the array.
  • 4. Linear Search Code const int arraySize = 100; int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87}; int key = 88; bool found = false; for (int i = 0; i < arraySize; i++) { if (a[i] = = key) { cout << “Found it at array subscript “ << i << endl; found = true; break; } } if (! found) cout << “Could not find element “ << key << “ in array a” << endl;
  • 5. Binary Search Algorithm 1. May only be used on a sorted array. 2. Eliminates one half of the elements after each comparison. 3. Locate the middle of the array 4. Compare the value at that location with the search key. 5. If they are equal - done! 6. Otherwise, decide which half of the array contains the search key. 7. Repeat the search on that half of the array and ignore the other half. 8. The search continues until the key is matched or no elements remain to be searched.
  • 6. Binary Search Example 1 0 1 2 3 4 5 6 7 8 9 10 11 12 5 15 19 25 27 29 31 33 45 55 88 100 middle of the array compare a[6] and 19 19 is smaller than 29 so the next search will use the lower half of the array search key = 19 a
  • 7. Binary Search Pass 2 1 0 1 2 3 4 5 5 15 19 25 27 search key = 19 use this as the middle of the array Compare a[2] with 19 15 is smaller than 19 so use the top half for the next pass a
  • 8. Binary Search Pass 3 3 4 5 25 27 search key = 19 use this as the middle of the array Compare a[4] with 19 25 is bigger than 19 so use the bottom half a 19
  • 9. Binary Search Pass 4 3 search key = 19 use this as the middle of the array Compare a[3] with 19 Found!! a 19
  • 10. Binary Search Example 1 0 1 2 3 4 5 6 7 8 9 10 11 12 5 15 19 25 27 29 31 33 45 55 88 100 middle of the array compare a[6] and 18 18 is smaller than 29 so the next search will use the lower half of the array search key = 18 a
  • 11. Binary Search Pass 2 1 0 1 2 3 4 5 5 15 19 25 27 search key = 18 use this as the middle of the array Compare a[2] with 18 15 is smaller than 18 so use the top half for the next pass a
  • 12. Binary Search Pass 3 3 4 5 25 27 search key = 18 use this as the middle of the array Compare a[4] with 18 25 is bigger than 18 so use the bottom half a 19
  • 13. Binary Search Pass 4 3 search key = 18 use this as the middle of the array Compare a[3] with 18 Does not match and no more elements to compare. Not Found!! a 19
  • 14. const int arraySize = 13; int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 }; int key = 23 , low = 0, middle, high = (arraySize - 1); while (low <= high) { middle = (low + high) / 2; if (key = = a[middle]) { cout << “Found element “ << key << “ at index “ << middle << endl; break; } else if (key < a [middle]) high = middle -1; // search low end of array else low = middle + 1; // search high end of array }
  • 15. Efficiency Searching an array of 1024 elements will take at most 10 passes to find a match or determine that the element does not exist in the array. 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 An array of one billion elements takes a maximum of 30 comparisons. The bigger the array the better a binary search is as compared to a linear search
  • 16. Exercises 1) Write a function prototype for a function that does a bubble sort named BubbleSort. 2) Write a function prototype for a function that does a binary search named BinarySearch. The return value for BinarySearch is of type int. If the function successfully finds the key for which it is searching the return value contains the index in the array of the element that matches the key. If the function does not find the key for which it is searching the return value contains a -1 3) Write a main program that initializes an array with data that is out of order, calls the BubbleSort function to sort it and then calls the BinarySearch function to find an element in the array.