0% found this document useful (0 votes)
5 views

DS Lab Exercise 1

The document outlines a series of lab exercises focused on data structures and algorithms, specifically array manipulation, searching techniques (linear and binary search), and sorting techniques (bubble sort, selection sort, and insertion sort). Each exercise includes a C program implementation with detailed code examples and expected outputs. The document serves as a practical guide for students to understand and apply these fundamental concepts in programming.

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DS Lab Exercise 1

The document outlines a series of lab exercises focused on data structures and algorithms, specifically array manipulation, searching techniques (linear and binary search), and sorting techniques (bubble sort, selection sort, and insertion sort). Each exercise includes a C program implementation with detailed code examples and expected outputs. The document serves as a practical guide for students to understand and apply these fundamental concepts in programming.

Uploaded by

howtoaddme
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DSA – Lab Exercises

Exercise 1: Array Manipulation


1. Write a program to reverse an Array
2. C programs to implement the Searching Techniques –
a. Linear Search
b. Binary Search
3. C programs to implement Sorting Techniques –
a. Bubble Sort
b. Selection Sort
c. Insertion Sort
----------------------------------------------------------------------------------------

Program 1: Write a C program to reverse and print the given array.


Solution:
#include <stdio.h>
int main()
{
// Variable declarations
int a[50]; // Array that can hold a maximum of 50 numbers
int n; // Current number of values
int i; // Maintains the index position

// How many values we are planning to store


scanf("%d", &n);

// Enter array elements one by one


for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}

// Printing the Original Array


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

// Printing the Array in the Reverse Order


printf("\nReversed Array: ");
for(i=n-1; i>=0; i--)
{
printf("%d ", a[i]);
}
return 0;
}
----------------------------------------------------------------------------------------

Searching Techniques
Program 2: Implementing Linear Search

#include<stdio.h>
int main()
{
int a[100];
int n,i,x;

// Getting the number of elements


printf("Enter the Number of Elements you want to store: ");
scanf("%d",&n);

// Loop to read N values and store it into the array


for(i=0;i<n;i++)
{
printf("\nEnter the Element: ");
scanf("%d",&a[i]);
}

// Getting the value of the element to search for within the array
printf("\nEnter the Number to search: ");
scanf("%d",&x);

// Loop to compare the array element with the search value


for(i=0;i<n;i++)
{ Input:
if(a[i] == x) How many elements?8
{ Enter 8 array elements:
break; 14 16 10 5 78 92 63 48
} Enter element to search: 16
} Output:
Element 16 is found at index 3
// Checking whether the match is found
// If found then printing the Index position at which it was found
if(i<n)
{
printf("\nElement %d is Found at Index %d",x,i);
}
else
{
printf("\nElement %d is NOT Found",x);
}
return 0;
}
------------------------------------------------------------------------------

Program 3: Implementing Binary Search


#include <stdio.h>

void Sort(int arr[] , int n)


{
int i,j,temp;
for (i=0; i<n; ++i)
{
for (j=i+1; j<n; ++j)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

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


// Repeat until the pointers low and high meet each other
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 a[20],n,x,i;

printf("How many elements?");


scanf("%d",&n);

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


for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
}

printf("\nEnter element to search: ");


scanf("%d",&x);

Sort(a,n);
printf("\nSorted Array - ");
for(i=0;i<n;++i)
{
printf("%d ",a[i]);
}

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


if (result == -1)
printf("\nNot found");
else
printf("\nElement is found at index %d", result);

return 0;
}

Output:
How many elements?8
Enter 8 array elements:
14
16
10
5
78
92
63
48
Enter element to search: 16
Sorted Array - 5 10 14 16 48 63 78 92
Element is found at index 3
------------------------------------------------------------------------------

Sorting Techniques
Program 4: Implementing Bubble Sort

#include <stdio.h>
// Function to perform the Bubble Sort Technique
void bubbleSort(int arr[], int n)
{
int i, j, temp;
int swapped;
// Outer loop for each pass
for (i = 0; i < n - 1; i++)
{
swapped = 0; // Track if any swaps are made

// Inner loop for comparing adjacent elements


for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// Swap elements if they are in the wrong order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

swapped = 1; // Mark that a swap occurred


}
}

