Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

BHAVESH_KRISHAN_GARG_CSE2B-G1

LAB-04
1. Write a program in C that takes the size of an array and elements of
array from the user and sort the array using Bubble Sort.

SOURCE CODE
#include<stdio.h>

void main()

int arr[10],i,j,n,pass,temp;

printf("Name = BHAVESH KRISHAN GARG\n");

printf("Roll No = 2K20CSUN01019\n");

printf("Enter the size of an array = ");

scanf("%d",&n);

printf("Enter array elements \n");

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

scanf("%d",&arr[i]);

// printf("\nProcedure for bubble sort is : ");//

for(pass=1;pass<=n-1;pass++)

for(j=0;j<=n-1-pass;j++)

if(arr[j]>arr[j+1])

1|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

temp=arr[j];

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

arr[j+1]=temp;

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

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

printf("\n%d",arr[i]);

OUTPUT

2|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

2. Write a program in C that takes the size of an array and elements of


array from the user and sort the array using Selection Sort.

SOURCE CODE
#include <stdio.h>

void main()

int array[100], n, c, d, position, t;

printf("Name = BHAVESH KRISHAN GARG\n");

printf("Roll No = 2K20CSUN01019\n");

printf("Enter number of elements = ");

scanf("%d", &n);

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

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

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

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

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

3|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

array[position] = t;

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

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

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

OUTPUT

3. Write a program in C that takes the size of an array and elements of array from the
user and sort the array using Insertion Sort.

4|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

SOURCE CODE

#include<stdio.h>

void main()

int i, j, n, temp, arr[25];

printf("Name = BHAVESH KRISHAN GARG\n");

printf("Roll No = 2K20CSUN01019\n");

printf("Enter the no of elements = ");

scanf("%d",&n);

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

printf("Enter elements %d : ",i+1);

scanf("%d",&arr[i]);

// Implementation of insertion sort algorithm//

for(i=1;i<n;i++)

temp=arr[i];

j=i-1;

while((temp<arr[j])&&(j>=0))

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

j=j-1;

arr[j+1]=temp;

5|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

printf("Sorted array elements: ");

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

printf(" %d",arr[i]);

OUTPUT

4. Write a program in C that takes the size of an array and elements of array from the user and
search the element given by user using sequential search.

SOURCE CODE
6|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

#include<stdio.h>

int main()

int n,i,arr[10],search;

printf("Name = BHAVESH KRISHAN GARG\n");

printf("Roll No = 2K20CSUN01019\n");

printf("Enter the no elements = ");

scanf("%d",&n);

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

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

printf("Enter the element to be search : ");

scanf("%d",&search);

//sequential search algo//

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

if(arr[i]==search)

printf("%d is present at location %d\n",search,i+1);

break;

}
7|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

} if(i==n)

printf("%d is not present in the array\n",search);

return 0;

OUTPUT

5. Write a program in C that takes the size of an array and elements of array from the user and
search the element given by user using Binary search.

SOURCE CODE
#include<stdio.h>

int main()

8|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

int n,i,arr[10],search,first,last,mid;

printf("Name = BHAVESH KRISHAN GARG\n");

printf("Roll No = 2K20CSUN01019\n");

printf("Enter the no elements = ");

scanf("%d",&n);

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

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

printf("Enter the element to be search : ");

scanf("%d",&search);

//binary search algo//

first=0;

last=n-1;

mid=(first+last)/2;

while(first<=last)

if(arr[mid]<search)

first=mid+1;

else if(arr[mid]==search)

printf("%d found at location %d",search,mid+1);

break;

9|Page
BHAVESH_KRISHAN_GARG_CSE2B-G1

else

last=mid-1;

mid=(first+last)/2;

if(first>last)

printf("%d not found in the list\n",search);

OUTPUT

10 | P a g e

You might also like