Unit 4
Unit 4
Unit 4
Searching
– Linear search
- binary search
Sorting
– Bubble sort
- selection sort
- Insertion sort
- Quick sort
- merge sort.
1
Searching and Sorting
• Searching is the process of finding a particular element in a list
• Sorting is the process of rearranging the elements in a list so that
they are stored in some well-defined order
Searching Algorithms
• Linear search: the search starts at the beginning of the list and
goes straight down the line of elements until it finds a match or
reaches the end of the list
• Binary search: the search starts at the center of a sorted list and
determines which half to continue to search on that basis
Linear Search
• The most basic
• Very easy to implement
• The list DOESN’T have to be sorted
• All list elements must be visited if the search fails
2
• Could be very slow
Example: Successful Linear Search
3
Example: Failed Linear Search
5
Linear Search Implementation
#include <stdio.h> /* Searches for target in an array using Linear
#define SIZE 8 search;
int linear_search(double a[], double target, int size); * Returns index of target or -1 if not found */
void read_array(double a[], int size);
int linear_search(double a[], double target,
int main(void) { int size)
double x[SIZE], target; {
int index; int i, found = 0, location;
read_array(x, SIZE); i = 0;
printf("Enter Element to search for: "); while (!found && i < size) {
scanf("%lf", &target); if (a[i] == target)
index = linear_search(x, target, SIZE); found = 1;
if (index != -1) else
printf("Target was found at index %d\n", index); ++i;
else }
printf("Sorry, target item was not found");
system("pause"); if (found)
return 0; location = i;
} else
void read_array (double a[], int size) { location = -1;
int i;
printf("Enter %d integer numbers separated by return location;
blanks\n> ", size); }
for (i = 0; i < size; ++i)
scanf("%lf", &a[i]); 6
}
Efficiency of Linear Search
• Big O Notation
– Indicates the worst-case run time for an algorithm
– In other words, how hard an algorithm has to work to solve a
problem
– Constant run time
• O(1)
• Does not grow as the size of the array increases
– Linear run time
• O(n)
• Grows proportional to the size of the array
– Logarithmic run time
• O(log n)
• Grows logarithmic proportional to the size of the array
– Quadratic run time
• O(n2)
• Grows proportional to the square of the size of the array
7
For Linear Search algorithm :O(n)
Binary Search
The algorithm starts searching with the middle element.
– If the item is less than the middle element, it starts over
searching the first half of the list.
– If the item is greater than the middle element, the search
starts over starting with the middle element in the second half
of the list.
– It then continues halving the list until the item is found.
• Each iteration eliminates half of the remaining elements
• At each step it splits the remaining array elements into two groups
Therefore, it is faster than the linear search
11
Bubble Sort Implementation
#include <stdio.h> void bubble_sort(double a[], int size) {
#define SIZE 10 int i,j, pass = 1, swap_occurs,temp;
47
Selection Sort Implementation
int find_min(double a[], int start, int size) {
#include <stdio.h>
#define SIZE 10 int i, min_index = start;
49
#include "stdio.h" void main()
#include "conio.h" {
void isort(int a[],int n) int a[100],i,n;
{ clrscr();
int i,j,index; printf("\n Enter the no.of
int min; elements:");
for(i = 1; i < n; i ++) scanf("%d",&n);
{ printf("\n Enter the elements :");
min = a[i]; for(i = 0; i < n; i++)
index = i; scanf("%d",&a[i]);
for(j = i; j > 0 ; j--) isort(a,n);
If(a[j-1] > min) printf("\n The elements in sorted
{ order is :\n");
a[j] = a[j -1]; for(i = 0; i < n; i++)
index = j-1; printf(" %d ",a[i]);
} getch();
a[index] = min; }
}
}
50
Mergesort
• Split array A[0..n-1] into about equal halves and make
copies of each half in arrays B and C
• Sort arrays B and C recursively
• Merge sorted arrays B and C into array A as follows:
– Repeat the following until no elements remain in one
of the arrays:
• compare the first elements in the remaining
unprocessed portions of the arrays
• copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
portion of that array
– Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the
other array into A.
Merge Sort
52
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
mid h
l
m
0 1 2 3 4 5 6 7
17 12 24 65 86 57 98 49
l m h
0 1 2 3 4 5 6 7
17 21 26 67 84 55 98 94
0
l,m 1 h 2 3 4 5 6 7
17 71 22 66 58 58 94 94
7 1 2 6 8 5 9 4
void main()
{
int i,n,a[100];
clrscr();
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("\n Enter the elements :\n");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
}
55
void merge(int a[],int l,int h,int m)
#include "stdio.h" {
#include "conio.h" int c[100],i,j,k;
void merge(int [],int,int,int); i = l; j = m + 1; k = l;
void mergesort(int a[],int low,int high) while(i <= m && j <= h)
{ {
int mid; if(a[i] < a[j])
if(low < high) {
{ c[k] = a[i];
mid = (low + high)/2; i++; k++;
mergesort(a,low,mid); }
mergesort(a,mid+1,high); else
merge(a,low,high,mid); {
} c[k] = a[j];
} j++; k++;
}
}
A[i]≤p A[i]>p 57
Recursive implementation with the left most array entry selected as
the pivot element.
58
#include "studio"
#include "conio.h"
void main()
{
int i,n,a[100];
clrscr();
printf("\n Enter the size of the array :");
scanf("%d",&n);
printf("\n Enter the elements :\n");
for(i = 0; i < n; i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);