Exercise 1 - Data Structures Lab
Exercise 1 - Data Structures Lab
Source code:
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
reverseArray(arr, size);
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements: 1 2 3 4 5
Reversed Array: 5 4 3 2 1
Result:Program to reverse an array is implemented successfully.
1
MITS B. Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
ii) Aim:To implement a C program to perform Linear search & Binary search.
Linear search
Procedure or Algorithm:
Source code:
#include <stdio.h>
int main() {
int size, target;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Output:
Enter the size of the array: 5
2
MITS B. Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
Enter 5 elements: 2 4 6 8 10
Enter the target element to search: 8
Element found at index 3
Binary search
Procedure or Algorithm:
Source code:
#include <stdio.h>
int main() {
int size, target;
printf("Enter the size of the sorted array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d sorted elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
3
MITS B. Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
Output:
Enter the size of the sorted array: 5
Enter 5 sorted elements: 1 3 5 7 9
Enter the target element to search: 7
Element found at index 3
Result: Linear search and Binary search operations performed successfully on given list of
elements.
iii) Aim: C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort
Bubble sort:
Source code:
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, size);
return 0;
}
4
MITS B. Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
Output:
Enter the size of the array: 5
Enter 5 elements: 64 25 12 22 11
Sorted array: 11 12 22 25 64
Selection sort:
Source code:
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
selectionSort(arr, size);
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements: 29 10 14 37 13
Sorted array: 10 13 14 29 37
Insertion Sort:
5
MITS B. Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
Source code:
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i< size; i++) {
scanf("%d", &arr[i]);
}
insertionSort(arr, size);
return 0;
}
Output:
Enter the size of the array: 5
Enter 5 elements: 5 3 8 6 2
Sorted array: 2 3 5 6 8
Result:Given list of elements are sorted successfully using Bubble sort, Selection sort and Insertion
sort.