Dsa CH 2
Dsa CH 2
Dsa CH 2
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.
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]
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
Disadvantages