1dsa - Jay

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

IVERSITY CHATTA

it.]
, just drag

N
the page
w here on

IU
ox any
text b

MA
is
ce th
o pla
int. T

T
y po
a ke

I
ze

TH
p hasi
o em
ce t

R
spa
this

u or
use

SK U
e nt
um
oc
th
ed
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]

You might also like