IT23302-DATA STRUCTURES
LAB RECORD
Name
2024506083
Ex no:1
Date:9/7/2025
Practice of programming
Aim
To practice of c programming in solving real time problems using
arrays,function,pointers , structure,and preprocessor directives.
a) Simple Main Program
ALGORITHM
1. Start
2. Declare an integer variable x
3. Display the message: "Enter a number:"
4. Read the value of x
5. Calculate the successor number x + 1
6. Display the successor of the number
[Link] 0
[Link]
Source code
#include<stdio.h>
Int main()
{
Int x;
Printf(“enter a number”);
Scanf(“%d”,&x);
Printf(“%d is the successive number of %d”,x+1,x)
Return 0;
}
Output
Enter a number:8
A is the successive of 5
b)if condition program
Algorithm
1. Start
2. Declare an integer variable number and initialize it with 10
3. Check if number > 0
If true, display "The number is positive"
4. return 0
[Link]
Source code
#include <stdio.h>
int main()
{
int number=10;
if (number >0);
{
printf("The number is positive.\n");
}
return 0;
}
Output
The number is positive
C) for loop
Algorithm
1. Start
2. Initialize count = 1
3. Repeat for count from 1 to 5
a) Print count
4. End
Source code
#include <stdio.h>
int main() {
int count;
for (count = 1; count <= 5; count++) {
printf("%d\n", count);
}
return 0;
}
Output:
Count 1
Count 2
Count 3
Count 4
Count 5
d) while loop
Alogrithm
1. Start
2. Initialize i = 1
3. Check if i <= 5
If true, go to step 4
If false, go to step 6
4. Print the value of i
5. Increment i by 1 and repeat step 3
[Link] 0
[Link]
Source code
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
e)do while
Algorithm
1. Start
2. Initialize i = 1
3. Do the following:
1. Print i
2. Increment i by 1
4. While i <= 5, repeat step 3
[Link] 0
[Link]
Source code
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Output:
1
2
3
4
5
f)Array int
Algroithm
1. Start
2. Declare an integer array numbers[5]
3. Initialize the array with 5 values (example: 50, 10, 20, 30, 40)
4. Set i = 0
5. Repeat until i < 5:
1. Print numbers[i]
2. Increment i by 1
[Link] 0
[Link]
Source code
#include <stdio.h>
int main()
{
int numbers[5] = {10,20,30,40,50};
printf("Array elements are:\n");
for (i = 0; i < 5; i++)
{
printf("%d\n", numbers[i]);
}
return 0;
}
Output:
Array elements are:
10
20
30
40
50
G)Array char
Algorithm
1. Start
2. Declare a character array word[10]
3. Initialize the array with string "HELLO"
4. Print the string using printf or a loop
5. End
Source code
#include <stdio.h>
int main()
{
char name[] = "HELLO";
printf("String is: %s\n", word);
return 0;
}
Output:
Name:HELLO
h)call by value
Algorithm
1. Algorithm: Call by Value
1. Start
2. Read an integer a
3. Call increment(a)
4. Inside the function increment(x)
1. Increase x by 1
2. Print the value of x
5. Return to main
6. Print the value of a in main (unchanged)
7. return 0
[Link]
Source code
#include<stdio.h>
Void increament(int x)
{
X=x+1;
}
Int main()
{
Int a=5;
Increament(a);
Printf(“after increament:%d\n”,a);
Return 0;
}
Output
After increament 5
i)call by reference
Algorithm
1. Start
2. Read an integer a
3. Call increment(&a) (pass address)
4. Inside the function increment(ptr)
1. Increase the value at *ptr by 1
2. Print the value at *ptr
5. Return to main
6. Print the value of a in main (changed)
7. return 0
Source code
#include<stdio.h>
Void increament(int*x)
{
(*x)++;
}
Int main()
{
Int a=5;
Increament(&a);
Printf(“after increament:%d\n”,a);
Return 0;
}
Output
After increament 6
j)pointer
Algorithm
1. Start
2. Declare an integer variable a and assign a value
3. Declare a pointer p and assign the address of a to p
4. Print:
1. Value of a
2. Address of a
3. Value of a using pointer *p
4. Address stored in p
[Link] 0
[Link]
Source code
#include<stdio.h>
Int main()
{
Int a=10;
Int*p=&a;
Printf(“value of a %d\n”,a);
Printf(“address of a: %p \n”,p);
Printf(value via pointer:%d\n”,*p);
Return 0;
}
Output
Value of a:10
Address of a:0
Value via pointer:10
k)structure
Algorithm
1. Start
2. Define a structure Point with members x and y
3. Declare a variable p of type Point
4. Assign values to p.x and p.y (e.g., 10 and 20)
5. Print the coordinates
6. return
Source code
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p;
p.x = 10;
p.y = 20;
printf("Point coordinates: (%d, %d)\n", p.x, p.y);
return 0;
}
Output:
Point coordinates: (10, 20)
j) File Handling – Write Operation
Algorithm
1. Start
2. Declare a FILE *fp pointer
3. Open a file in write mode using fopen("[Link]", "w")
4. If the file opens successfully:
1. Write a message to the file using fprintf or fputs
2. Close the file using fclose(fp)
5. return 0
Source code
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("[Link]", "w");
if (fp != NULL)
{
Printf(“error opening file!\n”);
Return 1;
fprintf(fp, "Hello, This is a test file\n");
fclose(fp);
printf("Data written successfully.\n");
} else {
printf("File could not be opened.\n");
}
return 0;
}
Output
Hello ,this is a test file
l)preprocessor
Algorithm
1. Start
2. Use #define to create a macro (example: PI 3.14159)
3. Print the macro value in main
4. End
Source code
#include <stdio.h>
#define PI 3.14159
int main()
{
printf("Value of PI = %f\n", PI);
return 0;
}
Output:
Value of PI = 3.141590
Ex no:2
1)Binary search
Algorithm
1. Start
2. Initialize:
low = 0
high = size- 1
3. Repeat while low <= high:
1. Compute mid = (low + high) / 2
2. If arr[mid] == key:
Element found → return position
3. Else if arr[mid] < key:
Set low = mid + 1
4. Else:
Set high = mid - 1
4. If low > high:
Element not found
[Link] 0
Source code
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = size- 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
return mid; // Element found
else if (arr[mid] < key)
low = mid + 1
else
high = mid - 1;
}
return -1; // Element not found
}
int main() {
int arr[50], n, key, result;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements (sorted):\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i])
printf("Enter the element to search: ");
scanf("%d", &key);
result = binarySearch(arr, n, key);
if (result == -1)
printf("Element not found.\n");
else
printf("Element found at position %d (index %d).\n", result + 1, result);
return 0;
}
Output
Enter number of elements :5
Enter 5 sorted elements
2 5 8 12 20
Elements found at position 3(index 2)
2)Linear search
Algorithm
1. Start
2. Input the array elements and the key element to search.
3. For each element in the array (from index 0 to n-size):
1. Compare the current element with the key.
2. If they are equal, element found → return position.
4. If the loop ends without finding the key:
Element is not found.
5. return 0
Source code
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key)
return i; // Return the index if found
}
return -1; // Element not found
}
int main() {
int arr[50], n, key, result;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &key);
result = linearSearch(arr, n, key);
if (result == -1)
printf("Element not found.\n");
else
printf("Element found at position %d (index %d).\n", result + 1, result);
return 0;
}
Output
Enter number of elements :5
Enter 5 elements :
10 25 30 40 50
Enter elements to search:30
Elements found at position 3(index 2)
3)Bubble sort
Algorithm
1. Start
2. Input the number of elements n.
3. Input n array elements.
4. Repeat for i = 0 to n-2:
1. Repeat for j = 0 to n-i-2:
If arr[j] > arr[j+1], swap them.
5. After all passes, the array is sorted.
6. Print the sorted array.
7. return 0
Source code
#include <stdio.h>
void bubbleSort(int arr[], int size) {
int i, j, temp;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {45, 12, 98, 32, 5, 67};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
bubbleSort(arr, size);
printf("Sorted array: ");
printArray(arr, size);
return 0;
}
Output
Original array:45 12 98 32 5 67
Sorted array: 5 12 32 45 67 98
4)Insertion sort
Algorithm
1. Start
2. Input the number of elements n.
3. Input n elements into the array arr.
4. Repeat for i = 0 to n-2:
1. Set min_index = i
2. Repeat for j = i+1 to n-1;
If arr[j] < arr[min_index], set min_index = j
3. Swap arr[i] and arr[min_index]
5. Print the sorted array.
6. return 0
Source code
#include <stdio.h>
void selectionSort(int arr[], int n)
{
int i, j, min_index, temp;
for (i = 0; i < n - 1; i++) {
min_index = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index])
min_index = j;
}
temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {7, 2, 9, 4, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Output
Before sorting 7 2 9 4 6
After descending sort 9 7 6 4 2
3)selection sort
Algorithm
1. Start
2. Input number of elements n
3. Input n array elements into arr
4. For i = 0 to n-2
1. Set min_index = i
2. For j = i+1 to n-1
If arr[j] < arr[min_index], set min_index = j
3. Swap arr[i] and arr[min_index] (if different)
5. Print the sorted array
6. return 0
Source code
#include <stdio.h>
void selectionSort(int arr[], int size) {
int i, j, min_idx, temp;
for (i = 0; i < size - 1; i++) {
min_idx = i;
for (j = i + 1; j < size; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
if (min_idx != i) {
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, size);
selectionSort(arr, size);
printf("Sorted array: ");
printArray(arr, size);
return 0;
}
Output
Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64
4)Radix sort
Algorithm
1. Find the maximum number in the array to determine the number of digits.
2. Initialize exponent exp = 1 (unit place).
3. Repeat until max / exp > 0:
1. Perform Counting Sort based on the current digit (arr[i]/exp)%10.
2. Increase exp by a factor of 10 to move to the next digit (tens, hundreds...).
4. After all digits are processed, the array is sorted.
Source code
#include <stdio.h>
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
void countSort(int arr[], int n, int exp)
{
int output[size];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i – 1];
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
radixSort(arr, n);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Output
Original array:
170 45 75 90 802 24 2 66
Sorted array:
2 24 45 66 75 90 170 802
5)Merge sort
Algorithm
1. If the array has one element, it is already sorted → return.
2. Find the middle index of the array.
3. Recursively apply merge sort on the left half.
4. Recursively apply merge sort on the right half.
5. Merge the two sorted halves into a single sorted array.
Source code
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Output
Original array:
38 27 43 3 9 82 10
Sorted array:
3 9 10 27 38 43 82
6)Quite sort
Algorithm
1. Choose a pivot element from the array.
2. Partition the array so that:
All elements less than pivot go to the left.
All elements greater than pivot go to the right.
3. Recursively apply Quick Sort to the left and right subarrays.
4. When subarrays have one element, they are sorted.
5. If low < high
6. Partition the array to find the pivot position p.
7. Recursively sort elements before and after pivot.
[Link] 0
Source code
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Output
Original array:
10 7 8 9 1 5
Sorted array:
1 5 7 8 9 10
7) count sort
Algorithm
1. Find the maximum element in the array.
2. Create a count array of size max+1 and initialize all elements to 0.
3. Count the occurrences of each element in the input array.
4. Update the count array such that each element at index i contains the sum of
counts up to i (cumulative count).
5. Build the output array using the cumulative counts (place elements in their correct
sorted positions).
6. Copy the sorted elements back to the original array.
Source code
#include <stdio.h>
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
void countSort(int arr[], int n) {
int max = getMax(arr, n);
int count[max + 1];
int output[n];
for (int i = 0; i <= max; i++)
count[i] = 0;
for (int i = 0; i < n; i++)
for (int i = 1; i <= max; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
int main() {
int arr[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
countSort(arr, n);
printf("\nSorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output
Original array:4 2 2 8 3 3 1
Sorted array:1 2 2 3 3 4 8