0% found this document useful (0 votes)
54 views

Chapter - 3 - Searching and Sorting Algorithms

The document discusses various searching and sorting algorithms. It covers sequential search, binary search, bubble sort, selection sort, and insertion sort. Searching algorithms like sequential and binary search are used to find a particular element in a list or array. Sorting algorithms arrange data in a particular order like numerical order. Bubble sort, selection sort, and insertion sort are discussed as examples of sorting algorithms that arrange data in ascending or descending order. Binary search and sorting algorithms improve search efficiency when data is stored in a sorted manner.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Chapter - 3 - Searching and Sorting Algorithms

The document discusses various searching and sorting algorithms. It covers sequential search, binary search, bubble sort, selection sort, and insertion sort. Searching algorithms like sequential and binary search are used to find a particular element in a list or array. Sorting algorithms arrange data in a particular order like numerical order. Bubble sort, selection sort, and insertion sort are discussed as examples of sorting algorithms that arrange data in ascending or descending order. Binary search and sorting algorithms improve search efficiency when data is stored in a sorted manner.

Uploaded by

elias ferhan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 3

Searching and Sorting Algorithms

1
Content
This Chapter Covers:
 Sequential search
 Binary search
 Bubble sort
 Selection sort
 Insertion sort

2
Why do we study sorting and searching algorithms?
they are the most common & useful tasks in any software development
they take more than 25% of running time of computers task
Example:
Searching documents over the internet
Searching files and folders in a hard disk
Sorting students by their name, year and so on
Sorting file, search results by file name, date created and so on
Deleting and recording data from a database

3
Simple searching Algorithm
The act of searching for a piece of information in a list is one of the fundamental
algorithms in computer science.
Searching: is a process of locating a particular element present in a given set of
elements.

A search algorithm: is an algorithm that accepts an argument ‘a’ and tries to


find an element whose value is ‘a’. 
It is possible that the search for a particular element in a set is unsuccessful if
that element does not exist.

There are number of techniques available for searching


Searching algorithms to be considered:
Sequential search
Binary search
4
Linear Searching
In search algorithms, we are concerned with the process of looking through a list
to find a particular element, called the target.
The linear search algorithm searches each element in an array sequentially.
If the search key does not match an element in the array, the algorithm tests each
element and, when the end of the array is reached, informs the user that the search key
is not present.
If the search key is in the array, the algorithm tests each element until it finds one that
matches the search key and returns the index of that element.
If after checking every array element, the method determines that the search key does
not match any element in the array, the function returns -1.
If there are duplicate values in the array, linear search returns the index of the first
element in the array that matches the search key.

5
Sequential Searching

Example :-Search the element (25)


Implementation:
int arr[10], i, num, found=0;
for(i=0; i<10; i++){
if(arr[i]==num){
cout<<"the element found at index "<<i;
found = 1; }}
if (found==0){
cout<<"the element not found“;}
cout<<endl;
return 0;}
6
Binary Searching
Algorithm:
The binary search algorithm is more efficient than the linear search algorithm, but it
requires that the array first must be sorted.
The first iteration of this algorithm tests the middle element in the array. If this
matches the search key, the algorithm ends.
If the search key is greater than the middle element, the search key cannot match any
element in the first half of the array, and the algorithm continues with only the second
half of the array(i.e., the element after the middle element through the last element)
Each iteration tests the middle value of the remaining portion of the array, called a
subarray.
The algorithm ends by either finding an element that matches the search key or
reducing the subarray to zero size.

7
Binary Searching
Algorithm:
1. Divide the list into halves
2. see which half of the list the key belongs
3. repeat 1 and 2 until only one element remains
4. if the remaining element is equal to search key then found =true else key not
found.
1st assign first and last
2nd calculate middle= first + last/2
A. if key>middle first=middle+1, last=last
B. . if key<middle first=first, last=middle-1
C. . if key==middle print position or index of Key

8
Binary Searching

9
Binary Searching
Implementation:
int i, arr[10], num, first=0, last=9, middle;
middle = (first+last)/2;
while(first <= last){
if(arr[middle]<num)
first = middle+1;
else if(arr[middle]==num){
cout<<"\nThe number, "<<num<<" found at Position "<<middle+1;
break;}
else
last = middle-1;
middle = (first+last)/2;}
if(first>last)
cout<<"\nThe number, "<<num<<" is not found in given Array";

10
Sorting Algorithms
For searching, sorting is important
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the
way to arrange data in a particular order.
Most common orders are in numerical order.
The importance of sorting lies in the fact that data searching can be optimized to a very
high level, if data is stored in a sorted manner.
Sorting is also used to represent data in more readable formats

11
Bubble Sort Algorithm
It a method which uses the interchanging of adjacent pair of element in the
array.
After each pass through the data one element (the largest or smallest element)
will be moved down to the end of the array in its proper place.
• AIM: Write a program to sort values in ascending order using bubble sort technique in
linear array.
• Description: ‘data’ is linear array and N is the number of values stored in data
• Basic Idea:
 Loop through array from i = 0 to N and swap adjacent elements if they are out of order

12
Example data element= 7,2,9,6,4
Algorithms:

• Implementations: Example sort the following array


for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1]){ Single pass
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
Continue
} until sorted

13
Selection Sort Algorithm
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part and
putting it at the beginning
It is in many ways like both simple and bubble algorithm.
Rather than swapping the neighbors continuously as the algorithm traverses
the sub array to be sorted, as done in the bubble sort case, this algorithm
finds the minimum element of the sub array and swaps it with the pivot
elements.
The algorithm maintains two subarrays in a given array.
The subarray which is already sorted. 
Remaining subarray which is unsorted.

14
Selection Sort Algorithm
 The smallest element is selected from unsorted element and exchange with element in the beginning unsorted
element
i=0
 Loop through the array from i = 0 to N – 1. J=1
Min=i
 Select the smallest element in the array from i = 1 to N - 1. 7 4 10 8 3 1
 Swap this value with value at position i.
Pass 1 1 4 10 8 3 7
Example
Algorithms:
void SelectionSort(int data[]) Pass 2 1 3 10 8 4 7
int smallest
loop through i =0 to N -1
Pass 3 1 3 4 8 10 7
smallest  0
loop through j  i+1 to N
Pass 4 1 3 4 7 10 8
if data[j] < data[smallest]
smallest  j
swap data[i] and data[smallest]); Pass 1 1 3 4 7 8 10
Example
Implementation:

int smallest;
for(int i=0;i<6;i++){
min=i;
for(int j=i+1;j<6;j++){
if(A[j]<A[min])
min=j;}
int temp=A[min];
A[min]=A[i];
A[i]=temp;
cout<<A[i]<<endl;}}

16
Insertion Sort Algorithm
As each element in the list is examined it is put into its proper place
among the elements that have been examined ( and put in correct
order).
When the last element is put into its proper position the list is sorted
and the algorithm is done.
It behaves in the worst case like the inefficient bubble sort and selection
sort algorithms.
But in many average case it performs better.

17
18
Implementation
• Implementations:
Int a[],
Int n,temp;
For(i=1;i<n;i++){
Temp=a[i]
j=i-1;
While (j>=0&&a[j]>temp)
{a[j+1]=a[j];
j--}
A[j+1]=temp;

19
Assignment - III
Write a C++ program that will implement all of the sorting algorithms. The
program should accept different unsorted data items from user and sort
using all algorithms and should tell which algorithm is efficient for that
unsorted data items. (Hint use how many comparison and swaps are done so
the one with the smaller number of swap and comparison number is
considered as an efficient algorithm for that data items.)

20

???
End!!!!!!!!

21

You might also like