Dsa CH 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Searching and Sorting Techniques

MODULE - 2
Syllabus
• Introduction to Searching: Linear search, Binary
search
• Introduction to Sorting:
– Selection Sort,
– Insertion Sort,
– Quick Sort,
– Merge Sort,
– Radix Sort.
• Complexity Analysis of searching and sorting
techniques.
• Case study: Store pixel values for manipulation
and analysis of images using Arrays
Linear Search
• Linear Search is a sequential searching
algorithm that is used to find an element in a
list.
Linear Search
• Check each element in the list by comparing it to
the key.

• If any element is equal to the key, return its index.

• If we reach the end of the list without finding the


element equal to the key, return some value to
represent that the element is not found.
main()
{
int array[100], key, i, n, count = 0;
printf("Enter number of elements in array\n");
scanf("%d", &n);

printf("Enter %d numbers\n", n);


for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &key);
for (i= 0; i< n; i++) {
if (array[i] == key) {
printf("%d is present at location a[%d].\n", key, i);
count++;
}
}
if (count == 0)
printf("%d isn't present in the array.\n", key);
else
printf("%d is present %d times in the array.\n", key, count);
}
Binary Search
Binary Search
• It follows the divide and conquer approach.
• Works on sorted list
• Can be implemented using iterative and
recursive method.
Binary Search - Algorithm
binarySearch(arr, x, low, high)
if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid]
return binarySearch(arr, x, mid + 1, high)
else
return binarySearch(arr, x, low, mid - 1)
Binary Search - logic
int binarySearch(int a[], int key, int low, int high)
{
if (low<= high)
{
int mid = (low + high) / 2;

if (a [mid] == key) // If key is a [mid], then return it


return mid;

if (a[mid] > key) // Search the left half


return binarySearch(a, key, low, mid - 1);

return binarySearch(a, key, mid + 1, high); // Search the right half


}
}
SELECTION SORT
SELECTION SORT
• Simplest sorting algorithm.
• In selection sort, the smallest value among the
unsorted elements of the array is selected in
every pass
• then smallest value is placed to its appropriate
position into the array.
SELECTION SORT
Procedure:
• Choose the first element and treat its index as
minimum
• Compare with all the other elements
– If any minimum value found
– Update the min index
– Then swap the values
Selection Sort - Algorithm
selectionsort(a,n)
{
for i0 to n-2
{
min_index i
for ji+1 to n-1
{
if(a[j] < a[min_index]
min_index  j
}
temp a[i]
a[i]  a[min_index]
a[min_index] temp
}
}
INSERTION SORT
Insertion Sort - Algorithm
• If the element is the first element, assume that it is
already sorted. Return 1.

• Pick the next element, and store it separately in a temp.


• Now, compare the temp with all elements in the sorted
array.
– If the element in the sorted array is smaller than the current
element, then move to the next element.
– Else, shift greater elements in the array towards the right.
– Insert the value.

• Repeat until the array is sorted


INSERTION SORT
void insert(int a[], int n) {
int i, j, temp;
for (j = 1; j < n; j++) {
temp = a[j];
i = j - 1;
while(i>=0 && temp <= a[i])
{
a[i+1] = a[i];
i= i-1;
}
a[i+1] = temp;
}
}
Time Complexity
Case Time Complexity
Best Case O(n)
Average Case O(n2)
Worst Case O(n2)
QUICK SORT
Quick Sort
1. Make a list of items that need to be sorted, lets apply
in an array.

2. Choose any element as pivot element from the array


list. (Complexity largely depends on choosing the
pivot element). So select first element as pivot

3. Select left(i) and right (j) values.


• Value at left index should be always less than or
equal to pivot element . If a[i] <=a[ pivot] then i++.
• Value at right index should be always greater than
pivot element . If a[j] > a[pivot] then j--.

Recursively repeat the 3rdstep


Quick Sort
4. If i and j are failed to increment/decrement
and
• i < j, then swap values of i and j index.
• j <= i, then swap pivot element with value at
i Index.
• Pivot element is placed in its right position.

5. Do the same procedure (step 1 to 4) for the


sub arrays
void main()
{
int a[20],n,i;

printf("Enter size of the array: ");


scanf("%d",&n);

printf("Enter %d elements: ",n);


for(i=0;i<n;i++)
scanf("%d",&a[i]);

quicksort(a,0,n-1);
printf("Sorted elements: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
}
void quicksort(int x[10],int first,int last) temp=x[pivot];
{
x[pivot]=x[j];
int pivot,j,temp,i; x[j]=temp;
if(first<last)
{ quicksort(x,first,j-1);
pivot=first; quicksort(x,j+1,last);
i=first;
j=last; } \\end if condition
} \\end function
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
} \\end of while loop
if __name__ == "__main__":
def quicksort(arr, first, last):
n = int(input("Enter size of the array: "))
if first < last:
a = [0] * n
pivot = first
i = first
print(f"Enter {n} elements:")
j = last
for i in range(n):
a[i] = int(input())
while i < j:
while arr[i] <= arr[pivot] and i < last:
quicksort(a, 0, n - 1)
i += 1
while arr[j] > arr[pivot]:
print("Sorted elements:", end=" ")
j -= 1
for i in range(n):
if i < j:
print(a[i], end=" ")
arr[i], arr[j] = arr[j], arr[i]

arr[pivot], arr[j] = arr[j], arr[pivot]

quicksort(arr, first, j - 1)
quicksort(arr, j + 1, last)
Complexity of Quick Sort
Worst Case : O(N^2)
• This happens when the pivot is the smallest or
the largest element. Then one of the partition is
empty and we repeat the recursion for N-1
elements
Best Case: O(NlogN)
• This is when the pivot is the median of the
array and the left and right part are the of the
same size.
• There are logN partitions and to compare we do
N comparisons
MERGE SORT
void main()
{
int a[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements to sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
partition(a,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
merge(arr,low,mid,high);
}
}
void merge(int arr[],int low,int mid,int high)
{
int i,m,k,l,res[MAX]; l=low; i=low; m=mid+1;
while((l<=mid)&&(m<=high))
{
if(arr[l]<=arr[m])
{
res[i]=arr[l];
l++;
}
else
{
res[i]=arr[m]; m++;
}
i++;
}}
if(l>mid)
{
for(k=m; k<=high ;k++)
{
res[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
res[i]=arr[k];
i++;
}
}
MERGE SORT – understanding code

#include<stdio.h>
int mergesort(int*,int,int);
void merge(int*,int,int,int);
int main()
{
int a[5]={3,5,2,6,8};
int i;
mergesort(a,0,4); 3 5 2 6 8
for(i=0;i<5;i++)
printf("\n%d",a[i]);
return 0;
}
int mergesort(int a[], int low, int high) int mergesort(int a[], int low, int high)
{ 0, 4 { 0, 0
int mid; int mid;
if(low<high) 3 5 2 6 8 if(low<high)(0<0-false)
{ {
mid=(low+high)/2;(2) mid=(low+high)/2;
mergesort(a,low,mid);(a,0,2) mergesort(a,low,mid);
mergesort(a,mid+1,high); mergesort(a,mid+1,high);
merge(a,low,high,mid); merge(a,low,high,mid);
} }
return(0); return(0);(now return will give control to
} Nearest function call)
}
int mergesort(int a[], int low, int high) int mergesort(int a[], int low, int high)
{ 0, 2 { 0, 1
int mid; int mid;
if(low<high) if(low<high)
{ {
mid=(low+high)/2;(1) mid=(low+high)/2;(0)
mergesort(a,low,mid);(0,1) mergesort(a,low,mid);(0,0)
mergesort(a,mid+1,high); mergesort(a,mid+1,high);
merge(a,low,high,mid); merge(a,low,high,mid);
} }
return(0); return(0);
} }
int mergesort(int a[], int low, int high) int mergesort(int a[], int low, int high)
{ 0,1 { 0,0
int mid; int mid;
if(low<high) if(low<high)(0<0-false)
{ {
mid=(low+high)/2;(0) mid=(low+high)/2;
mergesort(a,low,mid); mergesort(a,low,mid);
mergesort(a,mid+1,high); mergesort(a,mid+1,high);
merge(a,low,high,mid); merge(a,low,high,mid);
} 0,1,0 }
return(0); return(0);
} }
int mergesort(int a[], int low, int high) int mergesort(int a[], int low, int high)
{ (1,1) { 0,1
int mid; int mid;
if(low<high)(1<1-false) if(low<high)
{ {
mid=(low+high)/2; mid=(low+high)/2;(0)
mergesort(a,low,mid); mergesort(a,low,mid);(0,0)
mergesort(a,mid+1,high); mergesort(a,mid+1,high);(0+1,1)
merge(a,low,high,mid); merge(a,low,high,mid);
} }
return(0); return(0);
} }
Until now array has been
divided into 1 element each.
Now array is merged and
sorted in the merge function
Conclusion
Advantages

• One of the fastest algorithm on average.


• Doesn't need additional memory i.e. it's an in-
place sorting algorithm.

Disadvantages

• Worst Case complexity is O(N^2).


Radix Sort
 In Radix sort, there is digit by digit sorting is
performed that is started from the least
significant digit to the most significant digit.
In the decimal system we normally use, there
are 10 different digits from 0 till 9.
Radix Sort is a non comparative algorithm
that only works with non negative integers.
Radix Sort – Algorithm
radixSort(arr)
max = largest element in the given array
d = number of digits in the largest element (or, max)
Now, create d buckets of size 0 - 9
for i -> 0 to d
sort the array elements using counting sort accordi
ng to the digits at the ith place
Radix Sort
Radix Sort – time complexity
• The time complexity for Radix Sort is:
Worst case: O(n⋅k)
Best case: O(n+k)
N – no.of input values
K – no.of digits
Important Questions
 What is meant by Sorting and searching?
 Differentiate linear search and binary search.
 Explain different sorting algorithms used in
data structure.
 List the Real time Applications of Searching
and Sorting Techniques.
Important Questions
Complete the below logic of insertion sort
void insertionSort(int a[], int n)
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = _____;
j = i - 1;

while(j>=0 && temp <= a[j])


{
_____ = _____;
j = j-1;
}
_____ = temp;
}
}
Important Questions
 Searching  Searching and Sorting
 Procedure
 Linear search
 Step by step Solution of given
 Binary search problem / Example
 Algorithm
 Sorting
 C Program (Write output
 Insertion compulsory)
 Selection  Python code
 Real Time applications
 Merge
 Time complexity with Justification
 Quick (by default you have to write)
 Radix  Advantages / Disadvantages

You might also like