Sublect Code: Kcs553: Design and Analysis of Algorithm Lab
Sublect Code: Kcs553: Design and Analysis of Algorithm Lab
Sublect Code: Kcs553: Design and Analysis of Algorithm Lab
VISION:
To provide a congenial environment for learning in
discipline of Computer Science and Engineering, and to
mobilize students towards serving a globalized
technological society.
MISSION:
• To ensure that every engineering student is proficient
with necessary computer skills.
• To familiarize students with latest developments in
Computer Science and Information Technology and
motivate them to embrace all challenges and be future
ready.
• To inculcate strong ethical values, research capabilities,
professional behaviour and leadership abilities in your
character so as to work with a commitment to the
progress of the nation.
ESHA GUPTA
INDEX
SR PAGE
TOPIC DATE SIGNATURE
NO. NO
ESHA GUPTA
PRACTICAL 1
EXAMPLE:
i = 2. 14 will remain at same location as all elements in A[0..I-1] are smaller than 14
[12, 13, 14, 5, 6]
i = 3. 5 will move at beginning and other elements from 12 to 14 will move one position to
front from their current location.
[5, 12, 13, 14, 6]
i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position to
front of their current position.
PROGRAM –
#include <stdio.h>
int main()
int n, i, j, temp;
int arr[64] {
ESHA GUPTA
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;}
OUTPUT:
COMPLEXITY-
ESHA GUPTA
PRACTICAL 2
AIM- WRITE A PROGRAM FOR MERGE SORT.
THEORY –
MERGE SORT is a sorting algorithm that follows divide and conquer approach.
ALGORITHM-
ESHA GUPTA
EXAMPLE-
ESHA GUPTA
PROGRAM –
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
int main() {
int i;
ESHA GUPTA
for(i = 0; i<= max; i++)
printf("%d ", a[i]);
sort(0, max);
OUTPUT-
COMPLEXITY-
ESHA GUPTA
PRACTICAL 3
AIM- WRITE A PROGRAM FOR QUICK SORT.
THEORY –
QUICKSORT is a sorting algorithm that uses divide-and-conquer algorithm. It works by
selecting a "key" element or can say "pivot" element from the array and partitioning the other
elements into two sub-arrays, according to whether they are less than or greater than the
pivot. The sub-arrays are then sorted recursively
ALGORITHM-
PARTITION (ARR, BEG, END, LOC)
ESHA GUPTA
EXAMPLE-
i=0
A[] = {20, 90, 40, 100, 50, 60, 80} #No change as i and j are same
i=1
A[] = {20, 40, 90, 100, 50, 60, 80} #We swap 80 and 30
i=2
A[] = {20, 40, 50, 100, 90, 60, 80} #90 and 50 Swapped
j = 5 : Since A[j] <= pivot, do i++ and swap A[i] with A[j]
i=3
A[] = {20, 40, 50, 60, 90, 100, 80} #100 and 60 Swapped
A[] = {20, 40, 50, 60, 80, 100, 90} #90 and 80 Swapped
it.
ESHA GUPTA
PROGRAM –
#include <stdio.h>
void main()
clrscr();
int i;
int arr[10]={90,23,101,45,65,23,67,89,34,23};
quickSort(arr, 0, 9);
for(i=0;i<10;i++)
getch();
right = end;
flag = 0;
while(flag != 1)
right--;
if(loc==right)
flag =1;
ESHA GUPTA
else if(a[loc]>a[right])
temp = a[loc];
a[loc] = a[right];
a[right] = temp
loc = right;
if(flag!=1)
left++;
if(loc==left)
flag =1;
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
return loc;
int loc;
ESHA GUPTA
if(beg<end)
OUTPUT-
COMPLEXITY-
ESHA GUPTA
PRACTICAL 4
AIM- WRITE A PROGRAM FOR HEAP SORT.
THEORY –
HEAP SORT processes the elements by creating the min heap or max heap using the
elements of the given array. Min heap or max heap represents the ordering of the array in
which root element represents the minimum or maximum element of the array. At each step,
the root element of the heap gets deleted and stored into the sorted array and the heap will
again be heapified.
ALGORITHM-
MAX-HEAPIFY (A, i)
1. l ← left [i]
2. r ← right [i]
3. if l≤ heap-size [A] and A[l] > A [i]
4. then largest ← l
5. Else largest ← i
6. If r≤ heap-size [A] and A [r] > A[largest]
7. Then largest ← r
8. If largest ≠ i
9. Then exchange A [i] A [largest]
10. MAX-HEAPIFY (A, largest)
HEAP-SORT (A)
1. BUILD-MAX-HEAP (A)
2. For I ← length[A] down to Z
3. Do exchange A [1] ←→ A [i]
4. Heap-size [A] ← heap-size [A]-1
5. MAX-HEAPIFY (A,1)
ESHA GUPTA
EXAMPLE:
A: 40, 100, 30, 50, 10
40(0)
/ \
100(1) 30(2)
/ \
5(30) 10(4)
40(0)
/ \
100(1) 30(2)
/ \
50(3) 10(4)
100(0)
/ \
50(1) 30(2)
/ \
40(3) 10(4)
PROGRAM –
#include<stdio.h>
#include<conio.h>
int temp;
ESHA GUPTA
void heapify(int arr[], int size, int i)
int largest = i;
largest = left;
largest = right;
if (largest != i)
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
int i;
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
ESHA GUPTA
heapify(arr, i, 0); } }
void main()
{ clrscr();
int i;
printf(" %d ",arr[i]);
int i;
heapSort(arr, size);
printf(" %d",arr[i]);
getch(); }
OUTPUT-
COMPLEXITY-
ESHA GUPTA
PRACTICAL 5
AIM- WRITE A PROGRAM FOR COUNTING SORT.
THEORY –
COUNTING SORT is a linear time sorting algorithm which works faster by not making a
comparison. It assumes that the number to be sorted is in range 1 to k where k is small.Basic
idea is to determine the "rank" of each number in the final sorted array.Counting Sort uses
three arrays:
1. A [1, n] holds initial input.
2. B [1, n] holds sorted output.
3. C [1, k] is the array of integer. C [x] is the rank of x in A where x ∈ [1, k]
o Initialize C to zero
o For each j from 1 to n increment C [A[j]] by 1
We set B[C [x]] =A[j]. If there are duplicates, we decrement C [i] after copying.
ALGORITHM-
ESHA GUPTA
EXAMPLE:
Let us trace the above algorithm using an example:Consider the following input array
A to be sorted. All the elements are in range 0 to 9
A[]= {1, 3, 2, 8, 5, 1, 5, 1, 2, 7}
Copy
Step 1: Initialize an auxiliary array, say count and store the frequency of every
distinct element. Size of count is 10 (k+1, such that range of elements in A is 0 to k)
Step 3: Add elements of array A to resultant array B using the following steps:
• For, i=0, t=1, count[1]=3, v=2. After adding 1 to B[2], count[1]=2 and i=1
• For i=1, t=3, count[3]=6, v=5. After adding 3 to B[5], count[3]=5 and i=2
• For i=2, t=2, count[2]= 5, v=4. After adding 2 to B[4], count[2]=4 and i=3
ESHA GUPTA
• For i=3, t=8, count[8]= 10, v=9. After adding 8 to B[9], count[8]=9 and i=4
ESHA GUPTA
Thus, array B has the sorted list of elements.
PROGRAM –
OUTPUT-
COMPLEXITY-
ESHA GUPTA