SlideShare a Scribd company logo
1 of 98
Unit-IVUnit-IV
1.Searching techniques1.Searching techniques
linear search methodlinear search method
binary search methodbinary search method..
HashingHashing
Hash table representationHash table representation
Hash functionsHash functions
Collision resolution techniquesCollision resolution techniques
Chaining, linear probing,Chaining, linear probing,
quadratic probing, double hashingquadratic probing, double hashing..
 Sorting techniquesSorting techniques
bubble sortbubble sort
selection sort.selection sort.
insertion sort.insertion sort.
quick sortquick sort
merge sortmerge sort..Sathi Durga Devi, Dept of IT
Searching and SortingSearching and Sorting
• Searching is the process of finding a particular element in an array
• Sorting is the process of rearranging the elements in an array so that they are stored
in some well-defined order
Searching AlgorithmsSearching Algorithms
• Linear search: the search starts at the beginning of the array and goes
straight down the line of elements until it finds a match or reaches the
end of the array
• Binary search: the search starts at the center of a sorted array and
determines which half to continue to search on that basis
Sathi Durga Devi, Dept of IT
Linearsequential Search
• linearsequential search of a
list/array begins at the beginning of
the list/array and continues until the
item is found or the entire list/array
has been searched
Sathi Durga Devi, Dept of IT
Example: Successful Linear Search
Sathi Durga Devi, Dept of IT
Example: Failed Linear Search
Sathi Durga Devi, Dept of IT
Linear Search Implementation using non recursive method
#include <stdio.h>
#define SIZE 8
int linear_search(int a[], int target, int size);
void read_array(int a[], int size);
int main(void) {
int x[SIZE], target;
int index;
read_array(x, SIZE);
printf("Enter Element to search for: ");
scanf("%d", &target);
index = linear_search(x, target, SIZE);
if (index != 0)
printf("Target was found at index %dn",
index);
else
printf("Sorry, target item was not
found");
return 0;
}
void read_array (int a[], int size) {
int i;
printf("Enter %d integer numbers separated
by blanksn> ", size);
for (i = 0; i < size; ++i)
scanf("%d", &a[i]);
/* Searches for an target in an array using
Linear search;
* Returns index of target or -1 if not found */
int linear_search(int a[], int target, int size)
{
int i,loc=0;
for(i=0;i<SIZE;i++)
{
if(target==a[i])
return ++loc;
else
loc++;
}
return 0;
}
Sathi Durga Devi, Dept of IT
/* C program that use recursive function to perform the Linear
Search for a Key value in a given list of integers*/
#include<stdio.h> #define SIZE 5
int linearSearch(int array[], int index, int length, int value);
void main() {
int list[SIZE],element,i,target,index=0;
printf("nnEnter %d integer elements: ",SIZE);
for(i = 0; i < SIZE; i++) {
scanf("%d",&list[i]);
}
printf("nEnter target element to be searched: ");
scanf("%d",&target);
element = linearSearch( list,index,SIZE,target);
if( element != -1 )
printf("nElement is found at %d location ",element+1);
else
printf("Element is not found...");
}
Sathi Durga Devi, Dept of IT
int linearSearch(int array[], int index,int length, int value)
{
if(index>length-1)
return -1;
else
if (array[index]==value)
return index;
else
return linearSearch( array,index+1,length,
value);
}
Sathi Durga Devi, Dept of IT
Search Algorithms
-All the array elements must be visited if search fails.
Sathi Durga Devi, Dept of IT
Efficiency of Linear Search
• The efficiency of an algorithm is measured using the big O
notation ( O stands for order of )
• Big O Notation
– Indicates the worst-case run time (maximum time taken for
execution) for an algorithm
– In other words, how hard an algorithm has to work to solve a
problem
For Linear Search algorithm :O(n)
Sathi Durga Devi, Dept of IT
Binary Search: The search starts at middle of a
sorted array, if middle is equal to target element search
is successful otherwise it determines which half to be
continue to search on that basis.
 The algorithm starts searching with the mid element.
mid=(first + last)/2
 If the item is equal to mid then search is successful.
 If the item is less than the mid element, it starts over
searching the first half of the list.
 If the item is greater than the mid element, the search
starts over 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.
 It is faster than the linear search.
 It works only on SORTED array.
 Thus, there is a performance penalty for sorting the array.
 The Time complexity of Binary Search is O(log N).
Sathi Durga Devi, Dept of IT
Sathi Durga Devi, Dept of IT
/* C program that use recursive function to perform the Binary Search
for a Key value in a given list of integers*/
#include <stdio.h> #define SIZE 8
int binary_search (int list[], int low, int high, int target);
void main() {
int list[SIZE], target, index,i;
printf("Enter %d elements in ascending or descending order: ",SIZE);
for(i=0;i<SIZE;i++)
scanf("%d",&list[i]);
printf("Enter an element that is to be searched: ");
scanf("%d", &target);
index = binary_search(list, 0, SIZE-1, target);
if (index != -1)
printf("nTarget was found at index: %d ", index+1);
else
printf("Sorry, target item was not found");
}
Sathi Durga Devi, Dept of IT
int binary_search (int list[], int low, int high, int target)
{
int middle;
if (low > high)
return -1;
middle = (low + high)/2;
if (list[middle] == target)
return (middle);
else
if (list[middle] < target)
return binary_search(list,middle+1,high,target);
else
return binary_search(list,low,middle-1,target);
}
Sathi Durga Devi, Dept of IT
/* C program that use non recursive function to perform the Binary
Search for a Key value in a given list of integers*/
#include <stdio.h>
#define SIZE 8
int binary_search (int list[], int low, int high, int target);
void main() {
int list[SIZE], target, index,i;
printf(“nenter the array elements”);
for(i=0;i<SIZE;i++)
scanf("%d",&list[i]);
printf(“n enter the target element");
scanf("%d", &target);
index = binary_search(list, 0, SIZE-1, target);
if (index != -1)
printf("nelement atlocation %d ", index+1);
else
printf("Sorry, target item was not found");
getch();
} Sathi Durga Devi, Dept of IT
int binary_search(int a[],int low, int high, int target)
{
int middle;
while(low<=high)
{
middle=(low+high)/2;
if(target<a[middle])
high=middle-1;
else if(target>a[middle])
low=middle+1;
else
return middle;
}//while
return -1;
}//binary_search()
Sathi Durga Devi, Dept of IT
Hashing
Sathi Durga Devi, Dept of IT
Why hashing?
• Internet has grown to millions of users and terabytes of
data every data.
• It is impossible to find anything in the internet, unless we
develop a new data structure to store and access the data
very quickly.
• The amount of time required to look up an element in an
array or linked list is either O(logn) or O(n) based on the list
is sorted or not.
• New data structure called Hashing used to store and
retrieve any entry with constant time O(1).
• This technique is irrelevant to size of the list and order.
• To increase the search efficiency the items to be stored in
such a way as to make it easy to find them later.
Sathi Durga Devi, Dept of IT
Hashing
- Hashing is a technique used to generate key where an
element is to be inserted or to be located from.
• Hash Table
- hash table is a data structure to store and retrieve data
very fast.
- hash table consist of key and its value.
- Each location in the hash table is called cell or bucket.
- hash table is implemented using array.
- An element is accessed very fast if we know the key or its
index.
Example- to store the Student record in hash table, Student
rollno is used as a key
Sathi Durga Devi, Dept of IT
Hash Function
- to map the key value into its corresponding index in hash
table hash function is used.
A hash function h transforms a key into an index in a hash
table T[0…m-1]:
Where m is size of hash table.
-Use hash function to compute the index in the hash table for the given
key value.
-Hash function returns integer value which give the index value in the
hash table.
Sathi Durga Devi, Dept of IT
Types of hash functions
1. Division method
2. Mid square
3. Digit folding
Sathi Durga Devi, Dept of IT
1. Division method
h(key)= record%M
where M is size of the hash table
Example:
store following records in hash table 34, 20, 67, 8, 23.
M is 10
use hash function to map them in hash table
0 20
1
2
3 23
4 34
5
6
7 67
8 8
9
34%10= 4
20%10=0
67%10=7
8%10=8
23%10=3
Sathi Durga Devi, Dept of IT
Consider the following elements to be placed in the hash table
of size 10,
37,90,45,22,17,49,55
37
45
90
22
H1(37)=37%10=7
H1(90)=90%10=0
H1(45)=45%10=5
H1(22)=22%10=2
H1(17)=17%10=7
H1(49)=49%10=9
H1(55)=55%10=5
In above example 17 and 55 are hashed to
same location this condition is called collision
Sathi Durga Devi, Dept of IT
2. Mid square
- Square the key value and the middle or mid part of the
result is used as index.
- Example to place a record 3111 then
- Square of 3111= 9678321
- If the hash table size is 1000 then consider the middle 3
digits 783.
Sathi Durga Devi, Dept of IT
K 3205 7148
k2
10272025 51093904
H(k) 72 93
3. Folding method
- Key value is divided into parts and add
them yields required hash address.
Sathi Durga Devi, Dept of IT
Key 3205 7148
H(key) 32+05=37 71+48=19
Note- the leading digit 1 in H(7148) is ignored.
Collision resolution techniques
• Two or more keys are mapping to same location in the hash
table is called collision.
Collision resolution techniques
• They are two broad ways of collision resolution techniques
1. Separate chaining: an array of linked list representation
2. Open addressing: array based implementation
(i) Linear probing (linear search)
(ii) Quadratic probing (nonlinear search)
(iii) Double hashing (uses two hash functions
Sathi Durga Devi, Dept of IT
Separate chaining
• Hash table is implemented as array of linked list.
• All the records which are mapped to same hash address are
lined together to form a linked list.
• Example: Load the keys 23, 13, 21, 14, 7, 8, and 15 , in
this order, in a hash table of size 7 using separate chaining
with the hash function: h(key) = key % 7
h(23) = 23 % 7 = 2
h(13) = 13 % 7 = 6
h(21) = 21 % 7 = 0
h(14) = 14 % 7 = 0 collision
h(7) = 7 % 7 = 0 collision
h(8) = 8 % 7 = 1
h(15) = 15 % 7 = 1 collision
Sathi Durga Devi, Dept of IT
Linear probing (linear search)
• Idea is that when the collision occurs, find the next
available slot in the hash table (i.e probing)
• The process wraps around to the beginning of the table
• Example 89, 18, 49, 58, 9 and hash table size is 10
0 1 2 3 4 5 6 7 8 9
891849 58 9
89%10=9
18%10=8
49%10=9
58%10=8
9%10=9 Sathi Durga Devi, Dept of IT
3. Quadratic probing
- It operates by taking hash value and adding successive values of
an arbitrary quadratic polynomial.
- Uses following formula
Sathi Durga Devi, Dept of IT
Hi(key)= (Hashvalue+i2
)%m
Where,
m may be table size or any prime number
Ex- 37,99,55,22,11, 17,40,87
Sathi Durga Devi, Dept of IT
0 1 2 3 4 5 6 7 8 9
37%10=7
37
99%10=9
99
55%10=5
55
22%10=2
22
11%10=1
11
17%10=7 but already occupied
Consider i=0 then
(17+02
)%10=7
(17+12
)%10=8
17
40%10=0
40
87%10=7 occupied
(87+12
)%10=8 occupied
(87+22
)%10=1 occupied
(87+32
)%10=6
87
4. Double hashing
- Second hash function is applied when a collision is occurred.
- the resultant of second hash function is to get the number of
positions from the point of collision to insert.
Sathi Durga Devi, Dept of IT
H1(key)= keyvalue%tablesize
H2(key)= M-(key % M)
Where,
M is prime number smaller than table size.
Ex- 37,90,45,22,17,49,55
Sathi Durga Devi, Dept of IT
0 1 2 3 4 5 6 7 8 9
H1(37)=37%10=7
37
H1(90)=90%10=0
90
H1(45)=45%10=5
45
H1(22)=22%10=2
17
H1(17)=17%10=7
H2(17)=7-(17%7)=4
Move 4 locations from collision
H1(55)=55%10=5
H2(55)=7-(55%7)=1
Move one location from collision
H1(49)=49%10=9
495522
Sorting Techniques
1. Bubble sort/exchange sort
2. Insertion sort
3. Selection sort
4. Merge sort
5. Quick sort
Sathi Durga Devi, Dept of IT
Bubble Sort
Sathi Durga Devi, Dept
of IT
Bubble sortexchange sort
• Suppose the list of numbers a[1],a[2],…,a[n] is in memory.
The bubble sort algorithm works as follows.
Step 1- compare a[1] and a[2] and arrange them in the desired
order, so that a[1]<a[2].
then compare a[2] and a[3] and arrange them so that a[2]<a[3].
Then compare a[3] and a[4] and arrange them so that a[3]<
a[4]. Continue until we compare a[n-1] with a[n] and arrange
them so that a[n-1]<a[n].
- Observe that step-1 involves n-1 comparisons. when step-1 is
completed a[n] has the largest element in the list. The
largest element is “bubbled up “ to the nth position.
Step 2- repeat step1 with one less comparision; it involves n-2
comparisions and when step2 is completed the second
largest element will occupy a[n-1].
Step 3- repeat step1 with two fewer comparisons; involves n-3
comparisons.
Step n-1. compare a[1] with a[2] and arrange them so that
a[1]<a[2].
After n-1 steps, the list will be sorted in increasing order.
The process of sequentially traversing through all or part of
the list is frequently called a “pass”Sathi Durga Devi, Dept of IT
Bubble sort
27 32 51 23 13
51 23 13Pass-1
27 23 13
2
7
3
2
3
2
51
27 32 13
51
2
3
27 32 23 51 13
Sathi Durga Devi, Dept of IT
Bubble sort
Pass-2 23 13 512
7
3
2
27 13 513
2
2
3
27 23 3
2
13 51
27 23 13
5132
Sathi Durga Devi, Dept of IT
27 23 13
5132
Pass-3
13
5132
2
7
2
3
23
5132
2
7
13
23 13
513227
Pass-4
513227
2
3
13
Sorted Array after N-1 passes
13 23 27 32 51
Sathi Durga Devi, Dept of IT
/* Write a c program to implement the bubble sort */
#include<stdio.h>
void bubbleSort(int a[],int index,int size);
void main() {
int n,a[10],i;
printf("nnEnter the size of array: ");
scanf("%d",&n);
printf("nnEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("nnElements before sorting: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("n");
bubbleSort(a,0,n);
}
Sathi Durga Devi, Dept of IT
void bubblesort(int a[],int index, int n)
{
int i,x,j,temp;
for(i=1;i<=n-1;i++)
{
printf("n pass- %d n",i);
for(j=index;j<n-i;j++)
{ if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}//if
for(x=0;x<n;x++)
printf("%3d",a[x]);
printf("nn");
}//j
}//i
printf("n elements after sortingn");
for(i=0;i<n;i++)
printf("%3d",a[i]);
}//bublesort
Time complexity- O(n2
)
Sathi Durga Devi, Dept of IT
41
Selection Sort
Procedure:
 Selection sort involved scanning through the list to select the
smallest element and swap it with the first element.
 The rest of the list is then search for the next smallest and
swap it with the second element.
 This process is repeated until the rest of the list reduces to one
element, by which time the list is sorted.
The following table shows how selection sort works.
Sathi Durga Devi, Dept of IT
Pass-1
Pass-2
Pass-3
Pass-4
Pass-5
Pass-6
Pass-7
1122668855443377
1122668855443377
7722668855443311
7733668855442211
7744668855332211
7755668844332211
7788665544332211
7788665544332211
Sathi Durga Devi, Dept of IT
Write a C program to implement Selection sort
#include<stdio.h>
void selectionsort(int a[],int n);
void main()
{
int a[100],i,n;
printf("n enter the size of the array");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
}//main
Sathi Durga Devi, Dept of IT
void selectionsort(int a[],int n)
{
int i,j,k,loc,min,temp;
for(i=0;i<n-1;i++)
{
printf("n");
printf("nstep-%d",i+1);
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;}
}//j
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
for(k=0;k<n;k++)
printf("%3d",a[k]);
}//i
printf("nn sorted array is ");
for(k=0;k<n;k++)
printf("%3d",a[k]);
}//selectionsort
Sathi Durga Devi, Dept of IT
Insertion Sort
• Idea: like sorting a hand of playing cards
– Start with an empty left hand and the cards facing down
on the table.
– Remove one card at a time from the table, and insert it
into the correct position in the left hand
• compare it with each of the cards already in the hand,
from right to left
– The cards held in the left hand are sorted
these cards were originally the top cards of the pile on
the table
Sathi Durga Devi, Dept of IT
Insertion Sort
To insert 12, we need to make
room for it by moving first 36
and then 24.
6 10 24
12
36
Sathi Durga Devi, Dept of IT
Insertion Sort
6 10 24 36
12
Sathi Durga Devi, Dept of IT
Insertion Sort
6 10 24 36
12
Sathi Durga Devi, Dept of IT
49
Insertion Sort
Using insertion sort an element is inserted in correct location.
Insertion sort scans the list from A[0] to A[n-1], insert each element A[k] into its
proper position in previously sorted sub list A[0],A[1],A[2]…A[k-1]..
Procedure:
 A[0] by itself is trivially sorted.
 A[1] is inserted either before or after A[0] so that A[0],A[1] is sorted.
 A[2] is inserted into its proper place in A[0],A[1], that is, before A[0],
between A[0] and A[1], or after A[1] so that A[0],A[1],A[2] is sorted.
 Repeatedly compare A[k] to the element just to the left of the vacancy, and
as long as A[k] is smaller, move that element into the vacancy, else put A[k]
in the vacancy.
 Repeat the next element that has not yet examined.
 Time complexity- O(n2)
 This algorithm is used when n is small.
Sathi Durga Devi, Dept of IT
50
Insertion Sort
Sathi Durga Devi, Dept of IT
Insertion Sort
5 2 4 6 1 3
input array
left sub-array right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
Sathi Durga Devi, Dept of IT
Insertion Sort
Sathi Durga Devi, Dept of IT
/*Write a program to implement the insertion sort*/
#include<stdio.h>
void insertsort(int a[],int n);
void main()
{
int i,a[10],n;
printf("n enter array size");
scanf("%d",&n);
printf("n enter array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("n elements before swapping");
for(i=0;i<n;i++)
printf("%3d",a[i]);
insertsort(a,n);
printf("n elements after swapping");
for(i=0;i<n;i++)
printf("%3d",a[i]);
}//main
Sathi Durga Devi, Dept of IT
void insertsort(int a[],int n)
{
int k,temp,j,i;
//steps
for(k=0;k<n;k++)
{
printf("nstep-%d",k+1);
temp=a[k];
j=k-1;
while((j>=0)&&(temp<=a[j]))
{
a[j+1]=a[j];
a[j]=temp;
j--;
}//while
for(i=0;i<n;i++)
printf("%3d",a[i]);
}//for
}//insertsort
Sathi Durga Devi, Dept of IT
Merge Sort
 This algorithm uses the divide- and – conquer method.
 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.
Sathi Durga Devi, Dept of IT
Merge Sort
Sathi Durga Devi, Dept of IT
mergesort(a,0,7)
mergesort(a,0,3) mergesort(a,4,7)
mergesort(a,0,1) mergesort(a,2,3) mergesort(a,4,5) mergesort(a,6,7)
8 3 2 9 7 1 5 4
mergsort(a,0,0)ms(a,1,1) ms(a,2,2) ms(a,3,3) ms(a,4,4) ms(a,5,5) ms(a,6,6) ms(a,7,7)
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
merge(a,0,1,0) merge(a,2,3,2) merge(a,4,5,4) merge(a,6,7,6)
3 8 2 9 1 7 4 5
merge(a,0,3,1) merge(a,4,7,5)
2 3 8 9 1 4 5 7
merge(a,0,7,3)
1 2 3 4 5 7 8 9
Sathi Durga Devi, Dept of IT
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
Sathi Durga Devi, Dept of IT
#include "stdio.h"
void merge(int [],int,int,int);
void mergesort(int a[],int low,int high)
{
int mid;
if(low < high)
{
mid = (low + high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
} //if
}//mergesort
Mergsort program
Sathi Durga Devi, Dept of IT
void merge(int a[],int l,int h,int m){
int c[100],i,j,k;
i = l; j = m + 1; k = l;
while (i <= m && j <= h)
{
if(a[i] < a[j])
{
c[k] = a[i]; i++; k++;
}//if
else
{
c[k] = a[j]; j++; k++;
} //else
}//while
while(i <= m) c[k++] = a[i++];
while(j <= h) c[k++] = a[j++];
for(i = l; i < k; i++) a[i] = c[i];
}//merge
Sathi Durga Devi, Dept of IT
void main()
{
int i,n,a[100];
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);
printf("n Elements in sorted order :n");
for(i = 0; i < n; i++)
printf("%5d",a[i]);
getch();
}
Sathi Durga Devi, Dept of IT
 This algorithm uses the divide- and – conquer method.
 Select a pivot element from the array of elements.
 Rearrange the list so that all the elements before the pivot are smaller
than or equal to the pivot and those after the pivot are larger than the
pivot .
 After such a partitioning, the pivot is placed in its final position.
 Sort the two sublists recursively.
Quick Sort
Sathi Durga Devi, Dept of IT
Quicksort AlgorithmGiven an array of n elements (e.g., integers):
• If array only contains one element, return
• Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Quicksort two sub-arrays
– Return results
Sathi Durga Devi, Dept of IT
Sathi Durga Devi, Dept of IT
Recursive implementation with the left most array entry selected as
the pivot element.
Sathi Durga Devi, Dept of IT
/*Write a c program to implement the quick sort*/
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("nEnter size of the array: ");
scanf("%d",&size);
printf("nEnter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("nSorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
getch();
return 0;
}
Sathi Durga Devi, Dept of IT
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
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;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. Whilex[i] <= x[pivot]
++i
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 80 60 50 7 30 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
40 20 10 30 60 50 7 80 100pivot_index = 0
i j
1. While x[i] <= x[pivot]
++i
2. While x[j] >x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While data[i] <= data[pivot]
++i
2. While data[j] > data[pivot]
--j
3. If i < j
swap data[i] and data[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
5. Swap x[j] and x[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
i j
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Sathi Durga Devi, Dept of IT
1. While x[i] <= x[pivot]
++i
2. While x[j] > x[pivot]
--j
3. If i < j
swap x[i] and x[j]
4. While j > i, go to 1.
5. Swap x[j] and x[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
i j
Sathi Durga Devi, Dept of IT
Partition Result
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
Sathi Durga Devi, Dept of IT
Recursion: Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
Sathi Durga Devi, Dept of IT
• Example
44,33,11,55,77,90,40,60,99,22,66
Sathi Durga Devi, Dept of IT
Average
Case
Worst Case
Linear Search - O(n)
Binary Search O(log n) O(n)
Bubble Sort O(n2
) O(n2
)
Selection Sort - O(n2
)
Insertion Sort - O(n2
)
Merge Sort O(n log n) O(n log n)
Quick Sort O(n log n) O(n2
)
Time Complexities of Searching & Sorting Algorithms:
 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.
Sathi Durga Devi, Dept of IT
• Selection Sort
– An algorithm which orders items by repeatedly looking through remaining items
to find the least one and moving it to a final location
• Bubble Sort
– Sort by comparing each adjacent pair of items in a list in turn, swapping the
items if necessary, and repeating the pass through the list until no swaps are
done
• Insertion Sort
– Sort by repeatedly taking the next item and inserting it into the final data
structure in its proper order with respect to items already inserted.
• Merge Sort
– An algorithm which splits the items to be sorted into two groups, recursively
sorts each group, and merges them into a final, sorted sequence
• Quick Sort
– An in-place sort algorithm that uses the divide and conquer paradigm. It picks
an element from the array (the pivot), partitions the remaining elements into
those greater than and less than this pivot, and recursively sorts the partitions.
Review of Algorithms
Sathi Durga Devi, Dept of IT
Assignment - 2
1. Write and explain the breath first traversal and depth first traversal in a
graph with algorithm
2. Write notes on spanning tree. Write and explain the algorithms to find minimal
cost.
3. Write recursive functions in C for preorder, inorder and post order traversals of a
binary tree.
4. Explain with an example how an element is deleted from a binary search tree.
5. Construct the binary search for the following data
32, 12,45,6,78,24,33,9,53,3
6. Write a c program to implement the non recursive method for binary search.
7. Write a c program to implement the quick sort.
sort the given elements using quick sort
40,20,10,80,60,50,7,30,100.
8. Explain the procedure for merge sort. Write the time complexities for various
searching and sorting techniques.
Sathi Durga Devi, Dept of IT

More Related Content

What's hot

Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listAndres Mendez-Vazquez
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsHoang Nguyen
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures BasicsDurgaDeviCbit
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 

What's hot (20)

Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Data Structure
Data StructureData Structure
Data Structure
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_list
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Data structures
Data structuresData structures
Data structures
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Data structures
Data structuresData structures
Data structures
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
 

Viewers also liked (17)

Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Search
 
Principles of Management – Chpt 15 : Leadership
Principles of Management – Chpt 15 : LeadershipPrinciples of Management – Chpt 15 : Leadership
Principles of Management – Chpt 15 : Leadership
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Searching
SearchingSearching
Searching
 
File organization
File organizationFile organization
File organization
 
Chapter 15 leadership
Chapter 15 leadershipChapter 15 leadership
Chapter 15 leadership
 
File organisation
File organisationFile organisation
File organisation
 
File organization
File organizationFile organization
File organization
 
verification and validation
verification and validationverification and validation
verification and validation
 
Software testing objective_types
Software testing objective_typesSoftware testing objective_types
Software testing objective_types
 
Problem solving& Decision Making
Problem solving& Decision MakingProblem solving& Decision Making
Problem solving& Decision Making
 
File Organization
File OrganizationFile Organization
File Organization
 
Decision making & problem solving
Decision making & problem solvingDecision making & problem solving
Decision making & problem solving
 
Problem solving & decision making at the workplace
Problem solving & decision making at the workplaceProblem solving & decision making at the workplace
Problem solving & decision making at the workplace
 
15 leadership sample Powerpoint slides
15 leadership sample Powerpoint slides15 leadership sample Powerpoint slides
15 leadership sample Powerpoint slides
 

Similar to Unit iv(dsc++)

Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear searchmontazur420
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptxreddy19841
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxStephenRobert15
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdfAmuthachenthiruK
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfarpitaeron555
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfMustafaJutt4
 
Searching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptxSearching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptxDr. Madhuri Jawale
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptxDr.Shweta
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxMohammed472103
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 

Similar to Unit iv(dsc++) (20)

Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
 
21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx21CS32 DS Module 1 PPT.pptx
21CS32 DS Module 1 PPT.pptx
 
Searching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptxSearching in DSA that follow a dsa searching.pptx
Searching in DSA that follow a dsa searching.pptx
 
Searching
Searching Searching
Searching
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
In the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdfIn the binary search, if the array being searched has 32 elements in.pdf
In the binary search, if the array being searched has 32 elements in.pdf
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Hashing data
Hashing dataHashing data
Hashing data
 
Searching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptxSearching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptx
 
Searching
SearchingSearching
Searching
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 

Recently uploaded

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 

Recently uploaded (20)

(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

Unit iv(dsc++)

  • 1. Unit-IVUnit-IV 1.Searching techniques1.Searching techniques linear search methodlinear search method binary search methodbinary search method.. HashingHashing Hash table representationHash table representation Hash functionsHash functions Collision resolution techniquesCollision resolution techniques Chaining, linear probing,Chaining, linear probing, quadratic probing, double hashingquadratic probing, double hashing..  Sorting techniquesSorting techniques bubble sortbubble sort selection sort.selection sort. insertion sort.insertion sort. quick sortquick sort merge sortmerge sort..Sathi Durga Devi, Dept of IT
  • 2. Searching and SortingSearching and Sorting • Searching is the process of finding a particular element in an array • Sorting is the process of rearranging the elements in an array so that they are stored in some well-defined order Searching AlgorithmsSearching Algorithms • Linear search: the search starts at the beginning of the array and goes straight down the line of elements until it finds a match or reaches the end of the array • Binary search: the search starts at the center of a sorted array and determines which half to continue to search on that basis Sathi Durga Devi, Dept of IT
  • 3. Linearsequential Search • linearsequential search of a list/array begins at the beginning of the list/array and continues until the item is found or the entire list/array has been searched Sathi Durga Devi, Dept of IT
  • 4. Example: Successful Linear Search Sathi Durga Devi, Dept of IT
  • 5. Example: Failed Linear Search Sathi Durga Devi, Dept of IT
  • 6. Linear Search Implementation using non recursive method #include <stdio.h> #define SIZE 8 int linear_search(int a[], int target, int size); void read_array(int a[], int size); int main(void) { int x[SIZE], target; int index; read_array(x, SIZE); printf("Enter Element to search for: "); scanf("%d", &target); index = linear_search(x, target, SIZE); if (index != 0) printf("Target was found at index %dn", index); else printf("Sorry, target item was not found"); return 0; } void read_array (int a[], int size) { int i; printf("Enter %d integer numbers separated by blanksn> ", size); for (i = 0; i < size; ++i) scanf("%d", &a[i]); /* Searches for an target in an array using Linear search; * Returns index of target or -1 if not found */ int linear_search(int a[], int target, int size) { int i,loc=0; for(i=0;i<SIZE;i++) { if(target==a[i]) return ++loc; else loc++; } return 0; } Sathi Durga Devi, Dept of IT
  • 7. /* C program that use recursive function to perform the Linear Search for a Key value in a given list of integers*/ #include<stdio.h> #define SIZE 5 int linearSearch(int array[], int index, int length, int value); void main() { int list[SIZE],element,i,target,index=0; printf("nnEnter %d integer elements: ",SIZE); for(i = 0; i < SIZE; i++) { scanf("%d",&list[i]); } printf("nEnter target element to be searched: "); scanf("%d",&target); element = linearSearch( list,index,SIZE,target); if( element != -1 ) printf("nElement is found at %d location ",element+1); else printf("Element is not found..."); } Sathi Durga Devi, Dept of IT
  • 8. int linearSearch(int array[], int index,int length, int value) { if(index>length-1) return -1; else if (array[index]==value) return index; else return linearSearch( array,index+1,length, value); } Sathi Durga Devi, Dept of IT
  • 9. Search Algorithms -All the array elements must be visited if search fails. Sathi Durga Devi, Dept of IT
  • 10. Efficiency of Linear Search • The efficiency of an algorithm is measured using the big O notation ( O stands for order of ) • Big O Notation – Indicates the worst-case run time (maximum time taken for execution) for an algorithm – In other words, how hard an algorithm has to work to solve a problem For Linear Search algorithm :O(n) Sathi Durga Devi, Dept of IT
  • 11. Binary Search: The search starts at middle of a sorted array, if middle is equal to target element search is successful otherwise it determines which half to be continue to search on that basis.  The algorithm starts searching with the mid element. mid=(first + last)/2  If the item is equal to mid then search is successful.  If the item is less than the mid element, it starts over searching the first half of the list.  If the item is greater than the mid element, the search starts over 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.  It is faster than the linear search.  It works only on SORTED array.  Thus, there is a performance penalty for sorting the array.  The Time complexity of Binary Search is O(log N). Sathi Durga Devi, Dept of IT
  • 12. Sathi Durga Devi, Dept of IT
  • 13. /* C program that use recursive function to perform the Binary Search for a Key value in a given list of integers*/ #include <stdio.h> #define SIZE 8 int binary_search (int list[], int low, int high, int target); void main() { int list[SIZE], target, index,i; printf("Enter %d elements in ascending or descending order: ",SIZE); for(i=0;i<SIZE;i++) scanf("%d",&list[i]); printf("Enter an element that is to be searched: "); scanf("%d", &target); index = binary_search(list, 0, SIZE-1, target); if (index != -1) printf("nTarget was found at index: %d ", index+1); else printf("Sorry, target item was not found"); } Sathi Durga Devi, Dept of IT
  • 14. int binary_search (int list[], int low, int high, int target) { int middle; if (low > high) return -1; middle = (low + high)/2; if (list[middle] == target) return (middle); else if (list[middle] < target) return binary_search(list,middle+1,high,target); else return binary_search(list,low,middle-1,target); } Sathi Durga Devi, Dept of IT
  • 15. /* C program that use non recursive function to perform the Binary Search for a Key value in a given list of integers*/ #include <stdio.h> #define SIZE 8 int binary_search (int list[], int low, int high, int target); void main() { int list[SIZE], target, index,i; printf(“nenter the array elements”); for(i=0;i<SIZE;i++) scanf("%d",&list[i]); printf(“n enter the target element"); scanf("%d", &target); index = binary_search(list, 0, SIZE-1, target); if (index != -1) printf("nelement atlocation %d ", index+1); else printf("Sorry, target item was not found"); getch(); } Sathi Durga Devi, Dept of IT
  • 16. int binary_search(int a[],int low, int high, int target) { int middle; while(low<=high) { middle=(low+high)/2; if(target<a[middle]) high=middle-1; else if(target>a[middle]) low=middle+1; else return middle; }//while return -1; }//binary_search() Sathi Durga Devi, Dept of IT
  • 18. Why hashing? • Internet has grown to millions of users and terabytes of data every data. • It is impossible to find anything in the internet, unless we develop a new data structure to store and access the data very quickly. • The amount of time required to look up an element in an array or linked list is either O(logn) or O(n) based on the list is sorted or not. • New data structure called Hashing used to store and retrieve any entry with constant time O(1). • This technique is irrelevant to size of the list and order. • To increase the search efficiency the items to be stored in such a way as to make it easy to find them later. Sathi Durga Devi, Dept of IT
  • 19. Hashing - Hashing is a technique used to generate key where an element is to be inserted or to be located from. • Hash Table - hash table is a data structure to store and retrieve data very fast. - hash table consist of key and its value. - Each location in the hash table is called cell or bucket. - hash table is implemented using array. - An element is accessed very fast if we know the key or its index. Example- to store the Student record in hash table, Student rollno is used as a key Sathi Durga Devi, Dept of IT
  • 20. Hash Function - to map the key value into its corresponding index in hash table hash function is used. A hash function h transforms a key into an index in a hash table T[0…m-1]: Where m is size of hash table. -Use hash function to compute the index in the hash table for the given key value. -Hash function returns integer value which give the index value in the hash table. Sathi Durga Devi, Dept of IT
  • 21. Types of hash functions 1. Division method 2. Mid square 3. Digit folding Sathi Durga Devi, Dept of IT
  • 22. 1. Division method h(key)= record%M where M is size of the hash table Example: store following records in hash table 34, 20, 67, 8, 23. M is 10 use hash function to map them in hash table 0 20 1 2 3 23 4 34 5 6 7 67 8 8 9 34%10= 4 20%10=0 67%10=7 8%10=8 23%10=3 Sathi Durga Devi, Dept of IT
  • 23. Consider the following elements to be placed in the hash table of size 10, 37,90,45,22,17,49,55 37 45 90 22 H1(37)=37%10=7 H1(90)=90%10=0 H1(45)=45%10=5 H1(22)=22%10=2 H1(17)=17%10=7 H1(49)=49%10=9 H1(55)=55%10=5 In above example 17 and 55 are hashed to same location this condition is called collision Sathi Durga Devi, Dept of IT
  • 24. 2. Mid square - Square the key value and the middle or mid part of the result is used as index. - Example to place a record 3111 then - Square of 3111= 9678321 - If the hash table size is 1000 then consider the middle 3 digits 783. Sathi Durga Devi, Dept of IT K 3205 7148 k2 10272025 51093904 H(k) 72 93
  • 25. 3. Folding method - Key value is divided into parts and add them yields required hash address. Sathi Durga Devi, Dept of IT Key 3205 7148 H(key) 32+05=37 71+48=19 Note- the leading digit 1 in H(7148) is ignored.
  • 26. Collision resolution techniques • Two or more keys are mapping to same location in the hash table is called collision. Collision resolution techniques • They are two broad ways of collision resolution techniques 1. Separate chaining: an array of linked list representation 2. Open addressing: array based implementation (i) Linear probing (linear search) (ii) Quadratic probing (nonlinear search) (iii) Double hashing (uses two hash functions Sathi Durga Devi, Dept of IT
  • 27. Separate chaining • Hash table is implemented as array of linked list. • All the records which are mapped to same hash address are lined together to form a linked list. • Example: Load the keys 23, 13, 21, 14, 7, 8, and 15 , in this order, in a hash table of size 7 using separate chaining with the hash function: h(key) = key % 7 h(23) = 23 % 7 = 2 h(13) = 13 % 7 = 6 h(21) = 21 % 7 = 0 h(14) = 14 % 7 = 0 collision h(7) = 7 % 7 = 0 collision h(8) = 8 % 7 = 1 h(15) = 15 % 7 = 1 collision Sathi Durga Devi, Dept of IT
  • 28. Linear probing (linear search) • Idea is that when the collision occurs, find the next available slot in the hash table (i.e probing) • The process wraps around to the beginning of the table • Example 89, 18, 49, 58, 9 and hash table size is 10 0 1 2 3 4 5 6 7 8 9 891849 58 9 89%10=9 18%10=8 49%10=9 58%10=8 9%10=9 Sathi Durga Devi, Dept of IT
  • 29. 3. Quadratic probing - It operates by taking hash value and adding successive values of an arbitrary quadratic polynomial. - Uses following formula Sathi Durga Devi, Dept of IT Hi(key)= (Hashvalue+i2 )%m Where, m may be table size or any prime number
  • 30. Ex- 37,99,55,22,11, 17,40,87 Sathi Durga Devi, Dept of IT 0 1 2 3 4 5 6 7 8 9 37%10=7 37 99%10=9 99 55%10=5 55 22%10=2 22 11%10=1 11 17%10=7 but already occupied Consider i=0 then (17+02 )%10=7 (17+12 )%10=8 17 40%10=0 40 87%10=7 occupied (87+12 )%10=8 occupied (87+22 )%10=1 occupied (87+32 )%10=6 87
  • 31. 4. Double hashing - Second hash function is applied when a collision is occurred. - the resultant of second hash function is to get the number of positions from the point of collision to insert. Sathi Durga Devi, Dept of IT H1(key)= keyvalue%tablesize H2(key)= M-(key % M) Where, M is prime number smaller than table size.
  • 32. Ex- 37,90,45,22,17,49,55 Sathi Durga Devi, Dept of IT 0 1 2 3 4 5 6 7 8 9 H1(37)=37%10=7 37 H1(90)=90%10=0 90 H1(45)=45%10=5 45 H1(22)=22%10=2 17 H1(17)=17%10=7 H2(17)=7-(17%7)=4 Move 4 locations from collision H1(55)=55%10=5 H2(55)=7-(55%7)=1 Move one location from collision H1(49)=49%10=9 495522
  • 33. Sorting Techniques 1. Bubble sort/exchange sort 2. Insertion sort 3. Selection sort 4. Merge sort 5. Quick sort Sathi Durga Devi, Dept of IT
  • 34. Bubble Sort Sathi Durga Devi, Dept of IT
  • 35. Bubble sortexchange sort • Suppose the list of numbers a[1],a[2],…,a[n] is in memory. The bubble sort algorithm works as follows. Step 1- compare a[1] and a[2] and arrange them in the desired order, so that a[1]<a[2]. then compare a[2] and a[3] and arrange them so that a[2]<a[3]. Then compare a[3] and a[4] and arrange them so that a[3]< a[4]. Continue until we compare a[n-1] with a[n] and arrange them so that a[n-1]<a[n]. - Observe that step-1 involves n-1 comparisons. when step-1 is completed a[n] has the largest element in the list. The largest element is “bubbled up “ to the nth position. Step 2- repeat step1 with one less comparision; it involves n-2 comparisions and when step2 is completed the second largest element will occupy a[n-1]. Step 3- repeat step1 with two fewer comparisons; involves n-3 comparisons. Step n-1. compare a[1] with a[2] and arrange them so that a[1]<a[2]. After n-1 steps, the list will be sorted in increasing order. The process of sequentially traversing through all or part of the list is frequently called a “pass”Sathi Durga Devi, Dept of IT
  • 36. Bubble sort 27 32 51 23 13 51 23 13Pass-1 27 23 13 2 7 3 2 3 2 51 27 32 13 51 2 3 27 32 23 51 13 Sathi Durga Devi, Dept of IT
  • 37. Bubble sort Pass-2 23 13 512 7 3 2 27 13 513 2 2 3 27 23 3 2 13 51 27 23 13 5132 Sathi Durga Devi, Dept of IT
  • 38. 27 23 13 5132 Pass-3 13 5132 2 7 2 3 23 5132 2 7 13 23 13 513227 Pass-4 513227 2 3 13 Sorted Array after N-1 passes 13 23 27 32 51 Sathi Durga Devi, Dept of IT
  • 39. /* Write a c program to implement the bubble sort */ #include<stdio.h> void bubbleSort(int a[],int index,int size); void main() { int n,a[10],i; printf("nnEnter the size of array: "); scanf("%d",&n); printf("nnEnter the elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("nnElements before sorting: "); for(i=0;i<n;i++) printf(" %d",a[i]); printf("n"); bubbleSort(a,0,n); } Sathi Durga Devi, Dept of IT
  • 40. void bubblesort(int a[],int index, int n) { int i,x,j,temp; for(i=1;i<=n-1;i++) { printf("n pass- %d n",i); for(j=index;j<n-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; }//if for(x=0;x<n;x++) printf("%3d",a[x]); printf("nn"); }//j }//i printf("n elements after sortingn"); for(i=0;i<n;i++) printf("%3d",a[i]); }//bublesort Time complexity- O(n2 ) Sathi Durga Devi, Dept of IT
  • 41. 41 Selection Sort Procedure:  Selection sort involved scanning through the list to select the smallest element and swap it with the first element.  The rest of the list is then search for the next smallest and swap it with the second element.  This process is repeated until the rest of the list reduces to one element, by which time the list is sorted. The following table shows how selection sort works. Sathi Durga Devi, Dept of IT
  • 43. Write a C program to implement Selection sort #include<stdio.h> void selectionsort(int a[],int n); void main() { int a[100],i,n; printf("n enter the size of the array"); scanf("%d",&n); printf("enter array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); selectionsort(a,n); }//main Sathi Durga Devi, Dept of IT
  • 44. void selectionsort(int a[],int n) { int i,j,k,loc,min,temp; for(i=0;i<n-1;i++) { printf("n"); printf("nstep-%d",i+1); min=a[i]; loc=i; for(j=i+1;j<=n-1;j++) { if(min>a[j]) { min=a[j]; loc=j;} }//j temp=a[i]; a[i]=a[loc]; a[loc]=temp; for(k=0;k<n;k++) printf("%3d",a[k]); }//i printf("nn sorted array is "); for(k=0;k<n;k++) printf("%3d",a[k]); }//selectionsort Sathi Durga Devi, Dept of IT
  • 45. Insertion Sort • Idea: like sorting a hand of playing cards – Start with an empty left hand and the cards facing down on the table. – Remove one card at a time from the table, and insert it into the correct position in the left hand • compare it with each of the cards already in the hand, from right to left – The cards held in the left hand are sorted these cards were originally the top cards of the pile on the table Sathi Durga Devi, Dept of IT
  • 46. Insertion Sort To insert 12, we need to make room for it by moving first 36 and then 24. 6 10 24 12 36 Sathi Durga Devi, Dept of IT
  • 47. Insertion Sort 6 10 24 36 12 Sathi Durga Devi, Dept of IT
  • 48. Insertion Sort 6 10 24 36 12 Sathi Durga Devi, Dept of IT
  • 49. 49 Insertion Sort Using insertion sort an element is inserted in correct location. Insertion sort scans the list from A[0] to A[n-1], insert each element A[k] into its proper position in previously sorted sub list A[0],A[1],A[2]…A[k-1].. Procedure:  A[0] by itself is trivially sorted.  A[1] is inserted either before or after A[0] so that A[0],A[1] is sorted.  A[2] is inserted into its proper place in A[0],A[1], that is, before A[0], between A[0] and A[1], or after A[1] so that A[0],A[1],A[2] is sorted.  Repeatedly compare A[k] to the element just to the left of the vacancy, and as long as A[k] is smaller, move that element into the vacancy, else put A[k] in the vacancy.  Repeat the next element that has not yet examined.  Time complexity- O(n2)  This algorithm is used when n is small. Sathi Durga Devi, Dept of IT
  • 50. 50 Insertion Sort Sathi Durga Devi, Dept of IT
  • 51. Insertion Sort 5 2 4 6 1 3 input array left sub-array right sub-array at each iteration, the array is divided in two sub-arrays: sorted unsorted Sathi Durga Devi, Dept of IT
  • 52. Insertion Sort Sathi Durga Devi, Dept of IT
  • 53. /*Write a program to implement the insertion sort*/ #include<stdio.h> void insertsort(int a[],int n); void main() { int i,a[10],n; printf("n enter array size"); scanf("%d",&n); printf("n enter array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("n elements before swapping"); for(i=0;i<n;i++) printf("%3d",a[i]); insertsort(a,n); printf("n elements after swapping"); for(i=0;i<n;i++) printf("%3d",a[i]); }//main Sathi Durga Devi, Dept of IT
  • 54. void insertsort(int a[],int n) { int k,temp,j,i; //steps for(k=0;k<n;k++) { printf("nstep-%d",k+1); temp=a[k]; j=k-1; while((j>=0)&&(temp<=a[j])) { a[j+1]=a[j]; a[j]=temp; j--; }//while for(i=0;i<n;i++) printf("%3d",a[i]); }//for }//insertsort Sathi Durga Devi, Dept of IT
  • 55. Merge Sort  This algorithm uses the divide- and – conquer method.  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. Sathi Durga Devi, Dept of IT
  • 56. Merge Sort Sathi Durga Devi, Dept of IT
  • 57. mergesort(a,0,7) mergesort(a,0,3) mergesort(a,4,7) mergesort(a,0,1) mergesort(a,2,3) mergesort(a,4,5) mergesort(a,6,7) 8 3 2 9 7 1 5 4 mergsort(a,0,0)ms(a,1,1) ms(a,2,2) ms(a,3,3) ms(a,4,4) ms(a,5,5) ms(a,6,6) ms(a,7,7) 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 merge(a,0,1,0) merge(a,2,3,2) merge(a,4,5,4) merge(a,6,7,6) 3 8 2 9 1 7 4 5 merge(a,0,3,1) merge(a,4,7,5) 2 3 8 9 1 4 5 7 merge(a,0,7,3) 1 2 3 4 5 7 8 9 Sathi Durga Devi, Dept of IT
  • 58. 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 Sathi Durga Devi, Dept of IT
  • 59. #include "stdio.h" void merge(int [],int,int,int); void mergesort(int a[],int low,int high) { int mid; if(low < high) { mid = (low + high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,high,mid); } //if }//mergesort Mergsort program Sathi Durga Devi, Dept of IT
  • 60. void merge(int a[],int l,int h,int m){ int c[100],i,j,k; i = l; j = m + 1; k = l; while (i <= m && j <= h) { if(a[i] < a[j]) { c[k] = a[i]; i++; k++; }//if else { c[k] = a[j]; j++; k++; } //else }//while while(i <= m) c[k++] = a[i++]; while(j <= h) c[k++] = a[j++]; for(i = l; i < k; i++) a[i] = c[i]; }//merge Sathi Durga Devi, Dept of IT
  • 61. void main() { int i,n,a[100]; 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); printf("n Elements in sorted order :n"); for(i = 0; i < n; i++) printf("%5d",a[i]); getch(); } Sathi Durga Devi, Dept of IT
  • 62.  This algorithm uses the divide- and – conquer method.  Select a pivot element from the array of elements.  Rearrange the list so that all the elements before the pivot are smaller than or equal to the pivot and those after the pivot are larger than the pivot .  After such a partitioning, the pivot is placed in its final position.  Sort the two sublists recursively. Quick Sort Sathi Durga Devi, Dept of IT
  • 63. Quicksort AlgorithmGiven an array of n elements (e.g., integers): • If array only contains one element, return • Else – pick one element to use as pivot. – Partition elements into two sub-arrays: • Elements less than or equal to pivot • Elements greater than pivot – Quicksort two sub-arrays – Return results Sathi Durga Devi, Dept of IT
  • 64. Sathi Durga Devi, Dept of IT
  • 65. Recursive implementation with the left most array entry selected as the pivot element. Sathi Durga Devi, Dept of IT
  • 66. /*Write a c program to implement the quick sort*/ #include<stdio.h> void quicksort(int [10],int,int); int main(){ int x[20],size,i; printf("nEnter size of the array: "); scanf("%d",&size); printf("nEnter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("nSorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); getch(); return 0; } Sathi Durga Devi, Dept of IT
  • 67. void quicksort(int x[10],int first,int last){ int pivot,j,temp,i; if(first<last){ pivot=first; i=first; j=last; 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; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } Sathi Durga Devi, Dept of IT
  • 68. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 69. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 70. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 71. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. Whilex[i] <= x[pivot] ++i [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 72. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 73. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 74. 40 20 10 80 60 50 7 30 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 75. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 76. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 77. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 78. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 79. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 80. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 81. 40 20 10 30 60 50 7 80 100pivot_index = 0 i j 1. While x[i] <= x[pivot] ++i 2. While x[j] >x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 82. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 83. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 84. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 85. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 86. 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 87. 1. While data[i] <= data[pivot] ++i 2. While data[j] > data[pivot] --j 3. If i < j swap data[i] and data[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 88. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 89. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 90. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 91. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 5. Swap x[j] and x[pivot_index] 40 20 10 30 7 50 60 80 100pivot_index = 0 i j [0] [1] [2] [3] [4] [5] [6] [7] [8] Sathi Durga Devi, Dept of IT
  • 92. 1. While x[i] <= x[pivot] ++i 2. While x[j] > x[pivot] --j 3. If i < j swap x[i] and x[j] 4. While j > i, go to 1. 5. Swap x[j] and x[pivot_index] 7 20 10 30 40 50 60 80 100pivot_index = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] i j Sathi Durga Devi, Dept of IT
  • 93. Partition Result 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot] Sathi Durga Devi, Dept of IT
  • 94. Recursion: Quicksort Sub-arrays 7 20 10 30 40 50 60 80 100 [0] [1] [2] [3] [4] [5] [6] [7] [8] <= data[pivot] > data[pivot] Sathi Durga Devi, Dept of IT
  • 96. Average Case Worst Case Linear Search - O(n) Binary Search O(log n) O(n) Bubble Sort O(n2 ) O(n2 ) Selection Sort - O(n2 ) Insertion Sort - O(n2 ) Merge Sort O(n log n) O(n log n) Quick Sort O(n log n) O(n2 ) Time Complexities of Searching & Sorting Algorithms:  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. Sathi Durga Devi, Dept of IT
  • 97. • Selection Sort – An algorithm which orders items by repeatedly looking through remaining items to find the least one and moving it to a final location • Bubble Sort – Sort by comparing each adjacent pair of items in a list in turn, swapping the items if necessary, and repeating the pass through the list until no swaps are done • Insertion Sort – Sort by repeatedly taking the next item and inserting it into the final data structure in its proper order with respect to items already inserted. • Merge Sort – An algorithm which splits the items to be sorted into two groups, recursively sorts each group, and merges them into a final, sorted sequence • Quick Sort – An in-place sort algorithm that uses the divide and conquer paradigm. It picks an element from the array (the pivot), partitions the remaining elements into those greater than and less than this pivot, and recursively sorts the partitions. Review of Algorithms Sathi Durga Devi, Dept of IT
  • 98. Assignment - 2 1. Write and explain the breath first traversal and depth first traversal in a graph with algorithm 2. Write notes on spanning tree. Write and explain the algorithms to find minimal cost. 3. Write recursive functions in C for preorder, inorder and post order traversals of a binary tree. 4. Explain with an example how an element is deleted from a binary search tree. 5. Construct the binary search for the following data 32, 12,45,6,78,24,33,9,53,3 6. Write a c program to implement the non recursive method for binary search. 7. Write a c program to implement the quick sort. sort the given elements using quick sort 40,20,10,80,60,50,7,30,100. 8. Explain the procedure for merge sort. Write the time complexities for various searching and sorting techniques. Sathi Durga Devi, Dept of IT