0% found this document useful (0 votes)
4 views

DA3

Uploaded by

X I S I R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DA3

Uploaded by

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

DIGITAL ASSESSMENT-3

SHISIR PANDEY
20BCE2873

WRITE THE C PROGRAM FOR THE FOLLOWING:

1.IMPLEMENTING THE LINEAR SEARCH.


CODE:
#include<stdio.h>

int main()
{
int a[30],i,x,n;
printf("PLEASE ENTER THE NUMBER OF ELEMENTS:-");
scanf("%d",&n);

printf("Enter the elements:-\n");


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

printf("\nEnter element to search:");


scanf("%d",&x);
for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");

return 0;
}
OUTPUT
PLEASE ENTER THE NUMBER OF ELEMENTS:-5
Enter the elements:-
46
55
98
1036
51321

Enter element to search:1036


Element found at index 3
Process returned 0 (0x0) execution time : 12.754 s
Press any key to continue.

2.IMPLEMETING THE BINARY SEARCH.


CODE::
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d elements.\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find:-\n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d\n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.\n", key);
return 0;
}
OUTPUT::
Enter number of elements
5
Enter 5 elements.
12
13
15
19
20
Enter value to find:-
20
20 found at location 5

Process returned 0 (0x0) execution time : 12.043 s


Press any key to continue.

3.IMPLEMENT THE FOLLOWING SEARCHING ALGORITHMS:-


I. BUBBLE SORT:
Program:
#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter the number of elements\n");


scanf("%d", &n);

printf("Enter %d elements:-\n", n);

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


scanf("%d", &array[c]);

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


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

OUTPUT:
Enter the number of elements
5
Enter 5 elements:-
66
84
3
2
69
Sorted list in ascending order:
2
3
66
69
84

Process returned 0 (0x0) execution time : 10.250 s


Press any key to continue.

ii. INSERTION SORT


PROGRAM:-
#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d elements:-\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:-
Enter number of elements
5
Enter 5 elements:-
65
98
213
321
33
Sorted list in ascending order:
33
65
98
213
321

Process returned 0 (0x0) execution time : 10.806 s


Press any key to continue.

iii. SELECTION SORT


PROGRAM:-
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, t;
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d elements:-\n", n);

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


scanf("%d", &array[c]);

for (c = 0; c < (n - 1); c++) // finding minimum element (n-1) times


{
position = c;

for (d = c + 1; d < n; d++)


{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
t = array[c];
array[c] = array[position];
array[position] = t;
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

OUTPUT:-
Enter number of elements
5
Enter 5 elements:-
1321
854512
6534431
210
1
Sorted list in ascending order:
1
210
1321
854512
6534431

Process returned 0 (0x0) execution time : 8.644 s


Press any key to continue.
iv. MERGE SORT.
Program:-
#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[100],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter the elements:\n");

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

mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2) //while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Output:
Enter no of elements:5
Enter the elements:
32
66
09
05
005

Sorted array is :5 5 9 32 66
Process returned 0 (0x0) execution time : 9.201 s
Press any key to continue.

v. 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: \n", 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:-
How many elements are u going to enter?: 5
Enter 5 elements:
21
66
20
19
66666
Order of Sorted elements: 19 20 21 66 66666
Process returned 0 (0x0) execution time : 11.747 s
Press any key to continue.

You might also like