MITS B.
Tech I Year II Semester CSE-AI23CSE203 - DATA STRUCTURES LABORATORY
Exercise 1: Array Manipulation
i) Aim:To Implement a C-Program to reverse an array.
Procedure or Algorithm:
Source code:
#include <stdio.h>
void reverseArray(int arr[], int size) {
int start = 0, end = size - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
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);
printf("Reversed Array: ");
for (int i = 0; i< size; i++) {
printf("%d ", arr[i]);
}
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 linearSearch(int arr[], int size, int target) {
for (int i = 0; i< size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
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]);
}
printf("Enter the target element to search: ");
scanf("%d", &target);
int result = linearSearch(arr, size, target);
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 binarySearch(int arr[], int size, int target) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
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]);
}
printf("Enter the target element to search: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
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>
void bubbleSort(int arr[], int size) {
for (int i = 0; i< size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] >arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
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);
printf("Sorted array: ");
for (int i = 0; i< size; i++) {
printf("%d ", arr[i]);
}
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>
void selectionSort(int arr[], int size) {
for (int i = 0; i< size - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] <arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
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);
printf("Sorted array: ");
for (int i = 0; i< size; i++) {
printf("%d ", arr[i]);
}
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>
void insertionSort(int arr[], int size) {
for (int i = 1; i< size; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 &&arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
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);
printf("Sorted array: ");
for (int i = 0; i< size; i++) {
printf("%d ", arr[i]);
}
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.