0% found this document useful (0 votes)
27 views15 pages

DAA Lab File

The document contains programs implementing various sorting algorithms like linear search, binary search, selection sort, insertion sort, merge sort, quick sort, heap sort, counting sort and radix sort in C language. It includes the code for each algorithm and sample input/output. The programs take input size and elements, perform the respective sorting algorithm and display the sorted output.

Uploaded by

nrathaur844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

DAA Lab File

The document contains programs implementing various sorting algorithms like linear search, binary search, selection sort, insertion sort, merge sort, quick sort, heap sort, counting sort and radix sort in C language. It includes the code for each algorithm and sample input/output. The programs take input size and elements, perform the respective sorting algorithm and display the sorted output.

Uploaded by

nrathaur844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DESIGN AND ANALYSIS OF ALGORITHM

LAB FILE
SUB CODE: KCS553

Submitted to: Submitted by:


Class: 3rdyear ( )
Roll no.:

INDEX
S.no. Practical Date Signature
LINEAR SEARCH PROGRAM
#include <stdio.h>
int main(){
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf ("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++){
if (array[c] == search){ /* If required element is found */
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf ("%d isn't present in the array.\n", search);
return 0;
}
OUTPUT:

BINARY SEARCH PROGRAM

#include <stdio.h>
int main(){
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
OUTPUT:

QUICK SORT PROGRAM

#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);

}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);

quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT:

SELECTION SORT PROGRAM

#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements \n");
scanf("%d", &n);
printf("Enter %d Numbers \n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
OUTPUT:

INSERTION SORT PROGRAM

#include <stdio.h>
int main()
{
int n, array[1000], c, d, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);


for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
t = array[c];
for (d = c - 1 ; d >= 0; d--) {
if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("Sorted list :\n");
for (c = 0; c <= n - 1; c++) {
printf("%d ", array[c]);
}
return 0;
}
OUTPUT:

MERGE SORT PROGRAM


#include <stdio.h>
#include <stdlib.h>

// merge function

void Merge(int arr[], int left, int mid, int right)


{
int i, j, k;
int size1 = mid - left + 1;
int size2 = right - mid;

// created temporary array


int Left[size1], Right[size2];

// copying the data from arr to temporary array

for (i = 0; i < size1; i++)


Left[i] = arr[left + i];

for (j = 0; j < size2; j++)


Right[j] = arr[mid + 1 + j];

// merging of the array

i = 0; // intital index of first subarray


j = 0; // inital index of second subarray
k = left; // initial index of parent array
while (i < size1 && j < size2)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
i++;
}
else
{
arr[k] = Right[j];
j++;
}
k++;
}

// copying the elements from Left[], if any

while (i < size1)


{
arr[k] = Left[i];
i++;
k++;
}

// copying the elements from Right[], if any

while (j < size2)


{
arr[k] = Right[j];
j++;
k++;
}
}

//merge sort function

void Merge_Sort(int arr[], int left, int right)


{
if (left < right)
{

int mid = left + (right - left) / 2;

// recursive calling of merge_sort


Merge_Sort(arr, left, mid);
Merge_Sort(arr, mid + 1, right);

Merge(arr, left, mid, right);


}
}

// driver code
int main()
{
int size;
printf("Enter the size: ");
scanf("%d", &size);

int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}

Merge_Sort(arr, 0, size - 1);

printf("The sorted array is: \n");


for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

OUTPUT:

HEAP SORT PROGRAM

#include<stdio.h>
#include <conio.h>
void Adjust(int Heap_of_Numbers[],int i)
/*Function to arrange the elements in the heap*/
{
int j;
int copy;
int Number;
int Reference = 1;
Number=Heap_of_Numbers[0];
while(2*i<=Number && Reference==1)
{
j=2*i;
if(j+1<=Number && Heap_of_Numbers[j+1] > Heap_of_Numbers[j])
j=j+1;
if( Heap_of_Numbers[j] < Heap_of_Numbers[i])
Reference=0;
else
{
copy=Heap_of_Numbers[i];
Heap_of_Numbers[i]=Heap_of_Numbers[j];
Heap_of_Numbers[j]=copy;
i=j;
}
}
}
void Make_Heap(int heap[])
{
int i;
int Number_of_Elements;
Number_of_Elements=heap[0];
for(i=Number_of_Elements/2;i>=1;i--)
Adjust(heap,i);
}
int main()
{
int heap[30];
int NumberofElements;
int i;
int LastElement;
int CopyVariable;
printf("Enter the number of elements present in the unsorted
Array:");
scanf("%d",&NumberofElements);
printf("nEnter the members of the array one by one:");
/* Asking for the elements of the unsorted array*/
for(i=1;i<=NumberofElements;i++)
scanf("%d",&heap[i]);
heap[0]=NumberofElements;
Make_Heap(heap);
while(heap[0] > 1)
/*Loop for the Sorting process*/
{
LastElement=heap[0];
CopyVariable=heap[1];
heap[1]=heap[LastElement];
heap[LastElement]=CopyVariable;
heap[0]--;
Adjust(heap,1);
}
printf("nSorted Array:n");/*Printing the sorted Array*/
for(i=1;i<=NumberofElements;i++)
printf("%d ",heap[i]);
return 0;
}
OUTPUT:

COUNTING SORT PROGRAM


#include <stdio.h>
void counting_sort(int A[], int k, int n){
int i, j;
int B[15], C[100];
for (i = 0; i <= k; i++)
C[i] = 0;
for (j = 1; j <= n; j++)
C[A[j]] = C[A[j]] + 1;
for (i = 1; i <= k; i++)
C[i] = C[i] + C[i-1];
for (j = n; j >= 1; j--) {
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
printf("The Sorted array is : ");
for (i = 1; i <= n; i++)
printf("%d ", B[i]);
}
int main(){
int n, k = 0, A[15], i;
printf("Enter the number of input : ");
scanf("%d", &n);
printf("\nEnter the elements to be sorted :\n");
for (i = 1; i <= n; i++){
scanf("%d", &A[i]);
if (A[i] > k) {
k = A[i];
}
}
counting_sort(A, k, n);
printf("\n");
return 0;
}
OUTPUT:

RADIX SORT PROGRAM


#include <stdio.h>

int print(int *a, int n) {


int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}

void radix_sort(int *a, int n) {


int i, b[10], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}

while (m / exp > 0) {


int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
}
}

int main() {
int arr[10];
int i, num;

printf("Input number of elements: ");


scanf("%d", &num);

printf("\nInput array elements one by one : ");


for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

printf("\nArray elements : ");


print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\nSorted elements : ");
print(&arr[0], num);

return 0;
}

OUTPUT:

You might also like