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
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
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
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
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
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
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
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
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
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
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
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
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
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
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