Data Structure File

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

DATA STRUCTURE LAB

FILE
Name : Harsh
Enrollment no. : A50105221095
Course : B.Tech(CSE)
Batch : 2021 – 2025
Submitted to : Dr.Deepthi Sehrwat
INDEX
S.No Topic Practical Date Signature
1. Traversal
2. Insertion
3. Deletion
4. Minimum
Array
5. Maximum
6. Searching
a) Linear Search
7. Searching
b) Binary Search
8. Prime Number
9. Bubble Sort
10. Insertion Sort
11. Selection Sort
Sorting
12. Algorithms Merge Sort
13. Quick Sort
14. Heap Sort
15.
Dislay
POP (Insertion)
Stack POP (Deletion)
Infix to Postfix
Dislay
Enqueue (Insertion)
Linear
Queue Dequeue (Deletion)
EXPERIMENT-1

Traversal in an Array :-

Theory :- Traversing an array means accessing each and every element of the array for a
specific purpose.

Traversing the data elements of an array A can include printing every element, counting the
total number of elements, or performing any process on these elements. Since, array is a
linear data structure (because all its elements form a sequence), traversing its elements is very
simple and straightforward.

Code :-
// WAP on traverse the array

#include <stdio.h>
 void printArray(int* arr, int n)
{
    int i;

    printf("Array: ");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main()
{
    int arr[] = { 2, -1, 5, 6, 0, -3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    printArray(arr, n);

    return 0;
}

Output :-
Best Case :-
o(n)
We are printing whole array so time complexity will be same.

Worst Case :-
o(n)
We are printing whole array so time complexity will be same.

EXPERIMENT-2
Insertion in an Array :-

Theory :- Insert operation is to insert one or more data elements into an array.
Based on the requirement, new element can be added at the beginning, end or
any given index of array.

Code :-
#include<stdio.h>
int main(){
   int student[40],pos,i,size,value;
   printf("enter no of elements in array of students:");
   scanf("%d",&size);
   printf("enter %d elements are:\n",size);
   for(i=0;i<size;i++)
      scanf("%d",&student[i]);
   printf("enter the position where you want to insert the element:");
   scanf("%d",&pos);
   printf("enter the value into that poition:");
   scanf("%d",&value);
   for(i=size-1;i>=pos-1;i--)
      student[i+1]=student[i];
   student[pos-1]= value;
   printf("final array after inserting the value is\n");
   for(i=0;i<=size;i++)
      printf("%d\n",student[i]);
   return 0;
}

Output :-
Best Case :-
o(1) Best case will occur when element will be inserted at the last.

Worst Case :-
o(n) Worst case will occur when the element will be inserted at the
zeroth index.
EXPERIMENT-3

Deletion in an Array :-

Theory:- This program deletes or removes an element from an array. A user will enter the
position at which the array element deletion is required. Deleting an element does not affect
the size of the array. It also checks whether deletion is possible or not.

Code:-
#include <stdio.h>
int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

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

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


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

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if (position >= n+1)


printf("Deletion not possible.\n");
else
{
for (c = position - 1; c < n - 1; c++)
array[c] = array[c+1];

printf("Resultant array:\n");

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


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

return 0;
}

Output :-
Best Case :-
o(1) Best case will occur when element will be deleted from the last
index.

Worst Case :-
o(n) Worst case will occur when the element will be deleted from the
zeroth
EXPERIMENT-4

Finding Minimum element of an Array:-


Theory:- Whole array will be traversed to find the minimum
element of the array.

Code:-
#include <stdio.h>

int main()
{
int array[100], minimum, size, c, location = 1;

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


scanf("%d",&size);

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

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


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

minimum = array[0];

for ( c = 1 ; c < size ; c++ )


{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location number %d and
it's value is %d.\n", location, minimum);
return 0;
}
Output:-

We never know that that the element encountered is minimum


before checking the whole array. So, whole array will be visited and
time complexity will be same irrespective of worst case and best
case.

Best Case:-
o(n)

Worst Case:-
o(n)
EXPERIMENT-5

Finding Maximum element of an Array:-


Theory:- Whole array will be traversed to find the maximum
element of the array.

Code:-

#include <stdio.h>
int main() {
int n;
int arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);

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


printf("Enter the random number : %d: ", i + 1);
scanf("%d", &arr[i]);
}

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


if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}

printf("Largest element : %d", arr[0]);

return 0;
}
Output:-

We never know that that the element encountered is maximum


before checking the whole array. So, whole array will be visited and
time complexity will be same irrespective of worst case and best
case.

Best Case:-
o(n)

Worst Case:-
o(n)

You might also like