DAA Practicle 1
DAA Practicle 1
F E D,MUZA & T
E O ROA FFA EC
EG TH RN HN
LL SA AG
OL
ON
AR
.C
OG
JA
S.D
Y
(2023-2024)
PRACTICLE FILE
Subject-DAA(KCS-553)
int main()
{
int n, value, pos, m = 0, arr[100];
printf("Enter the total elements in the array ");
scanf("%d", &n);
Output :
Enter the total elements int the array 6
Algorithm:
Step1: Start
If [an-1]= key
Return n; go to step 5
Step5: if pos!=0
else
Step6: Exit
Program--02
Objective: Write a program in C of Recursive Binary Search
Program:
#include <stdio.h>
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x) {
return -1;
int main(void)
int x = 10;
int index = binarySearch(arr, 0, size - 1, x);
if (index == -1) {
else {
return 0;
Output:
10 is found at index 3
Algorithm:
Step 1: Binary Search[int [a],int first ,int last,int element]
Return mid+1;
Return binarysearch(a,mid+1,last,element)
Step7: Stop.
Program--03
Objective : Write a program in C to implement Quick Sort
Program:
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
Output:
Sorted Array is
{1,5,6,12,17,25}
Algorithm:
Step1: Start
Step2: down= lb
Step3: up= ub
Step4: pivot= a[lb];
Step5: while(A[down]<=pivot && down,ub) and down++
Step6: while(a[up>pivot && up>lb and up)
Step7: if(down < up) interchange a[down] and a[up] and go to step5
Step8: Interchange a[up] and a[lb]
Step9: return up
Step10: stop
Program--04
Objective : Write a program in C for Selection Sort.
Program:
#include <stdio.h>
void selection(int arr[], int n)
{
int i, j, small;
Algorithm:
SELECTION SORT(arr, n)
Step 1: Repeat Steps 2 and 3 for i = 0 to n-1
Step 2: CALL SMALLEST(arr, i, n, pos)
Step 3: SWAP arr[i] with arr[pos]
[END OF LOOP]
Step 4: EXIT
Output:
Sorted array is :
{8,12,17,25,31,32}
Algorithm:
The simple steps of achieving the insertion sort are listed as follows -
Step 1 : If the element is the first element, assume that it is already sorted.
Return 1.
Step3 : Now, compare the key with all elements in the sorted array.
Step 4 : If the element in the sorted array is smaller than the current
element, then move to the next element. Else, shift greater elements in the
array towards the right.
Program:
#include <stdio.h>
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int LeftArray[n1], RightArray[n2];
for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];
i = 0;
j = 0;
k = beg;
while (i < n1 && j < n2)
{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}
void printArray(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");
printArray(a, n);
return 0;
}
Output:
Algorithm:
1. MERGE_SORT(arr, beg, end)
2. if beg < end
3. set mid = (beg + end)/2
4. MERGE_SORT(arr, beg, mid)
5. MERGE_SORT(arr, mid + 1, end)
6. MERGE (arr, beg, mid, end)
7. end of if
8. END MERGE_SORT
Program--07
Objective: Write A program in C on Heap Sort.
Program:
#include <
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, N, largest);
}
heapify(arr, N, i);
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
printf("\n");
int main()
heapSort(arr, N);
Output:
Sorted array is:
5 6 7 11 12 13
Algorithm:
First convert the array into heap data structure using heapify, then one by
one delete the root node of the Max-heap and replace it with the last node
in the heap and then heapify the root of the heap. Repeat this process until
size of heap is greater than 1.