0% found this document useful (0 votes)
33 views24 pages

1dsa - Jaykishor

The document details 4 experiments performed in a data structures and algorithms lab, where linear search, binary search, bubble sort, and merge sort algorithms are implemented to search and sort arrays. Code snippets are provided for each algorithm implementation along with sample outputs. The experiments are submitted by a student for their data structures course.

Uploaded by

Jaykishor Singh
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)
33 views24 pages

1dsa - Jaykishor

The document details 4 experiments performed in a data structures and algorithms lab, where linear search, binary search, bubble sort, and merge sort algorithms are implemented to search and sort arrays. Code snippets are provided for each algorithm implementation along with sample outputs. The experiments are submitted by a student for their data structures course.

Uploaded by

Jaykishor Singh
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/ 24

IVERSITY CHATTA

it.]
, just drag

N
the page
ere on
anywh

IU
xt box

MA
his te
ace t
To pl
oint.

T
yp
a ke

I
size

TH
pha
o em
ce t

R
spa
this

u or
use

K U
e nt
um
oc
ed

S
th
m
fro
te
uo
tq
ea
r
ag
SAN it h
nw

RA
tio
en
att
er ’s
read
[Grab your

SESSION:2023-2024

SUBJECT:DATA STRUCTURE AND


ALGORITHM LAB

SUBMITTED BY

NAME: JAYKISHOR SINGH


ADMISSION NO: 2200864
ENROLLMENT NO:2202305039
COURSE: B.TECH [CSE]
SESSION:2023-2024
SUBJECT:DATA STRUCTURE AND ALGORITHM LAB

SUBMITTED TO: MR RAHUL SIR


Sanskriti University Sub-Data – Structure

EXPERIMENT:1

•Aim -- Write a program to create a array and search a number by the help
of linear search

#include<stdio.h>

int main(){

//Linear search

int number, store, arr[50] , n, i;

printf("Enter the size of array : ");

scanf("%d", &number);

for (int n = 0; n < number; n++)

printf("Enter the elment of the array : ");

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

printf("Enter the element you want to search : ");

scanf("%d", &store);

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

if (arr[i] == store) /* If required element is found */

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

break;

} }

if (i == number) printf("%d isn't present in the array.\n", store);

return 0; }

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:2
• Aim - Write a program to create a array and search a number by the help of
binary search
#include <stdio.h>

// Function to perform binary search

