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

Assignment 3

The document discusses 7 programs related to searching and sorting techniques in data structures. It provides the code and output for programs that use linear search, binary search, bubble sort, selection sort, insertion sort, quick sort and merge sort algorithms.

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assignment 3

The document discusses 7 programs related to searching and sorting techniques in data structures. It provides the code and output for programs that use linear search, binary search, bubble sort, selection sort, insertion sort, quick sort and merge sort algorithms.

Uploaded by

Gourav Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Stream: BCA Year: 2nd Semester:3rd

Subject: Data Structure & Algorithm Lab


Code: BCAC393

Name
University Roll
Number
Data Structure & Algorithm Lab (BCAC393)
2021
ROLL
NAM
E

Learning outcome: Students will be able to perform different searching and Sorting
techniques.

ASSIGNMENT: III

Sl. Program Listing


No
1 Write a function to search an key element in an array using linear searching
technique.
2 Write a function to search an key element in an array using binary searching
technique
3 Write a function to sort a list using Refined Bubble sorting technique.
4 Write a function to sort a list using Selection sorting technique.
5 Write a function to sort a list using Insertion sorting technique.
6 Write a function to sort a list using Quick sorting technique.
7 Write a function to sort a list using Merge sorting technique.
Program – 1
Write a function to search an key element in an array using linear
searching technique.

Code-
#include<stdio.h>
int linearsearching(int arr[], int n, int key)
{
int i, loc;
arr[n]=key;
for(i=0;i<n;i++)
{
if(arr[i]==key)
break;
}
if(i<n)
return i;
else
return -1;

}
int main()
{
int i, n, key, arr[20];
printf("enter the number of element:");
scanf("%d",&n);
printf("enter the element of the array:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the key to short:");
scanf("%d",&key);
i=linearsearching(arr,n,key);
if(i<=0)
printf("number is not present array:");
else
printf("the number in %d in the array:",i);
}
Output
Program-2
Write a function to search an key element in an array using binary searching
technique

Code-
#include<stdio.h>
int binarysearch(int arr[],int n, int key)
{
int i, j, mid;
i=0;j=n-1;
while(i<=j)
{
mid=(i+j)/2;
if(key==arr[mid])
return mid;
if(key<arr[mid])
j=mid-1;
else
i=mid+1;
}
return-1;
}
int main()
{
int i, n, key, arr[20];
printf("enter the number of element:");
scanf("%d",&n);
printf("enter the element of the array:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the key to short:");
scanf("%d",&key);
i=binarysearch(arr,n,key);
if(i<=0)
printf("number is not present array:");
else
printf("the number in %d in the array:",i);
}

Output-
Program-3
Write a function to sort a list using Refined Bubble sorting technique.

Code-
#include<stdio.h>
void bobblesort(int arr[], int n)
{
int i=1,j,flag,t;
while(1)
{
flag = 0;
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
t=arr[j];arr[j]=arr[j+1];arr[j+1]=t;
flag=1;
}
}
if(!flag)return;
i++;
}
}
int main()
{
int i, n, arr[20];
printf("enter the number of element:");
scanf("%d",&n);
printf("enter the element of the array:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bobblesort(arr,n);
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
}
Output-
Program-4
Write a function to sort a list using Selection sorting technique.

Code-
#include<stdio.h>
int main()
{
int i,j,n,temp,arr[25];
printf("enter the number of element: ");
scanf("%d",&n);
printf("enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("sorted element: ");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
return 0;
}
Output
Program-5
Write a function to sort a list using Insertion sorting technique.
Code-

#include<stdio.h>
int main()
{
int i,j,n,temp,arr[25];
printf("enter the number of element: ");
scanf("%d",&n);
printf("enter %d elements: ", n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
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;
}
printf("order of sorted element: ");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
return 0;
}
Output-
Program-6
Write a function to sort a list using Quick sorting technique.

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

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

while(i<j)
{
while(arr[i]<=arr[pivot]&&i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
quicksort(arr,first,j-1);
quicksort(arr,j+1,last);
}
}

int main()
{
int i, count, arr[25];

printf("enter no of element ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


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

quicksort(arr,0,count-1);

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",arr[i]);

return 0;
}
Output-
Program-7
Write a function to sort a list using Merge sorting technique.
Code-
#include <stdio.h>
void merge_sort(int i, int j, int a[], int aux[])
{
if (j <= i)
{
return;
}
int mid = (i + j) / 2;
merge_sort(i, mid, a, aux);
merge_sort(mid + 1, j, a, aux);

int pointer_left = i;
int pointer_right = mid + 1;
int k;

for (k = i; k <= j; k++)


{
if (pointer_left == mid + 1)
{
aux[k] = a[pointer_right];
pointer_right++;
}
else if (pointer_right == j + 1)
{
aux[k] = a[pointer_left];
pointer_left++;
}
else if (a[pointer_left] < a[pointer_right])
{
aux[k] = a[pointer_left];
pointer_left++;
} else {
aux[k] = a[pointer_right];
pointer_right++;
}
}

for (k = i; k <= j; k++) {


a[k] = aux[k];
}
}

int main() {
int a[100], aux[100], n, i, d, swap;
printf("Enter number of elements in the array:\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
merge_sort(0, n - 1, a, aux);
printf("Printing the sorted array:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}
Output-

---------------------------------------x-----------------------------------------

You might also like