// If no swaps were made, the list is already sorted


if (swapped == 0)
{
break;
}
}
}
// Main Code
int main()
{
// Declaring variable for maintaining the size of the array
int n;

// Input the size of the array


printf("Enter the number of elements: ");
scanf("%d", &n);

// Creating the array of size N


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]);
}

// Call the bubbleSort function


bubbleSort(arr, n);

// Output the sorted array


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

return 0;
}
------------------------------------------------------------------------------
Program 5: Implementing Selection Sort

#include <stdio.h>
// Function to implement the Selection Sort Technique
void selectionSort(int arr[], int n)
{
int i, j, minIndex, temp;

// Outer loop to iterate through the array


for (i = 0; i < n - 1; i++) {
// Step 1: Assume the first element of the unsorted part is the minimum
minIndex = i;

// Step 2: Find the smallest element in the unsorted part


for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j; // Update the index of the smallest element
}
}

// Step 3: Swap the found minimum element with the first element of the
unsorted part
if (minIndex != i)
{
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}

// Print the array after each iteration (optional for understanding)


printf("Array after iteration %d: ", i + 1);
for (int k = 0; k < n; k++)
{
printf("%d ", arr[k]);
}
printf("\n");
}
}
// Main Code
int main()
{
int n;

// Input the size of the array


printf("Enter the number of elements: ");
scanf("%d", &n);

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]);
}

// Call the selectionSort function


selectionSort(arr, n);

// Output the sorted array


printf("Sorted array:\n");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Input:
Enter the number of elements: 8
Enter 8 elements:
78
52
10
46
91
19
30
97
Output:
Array after iteration 1: 10 52 78 46 91 19 30 97
Array after iteration 2: 10 19 78 46 91 52 30 97
Array after iteration 3: 10 19 30 46 91 52 78 97
Array after iteration 4: 10 19 30 46 91 52 78 97
Array after iteration 5: 10 19 30 46 52 91 78 97
Array after iteration 6: 10 19 30 46 52 78 91 97
Array after iteration 7: 10 19 30 46 52 78 91 97
Sorted array:
10 19 30 46 52 78 91 97
------------------------------------------------------------------------------
Program 6: Implementing Insertion Sort
#include <stdio.h>
// Function to perform the Insertion Sort
void insertionSort(int arr[], int n)
{
int i, j, key;

// Outer loop to iterate through each element in the array


for (i = 1; i < n; i++)
{
// Step 1: Take the current element as the 'key'
key = arr[i];
j = i - 1;

// Step 2: Compare 'key' with elements in the sorted sublist


// Move elements of the sorted sublist that are greater than 'key' to one
position ahead
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}

// Step 3: Insert 'key' at the correct position


arr[j + 1] = key;

// Print the array after each iteration (optional for understanding)


printf("Array after iteration %d: ", i);
for (int k = 0; k < n; k++)
{
printf("%d ", arr[k]);
}
printf("\n");
}
}
// Main Code
int main()
{
int n;

// Input the size of the array


printf("Enter the number of elements: ");
scanf("%d", &n);

// Creating the Array


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]);
}

// Call the insertionSort function


insertionSort(arr, n);

// Output the sorted array


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

return 0;
}

Input:
Enter the number of elements: 10
Enter 10 elements:
56
12
48
79
90
64
37
10
5
29
Output:
Array after iteration 1: 12 56 48 79 90 64 37 10 5 29
Array after iteration 2: 12 48 56 79 90 64 37 10 5 29
Array after iteration 3: 12 48 56 79 90 64 37 10 5 29
Array after iteration 4: 12 48 56 79 90 64 37 10 5 29
Array after iteration 5: 12 48 56 64 79 90 37 10 5 29
Array after iteration 6: 12 37 48 56 64 79 90 10 5 29
Array after iteration 7: 10 12 37 48 56 64 79 90 5 29
Array after iteration 8: 5 10 12 37 48 56 64 79 90 29
Array after iteration 9: 5 10 12 29 37 48 56 64 79 90
Sorted array:
5 10 12 29 37 48 56 64 79 90
------------------------------------------------------------------------------

You might also like