int binarySearch(int arr[], int size, int search) {

int i = 0;

int j = size - 1;

while (i <= j) {

int mid = i + (j - i) / 2;

// If the search is found, return its index

if (arr[mid] == search) {

return mid;

// If the search is greater, ignore the left half

if (arr[mid] < search) {

i = mid + 1;

// If the search is smaller, ignore the right half

else {

j = mid - 1;

// If the search is not found, return -1

return 0; }

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

int main() {

int size, search;

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

printf("Enter the elements of the sorted array:\n");

for (int i = 0; i < size; i++) {

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

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

scanf("%d", &search);

int result = binarySearch(arr, size, search);

if (result != -1) {

printf("Element %d found at index %d.\n", search, result);

} else {

printf("Element %d not found in the array.\n", search);

}
return 0;}

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:3
• Aim -- Write a program to sort the unsorted array by the help of bubble sort

#include <stdio.h>

int main() {

int n;

// Get the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declare an array of n elements

int arr[n];

// Input the elements of the array

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

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

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

// Bubble Sort Algorithm

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

// Swap if the element found is greater

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


int temp = arr[j];

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

arr[j + 1] = temp;

} } }

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

// Display the sorted array

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

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

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

return 0; }

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:4
• Aim - Write a program to sort the unsorted array by the help of merge sort

#include <stdio.h>

// Function to merge two sorted subarrays into a single sorted subarray

void merge(int arr[], int left[], int leftSize, int right[], int rightSize) {

int i = 0, j = 0, k = 0;

while (i < leftSize && j < rightSize) {

if (left[i] <= right[j]) {

arr[k] = left[i];

i++;

} else {

arr[k] = right[j];

j++;

k++;

}
// Copy the remaining elements of left[] and right[], if any

while (i < leftSize) {

arr[k] = left[i];

i++;

k++;

}
while (j < rightSize) {

arr[k] = right[j];

j++;

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

k++;

// Function to perform Merge Sort on an array

void mergeSort(int arr[], int size) {

if (size < 2) {

return; // Base case: If the array has 0 or 1 elements, it is already sorted.

int mid = size / 2;

int left[mid];

int right[size - mid];

// Divide the array into two subarrays

for (int i = 0; i < mid; i++) {

left[i] = arr[i];

for (int i = mid; i < size; i++) {

right[i - mid] = arr[i];

// Recursively sort the two subarrays

mergeSort(left, mid);

mergeSort(right, size - mid);

// Merge the sorted subarrays

merge(arr, left, mid, right, size - mid);

}
Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]
Sanskriti University Sub-Data – Structure

int main() {

int n;

// Get the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declare an array of n elements

int arr[n];

// Input the elements of the array

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

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

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

// Perform Merge Sort

mergeSort(arr, n);

// Display the sorted array

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

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

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

return 0; }

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:5
• Aim -- Write a program to sort the unsorted array by the help of insertion sort

#include <stdio.h>

// Function to perform Insertion Sort on an array

void insertionSort(int arr[], int size) {

int i, j, key;

for (i = 1; i < size; i++) {

key = arr[i];

j = i - 1;

// Move elements of arr[0..i-1] that are greater than key

// to one position ahead of their current position

while (j >= 0 && arr[j] > key) {

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

j--;

arr[j + 1] = key;

int main() {

int n;

// Get the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declare an array of n elements

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

int arr[n];

// Input the elements of the array

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

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

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

// Perform Insertion Sort

insertionSort(arr, n);

// Display the sorted array

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

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

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

return 0; }

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:6
• Aim -- Write a program to sort the unsorted array by the help of selection sort

#include <stdio.h>

// Function to perform Selection Sort on an array

void selectionSort(int arr[], int size) {

int i, j, minIndex, temp;

for (i = 0; i < size - 1; i++) {

// Find the minimum element in the unsorted portion of the array

minIndex = i;

for (j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

} }

// Swap the found minimum element with the first element

temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

} }

int main() {

int n;

// Get the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declare an array of n elements

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

int arr[n];

// Input the elements of the array

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

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

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

// Perform Selection Sort

selectionSort(arr, n);

// Display the sorted array

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

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

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

return 0; }

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:7
• Aim -- Write a program to sort the unsorted array by the help of quick sort

#include <stdio.h>

// Function to partition the array into two subarrays and return the pivot index

int partition(int arr[], int low, int high) {

int pivot = arr[high]; // Choose the rightmost element as the pivot

int i = low - 1; // Initialize the index of the smaller element

for (int j = low; j < high; j++) {

// If the current element is smaller than or equal to the pivot

if (arr[j] <= pivot) {

i++; // Increment the index of the smaller element

// Swap arr[i] and arr[j]

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

// Swap arr[i + 1] and arr[high] (pivot)

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return i + 1; }

// Function to perform Quick Sort on an array

void quickSort(int arr[], int low, int high) {

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

if (low < high) {

// Find the pivot element such that

// element smaller than pivot are on the left

// and elements greater than pivot are on the right

int pi = partition(arr, low, high);

// Recursively sort elements before and after the pivot

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

} }

int main() {

int n;

// Get the number of elements in the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declare an array of n elements

int arr[n];

// Input the elements of the array

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

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

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

// Perform Quick Sort

quickSort(arr, 0, n - 1);

// Display the sorted array

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

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

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

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

return 0; }

OUTPUT:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


EXPERIMENT:8

• Aim -- Write a program for performing the push and pop operation of the stack .

#include <stdio.h>

#define MAX_SIZE 5

int arr[MAX_SIZE], top = -1, i;

// Insert the elements


void push(int x)
{
if (top == (MAX_SIZE - 1))
{
printf("Stack is Overflow\n");
}
else
{
arr[++top] = x; // first increament
}
}
// deleted the elements
void pop()
{
if (top < 0)
{
printf("Stack is Underflow\n");
}
else
{
top--; // post decreament
}
}
// it is used for the show the elements
void disp()
{
printf("\nElements are\n");
for (i = 0; i <= top; i++)
{
printf("%d\n", arr[i]);
}
}
Sanskriti University Sub-Data – Structure

int main()
{
int choice, num; // declared the variable

while (1)
{

printf("\n1. PUSH\n");
printf("2. POP\n");
printf("3. DISPLAY\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);

// here we are calling


switch (choice)
{
case 1:
printf("Enter the element to push: ");
scanf("%d", &num);
push(num);
break; // terminate the program
case 2:
pop();
break;
case 3:
disp();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}

return 0;
}

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

Output:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

EXPERIMENT:9

• Aim -- Write a program for performing the enqueue and dequeue operation of the
queue.

#include <stdio.h>

#define maxsize 5

int queue[maxsize];
int rear = -1, front = 0;

// Function prototypes
void insert(int n);
void display();
void delete();

int main() {
int choice,n;

printf("\n------------ QUEUE MENU --------------\n");


printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");

do {
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:

printf("Enter an element to insert: ");


scanf("%d",&n);
insert(n);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);

return 0;
}

void insert(int n) {
if (rear == maxsize - 1) {
printf("\nQUEUE is full.\n");
} else {
rear++;
queue[rear] = n;
}
}

void display() {
if (front > rear) {
printf("\nQueue is empty.\n");
} else {
printf("\nQueue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}

void delete() {
if (front > rear) {
printf("\nQueue is empty.\n");
} else {
int n = queue[front];
front++;
printf("The deleted element is %d.\n", n);
}
}

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]


Sanskriti University Sub-Data – Structure

Output:

Name – Jaykishor singh Enrollment No. – 2202305039 Course – BTECH(CSE) [A]

You might also like