Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Devanshu File C

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

DATA STRUCTURES USING C

CSIT124

Amity University Uttar Pradesh


Lab Record

Submitted by: Devanshu Shrivastava Submitted to: Dr. Ashok Kumar


Yadav
Enrolment no: A2345921026 (CSIT124)
Class:1CSE Evng.-1X
1.(a) Write a program for Linear Search
Name: Devanshu Shrivastava Enrollment Number: A2345921026
Date of Compilation :
Code:
#include <stdio.h>

int linearsearch (int arr[], int size, int element)

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

if (arr[i]== element)

return i;

return -1;

int main(int argc, char const *argv[])

int arr[]= {1,5,7,9,6};

int size = sizeof(arr)/sizeof (int);

int element;

printf("enter element to find ");

scanf("%d",&element);

int searchIndex = linearsearch(arr,size, element);

printf("element is = %d at index %d \n", element,searchIndex);

return 0;

Output:
1.(b) Write a program for Binary Search
Name: Devanshu Shrivastava Enrollment Number: A2345921026
Date of Compilation :
Code:
#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {

while (low <= high) {

int mid = low + (high - low) / 2;

if (array[mid] == x)

return mid;

if (array[mid] < x)

low = mid + 1;

else

high = mid - 1;

return -1;

int main(void) {

int array[] = {3, 4, 5, 6, 7, 8, 9};

int n = sizeof(array) / sizeof(array[0]);

int x;

printf("enter element to find ");


scanf("%d",&x);

int result = binarySearch(array, x, 0, n - 1);

if (result == -1){

printf("Not found");

else{

printf("Element %d found at index %d", x,result);

return 0;

Output:
2. Write a program for Bubble sort
Name: Devanshu Shrivastava Enrollment Number: A2345921026
Date of Compilation :
Code:
#include<stdio.h>

void printArray (int*A, int n)

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

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

printf("\n");

void bubblesort (int*A , int n){

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

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

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

int temp = A[j];

A[j] = A[j+1];

A[j+1] = temp;

int main()

int A[6] = {4,6,5,8,2,1};


int n= 6;

printArray (A,n);

bubblesort (A,n);

printArray (A,n);

return 0;

Output:
3. Write a program for Selection sort
Name: Devanshu Shrivastava Enrollment Number: A2345921026
Date of Compilation :
Code:
#include <stdio.h>

void selectionsort(int arr[], int n)

int i, j, small;

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

small = i;

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

if (arr[j] < arr[small])

small = j;

int temp = arr[small];

arr[small] = arr[i];

arr[i] = temp;

void printArr(int a[], int n)

int i;

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

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

int main()

int a[] = {1,5,8,7,6,3, };

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");

printArr(a, n);
selectionsort(a, n);

printf("\nAfter sorting array elements are - \n");

printArr(a, n);

return 0;

Output:
4.(a) – Write a program to Implement Queue using Array
Name: Devanshu Shrivastava Enrollment Number: A2345921026
Date of Compilation :

Code:
#include <stdio.h>

#include <stdlib.h>

#define MAX 50

void enqueue();

void dequeue();

void show();

int queue_array[MAX];

int rear = - 1;

int front = - 1;

void main()

int choice;

while (1)

printf("1 for enqueue\n");

printf("2 for dequeue \n");

printf("3 to show \n");

printf("8 to quit \n");

printf("Please enter your choice : ");

scanf("%d", &choice);

switch (choice)

{
case 1:

enqueue();

break;

case 2:

dequeue();

break;

case 3:

show();

break;

case 8:

exit(1);

default:

printf("Wrong choice \n");

void enqueue()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

}
void dequeue()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

void show()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)

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

printf("\n");

}
Output:

You might also like