0% found this document useful (0 votes)
15 views58 pages

Abhiroop dsa file

The document outlines a series of programming assignments related to Data Structures and Algorithms for a B-Tech CSE course at Mahavir Swami Institute of Technology and Management. It includes detailed descriptions and code implementations for various data structures such as arrays, linked lists, stacks, queues, and binary search trees, along with sorting and searching algorithms. Each program is presented with an aim, code, and functionality, demonstrating practical applications of data structures.

Uploaded by

arpita160205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views58 pages

Abhiroop dsa file

The document outlines a series of programming assignments related to Data Structures and Algorithms for a B-Tech CSE course at Mahavir Swami Institute of Technology and Management. It includes detailed descriptions and code implementations for various data structures such as arrays, linked lists, stacks, queues, and binary search trees, along with sorting and searching algorithms. Each program is presented with an aim, code, and functionality, demonstrating practical applications of data structures.

Uploaded by

arpita160205
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

MAHAVIR SWAMI INSTITUTE OF TECHNOLOGY

AND MANAGEMENT

Affiliated to MDU

Data Structure & Algorithms

Submitted by : Abhiroop ghosh


Course Name : B-Tech CSE
Registration no. : 2317681010

Subject Code : LC-CSE-213G


INDEX
SN Name of Program Page Date Remarks/Signature
No

1 Write a menu driven 5-13


program that implements
following operations using
separate functions for each
using a linear array:
2 Write a menu driven 14-19
programme that maintains a
linear linked list whose
elements are stored in a
ascending order and
implements the following
operations using seperate
functions

2
3 Write a program to 20-22
demonstrate the use of
stack(implemented using
linear array in converting
expression from infix
notation to postfix notation.
4 Program to 23-25
demonstrate
the
implementation
of various
operations on a
linear queue
represented
using a linear
array.

5 Program to demonstrate the 26-27


implementation of various
operations on a circular
queue represented using a
linear array.
6 Program to illustrate 28-34
implementation of different
operations on a BST.

7 Program to illustrate the 35-37


traversal of graph using
BFS.
8 Program to illustrate 38-42
the traversal of graph
using DFS.

9 Program to sort an array of 43-44


integers in ascending using
bubble sort.
10 Program to sort an array of 45-47
integers in ascending using
Selection sort.

11 Program to sort an array of 48-49


integers in ascending using
insertion sort.

12 Program to sort an array of 50-52


integers in ascending using
radix sort.

13 Program to sort an array of 53-55


integers in ascending using
merge sort.

14 Program to sort an array of 56-57


integers in ascending using
quick sort.

15 Program to demonstrate the 58-59


use of linear search to
search a given element in
an array.
16 Program to demonstrate the 60-61
use of binary search to
search a given element in
an array of ascending order.

4
Program 1
Aim:
Write a menu driven program that implements following
operations using separate functions for each using a linear array:
 insert a new element at end as well as at a given position
 delete an element from a given whose value is given or
whose position is given

 to find location of a given element⚫to display elements of a

linear array.

Code:

#include <stdio.h> void ins_end(int arr[], int *size, int

element); void ins_pos(int arr[], int *size, int position,

int element); void del_val(int arr[], int *size, int value);

void del_pos(int arr[], int *size, int position); int

search(int arr[], int size, int element); void show(int

arr[], int size); int main() { int arr[100];

int size = 0; int choice, element, position,

value; do { printf("\nMenu:\n");

printf("1. Insert at end\n"); printf("2.


Insert at position\n"); printf("3. Delete

by value\n"); printf("4. Delete by

position\n"); printf("5. Find location

of element\n"); printf("6. Display

array\n"); printf("7. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice); switch (choice)

{ case 1:

printf("Enter element to insert at end: ");

scanf("%d", &element);

ins_end(arr, &size, element);

break; case 2:

printf("Enter position to insert at: ");

scanf("%d", &position); printf("Enter

element to insert: "); scanf("%d",

&element); ins_pos(arr, &size,

6
position, element); break; case

3:

printf("Enter value to delete:

"); scanf("%d", &value);

del_val(arr, &size, value);

break; case 4:

printf("Enter position to delete: ");

scanf("%d", &position); del_pos(arr, &size,

position); break; case 5:

printf("Enter element to find: ");

scanf("%d", &element); position = search(arr, size,

element); if (position != -1)

{ printf("Element found at position %d\n",

position);

} else { printf("Element not

found in the array\n");


}

break; case 6:

show(arr, size);

break; case 7:

printf("Exiting the program\n");


break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 7); return 0; } void

ins_end(int arr[], int *size, int element)

{ arr[*size] = element;

(*size)++; printf("Element %d inserted at the

end.\n", element);

} void ins_pos(int arr[], int *size, int position, int

element) { if (position < 0 || position > *size)

8
{ printf("Invalid position for insertion.\n");

return;

}
for (int i = *size; i > position; i--)

{ arr[i] = arr[i - 1];

arr[position] = element;

(*size)++; printf("Element %d inserted at

position %d.\n", element, position); } void del_val(int

arr[], int *size, int value) { int found = 0 for (int i = 0; i

< *size; i++) { if (arr[i] == value) { for (int j =

i; j < *size - 1; j++) { arr[j] = arr[j + 1];

(*size)--;

found = 1;

printf("Element

%d deleted from
the array.\n",

value);

break;

} } if (!found) { printf("Element %d not

found in the array.\n", value);

} void del_pos(int arr[], int *size, int

position) { if (position < 0 || position >=

*size) { printf("Invalid position for

deletion.\n"); return; } for (int i =

position; i < *size - 1; i++) { arr[i] =

arr[i + 1];

(*size)--;
printf("Element at position %d deleted from the array.\n",

position); } int search(int arr[], int size, int element) { for

10
(int i = 0; i < size; i++) { if (arr[i] == element)

{ return i;

} } return -1; } void

show(int arr[], int size)

{ printf("Array elements:

"); for (int i = 0; i < size;

i++) { printf("%d ",

arr[i]);

printf("\n");}
Program 2
Aim:
Write a menu driven programme that maintains a linear linked
list whose elements are stored in a ascending order and
implements the following operations using seperate functions
 insert a new element
 delete an existing element

 search an element⚫display all the elements.


Code:
#include <stdio.h>
#include <stdlib.h>

12
struct Node { int
data; struct Node
*next;
}; struct Node* createNode(int data); struct Node*
ins_element(struct Node* head, int element); struct Node*
del_element(struct Node* head, int element); int
search(struct Node* head, int element); void show(struct
Node* head); int main() { struct Node *head = NULL;
int choice, element, result;

do {
printf("\nWhat would you like to do?:\n");
printf("1. Insert an element in the list\n");
printf("2. Delete an element from the list\n");
printf("3. Search for an element\n");
printf("4. Display the linked list\n");
printf("5. Quit\n"); printf("Enter your
choice: "); scanf("%d", &choice);
switch (choice) { case 1:
printf("Enter element to insert in the linked list:
"); scanf("%d", &element); head =
ins_element(head, element); break; case 2:
printf("Enter element to delete from the list:
"); scanf("%d", &element); head =
del_element(head, element); break;
case 3:
printf("Enter element to search in the list: ");
scanf("%d", &element);
result = search(head, element); if
(result) printf("Element found in the
list.\n"); else printf("Element
not found in the list.\n"); break;
case 4: show(head); break;
case 5:
printf("Exiting the
program\n"); break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5); return
0; } struct Node* createNode(int
data) {
struct Node* newNode = (struct
Node*)malloc(sizeof(struct Node)); if (newNode ==

14
NULL) { printf("Memory allocation failed.\n");
exit(1);
} newNode->data =
data; newNode->next =
NULL; return newNode;
} struct Node* ins_element(struct Node* head, int
element) { struct Node *newNode =
createNode(element); struct Node *current = head,
*prev = NULL; while (current != NULL && current-
>data < element) { prev = current; current =
current->next;
}
if (prev == NULL)
{ newNode->next = head;
head = newNode;
} else { newNode-
>next = current; prev-
>next = newNode;
}
printf("Element %d inserted into the linked list.\n", element);
return head;
} struct Node* del_element(struct Node* head, int
element) { struct Node *current = head, *prev = NULL;
while (current != NULL && current->data != element)
{ prev = current; current = current->next;
}
if (current != NULL)
{ if (prev == NULL)
{ head = current-
>next;
} else { prev->next =
current->next;
} free(current); printf("Element %d deleted
from the list.\n", element);
} else { printf("Element %d not found in the
list.\n", element);
} return
head;
} int search(struct Node* head, int element) { struct
Node* current = head; while (current != NULL &&
current->data < element) { current = current->next;
}
return (current != NULL && current->data == element);
} void show(struct Node* head)
{ printf("List elements: ");
struct Node* current = head;
while (current != NULL)

16
{ printf("%d ", current-
>data); current = current-
>next;
}
printf("\n");
}

Program 3
Aim:
Write a program to demonstrate the use of stack(implemented
using linear array in converting expression from infix notation to
postfix notation.
Code:
#include <stdio.h>
#include <ctype.h>
char stack[100]; int
top = -1; void
push(char x)
{ stack[++top] =
x;
} char pop() { if (top
== -1) { return -
1; } else
{ return stack[top-
-];
}
} int priority(char
x) { if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1; if (x == '*' || x == '/') return 2;
return 0; } int main() { char exp[100]; char *e, x;
printf("Enter the expression: "); scanf("%s", exp);
printf("The postfix notation for the given infix expression
is:\n"); e = exp; while (*e != '\0') { if

18
(isalnum(*e)) { printf("%c ", *e); } else if (*e
== '(') { push(*e); } else if (*e == ')')
{ while ((x = pop()) != '(') { printf("%c ",
x);
}
} else {
while (priority(stack[top]) >= priority(*e)) {
printf("%c ", pop());
}
push(*e);
}
e++;
}
while (top != -1) {
printf("%c ", pop());
}
printf("\n");
return 0;
}

20
Program 4
Aim:
Program to demonstrate the implementation of various
operations on a linear queue represented using a linear array.
Code:
#include <stdio.h> #define N 5 int main() { int
queue[N], ch = 1, front = 0, rear = 0, i, j = 1;
printf("Queue using Array\n"); printf("1.
Insertion\n"); printf("2. Deletion\n");
printf("3. Display\n"); printf("4. Exit\n");
while (ch) { printf("\nEnter your choice: ");
scanf("%d", &ch); switch (ch) { case
1:
if (rear == N)
{ printf("\nQueue is Full\n");
} else { printf("\nEnter
number %d: ", j++); scanf("%d",
&queue[rear++]);

}
break;
case 2:
if (front == rear)
{ printf("\nQueue is Empty\n");
} else { printf("\nDeleted
Element is %d\n", queue[front++]);
}
break;
case 3:
printf("\nQueue Elements are:\n");
if (front == rear)
{ printf("Queue is Empty\n");
} else { for (i =
front; i < rear; i++)
{ printf("%d ", queue[i]);
}
printf("\n");
}
break;
case 4:
printf("Exiting the program\n");
return 0;
default:
printf("Wrong Choice. Please see the options.\n");
break;
} }
return 0;
}

22
Program 5 Aim:
Program to demonstrate the implementation of various
operations on a circular queue represented using a linear array.
Code :
#include <stdio.h> #define MAX 6 int queue[MAX]; int
front = -1; int rear = -1; void enqueue(int element) { if
((rear + 1) % MAX == front) { // Condition to check if
queue is full printf("Queue is overflow.\n"); } else if
(front == -1 && rear == -1) { front = 0; rear =
0; queue[rear] = element;
} else { rear = (rear +
1) % MAX; queue[rear]
= element;
}
}

void dequeue() { if (front == -1 && rear == -1)


{ printf("Queue is underflow.\n"); } else if (front
== rear) { printf("The dequeued element is %d\n",
queue[front]); front = -1; rear = -1; } else
{ printf("The dequeued element is %d\n",
queue[front]); front = (front + 1) % MAX;
}
}

24
Program 6
Aim:
Program to illustrate implementation of different operations on a
BST.
Code:

#include <stdio.h>
#include <stdlib.h> struct
node { int data; struct
node *right_child;
struct node *left_child;
}; struct node* new_node(int x)
{ struct node *temp; temp =
malloc(sizeof(struct node)); temp-
>data = x; temp->left_child =
NULL; temp->right_child =
NULL; return temp;
} struct node* search(struct node *root, int
x) { if (root == NULL || root->data == x)
return root;
else if (x > root->data) return
search(root->right_child, x); else
return search(root->left_child, x);
} struct node* insert(struct node *root, int x) { if
(root == NULL) return new_node(x); else if
(x > root->data) root->right_child =
insert(root->right_child, x); else root-
>left_child = insert(root->left_child, x); return
root;
} struct node* find_minimum(struct node
*root) { if (root == NULL) return
NULL; else if (root->left_child != NULL)
return find_minimum(root->left_child);
return root;
} struct node* delete(struct node *root, int
x) { if (root == NULL)
return NULL; if (x > root->data) root-
>right_child = delete(root->right_child, x); else if
(x < root->data) root->left_child = delete(root-
>left_child, x); else {
if (root->left_child == NULL && root->right_child ==
NULL) {
free(root);
return NULL;

26
} else if (root->left_child == NULL || root->right_child ==
NULL) {
struct node *temp; if
(root->left_child == NULL)
temp = root->right_child; else
temp = root->left_child;
free(root); return temp;
} else { struct node *temp = find_minimum(root-
>right_child); root->data = temp->data; root-
>right_child = delete(root->right_child, temp>data);
} } return root; } void
inorder(struct node *root) { if
(root != NULL)
{ inorder(root->left_child);
printf("%d ", root->data);
inorder(root->right_child);
}
} int main() { struct node *root =
new_node(20); insert(root, 52);
insert(root, 13); insert(root, 17);
insert(root, 14); insert(root, 7);
insert(root, 12); insert(root, 3);
insert(root, 5); insert(root, 20);
insert(root, 65);
insert(root, 92); printf("The BST in inorder
traversal is: \n"); inorder(root);
printf("\n"); root = delete(root, 12); root =
delete(root, 65); root = delete(root, 52);
root = delete(root, 3); printf("After deletion,
the BST becomes: \n"); inorder(root);
printf("\n"); return 0; } void display() { int
i = front; if (front == -1 && rear == -1)
{ printf("Queue is empty.\n");
} else { printf("Elements in the
Queue are: "); while (i != rear)
{ printf("%d ", queue[i]); i
= (i + 1) % MAX;
}
printf("%d\n", queue[rear]);
}
} int main() { int choice = 1, x; while
(choice < 4 && choice != 0)
{ printf("\nPress 1: Insert an
element"); printf("\nPress 2: Delete
an element"); printf("\nPress 3:
Display the queue"); printf("\nEnter

28
your choice: "); scanf("%d",
&choice); switch (choice)
{ case 1:
printf("Enter the element to be inserted:
"); scanf("%d", &x);
enqueue(x); break; case 2:
dequeue(); break; case 3:
display(); break;
default:
printf("Invalid choice. Please try again.\n");
} }
return 0;
}
Program 7
Aim:
Program to illustrate the traversal of graph using BFS.
Code:
#include <stdio.h>
int n, i, j; int visited[10], queue[10], front = -1,
rear = -1; int adj[10][10]; void bfs(int v)
{ visited[v] = 1; queue[++rear] = v;
while (front < rear) { int current =
queue[++front]; for (i = 1; i <= n; i++)
{ if (adj[current][i] && !visited[i])
{ queue[++rear] = i;
visited[i] = 1;
}
}

30
}
} int
main()
{ int v;
printf("Ent
er the
number of
vertices: ");
scanf("%d"
, &n);
for (i = 1; i
<= n; i++)
{ que
ue[i] = 0;
visited[i] =
0;
}
printf("Enter graph data in matrix form:\n");
for (i = 1; i <= n; i++) for (j = 1; j <= n;
j++) scanf("%d", &adj[i][j]);
printf("Enter the starting vertex: ");
scanf("%d", &v); bfs(v); printf("The
nodes that are reachable are:\n"); int
reachable = 0; for (i = 1; i <= n; i++)
{ if (visited[i]) { printf("%d\t", i);
reachable = 1;
} } if
(!reachable)
{ printf("BF
S is not possible.
Not all nodes are
reachable.");

}
printf("\n");
return 0;
}

32
Program 8
Aim:
Program to illustrate the traversal of graph using DFS.

Code:

#include <stdio.h>

#include <stdlib.h>

struct node { int

vertex; struct

node* next;

}; struct Graph { int

totalVertices; int*

visited; struct node**

adjLists;
}; struct node* createNode(int v) { struct node*

newNode = malloc(sizeof(struct node)); newNode-

>vertex = v;

newNode->next = NULL;

return newNode;

} struct Graph* createGraph(int vertices) { struct

Graph* graph = malloc(sizeof(struct Graph));

graph->totalVertices = vertices; graph->adjLists =

malloc(vertices * sizeof(struct node*)); graph-

>visited = malloc(vertices * sizeof(int));

int i; for (i = 0; i < vertices;

i++) { graph->adjLists[i] =

NULL; graph->visited[i] =

0;

} return

graph;

} void addEdge(struct Graph* graph, int src, int dest)

{ struct node* newNode = createNode(dest);

34
newNode->next = graph->adjLists[src]; graph-

>adjLists[src] = newNode; newNode =

createNode(src); newNode->next = graph-

>adjLists[dest]; graph->adjLists[dest] = newNode;

} void DFS(struct Graph* graph, int vertex)

{ struct node* adjList = graph-

>adjLists[vertex]; struct node* temp = adjList;

graph->visited[vertex] = 1; printf("%d -> ",

vertex); while (temp != NULL) { int

connectedVertex = temp->vertex; if (graph-

>visited[connectedVertex] == 0) {

DFS(graph, connectedVertex);

temp = temp->next;

}
} void displayGraph(struct Graph* graph)

{ for (int v = 0; v < graph->totalVertices; v++)

{ struct node* temp = graph->adjLists[v];


printf("\n%d => ", v); while (temp)

{ printf("%d -> ", temp->vertex);

temp = temp->next;

printf("NULL");

printf("\n");

} int main() { struct Graph* graph =

createGraph(8) addEdge(graph, 1, 5);

addEdge(graph, 1, 2);

addEdge(graph, 1, 3);

addEdge(graph, 3, 6); addEdge(graph, 2, 7);

addEdge(graph, 2, 4); printf("The Adjacency List of the

Graph is:"); displayGraph(graph); printf("\nDFS

traversal of the graph starting from vertex 1:\n");

DFS(graph, 1);

printf("NULL\n");

return 0;

36
}

Program 9

Aim :
Program to sort an array of integers in ascending using bubble
sort.
Code
#include <stdio.h> void
bubble_sort(int arr[], int n) {
int i, j; for (i = 0; i < n - 1;
i++) { for (j = 0; j < n - 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 arr[] = {48, 21, 67,
12, 25, 9, 87}; int n = sizeof(arr) /
sizeof(arr[0]); bubble_sort(arr, n);

printf("Sorted array: ");


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

return 0;
}

38
Program 10

Aim:

Program to sort an array of integers in ascending using Selection


sort.

Code:

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;
*b = temp;

void selectionSort(int array[], int size) {

for (int step = 0; step < size - 1; step++) {


int min_idx = step;

for (int i = step + 1; i < size; i++) {

if (array[i] < array[min_idx])

min_idx = i;

swap(&array[min_idx], &array[step]);

void printArray(int array[], int size) {

40
for (int i = 0; i < size; ++i) {

printf("%d ", array[i]);

}
printf("\n");

int main() {

int data[] = {28, 17, 11, 9, 25};

int size = sizeof(data) / sizeof(data[0]);

selectionSort(data, size);

printf("Sorted array in Ascending Order:\n");

printArray(data, size);

return 0;

}
Program 11

Aim:

Program to sort an array of integers in ascending using insertion


sort.
Code :
#include <stdio.h> void
insert(int a[], int n)
{ int i, j, temp;
for (i = 1; i < n; i++)
{ temp = a[i];
j = i - 1;
while (j >= 0 && temp < a[j]) {
a[j + 1] = a[j];
j = j - 1;
} a[j +
1] = temp;
} } void printArr(int
a[], int n) {

42
int i; for (i = 0;
i < n; i++)
printf("%d ", a[i]);
printf("\n");
} int main() { int a[] = { 21, 32, 45, 18, 22,
1 }; int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n); insert(a, n);
printf("After sorting array elements are - \n");
printArr(a, n); return 0;
}
Program:12
Aim:
Program to sort an array of integers in ascending using radix
sort.
Code
#include <stdio.h> int
getMax(int array[], int n)
{ int max = array[0];
for (int i = 1; i < n; i++)
{ if (array[i] > max)
max = array[i];
} return
max;
} void countingSort(int array[], int size, int
place) { int output[size]; int count[10] =
{0}; for (int i = 0; i < size; i++)
{ count[(array[i] / place) % 10]++;
} for (int i = 1; i < 10;
i++) { count[i] +=
count[i - 1];
} for (int i = size - 1; i >= 0; i--)
{ output[count[(array[i] / place) % 10] - 1] =
array[i];

46
count[(array[i] / place) % 10]--;
} for (int i = 0; i < size;
i++) { array[i] =
output[i];
} } void radixsort(int array[], int size) { int
max = getMax(array, size); for (int place = 1;
max / place > 0; place *= 10)
{ countingSort(array, size, place);
} } void printArray(int array[], int
size) { for (int i = 0; i < size; i++)
{ printf("%d ", array[i]);
}
printf("\n");
} int main() { int array[] = {127, 532,
264, 13, 18, 5, 78}; int n = sizeof(array) /
sizeof(array[0]); radixsort(array, n);
printArray(array, n); return 0;
}
Program 13
Aim:
Program to sort an array of integers in ascending using merge
sort.
Code

48
#include <stdio.h> #include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{ int i, j, k; int n1 = m - l + 1;
int n2 = r - m; int L[n1], R[n2];
for (i = 0; i < n1; i++) L[i] = arr[l
+ i]; for (j = 0; j < n2; j++) R[j]
= arr[m + 1 + j]; i = 0; j = 0; k
= l;
while (i < n1 && j < n2)
{ if (L[i] <= R[j])
{ arr[k] = L[i];
i++;
} else
{ arr[k] =
R[j];
j++; }
k++;
} while (i <
n1) { arr[k]
= L[i]; i++;
k++;
} while (j <
n2) { arr[k]
= R[j]; j++;
k++;
}
} void mergeSort(int arr[], int l, int r)
{ if (l < r) { int m = l + (r -
l) / 2; mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
} void printArray(int A[], int size)
{ for (int i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
} int main() { int arr[] = { 22, 14, 63, 5,
16, 2 }; int arr_size = sizeof(arr) /
sizeof(arr[0]); printf("Given array is
\n"); printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size); return 0;
}

50
Program 14
Aim:
Program to sort an array of integers in ascending using quick sort.
Code
#include <stdio.h> void
swap(int *a, int *b)
{ int t = *a; *a = *b;
*b = t; } int partition(int array[], int
low, int high) { int pivot = array[high];
int i = (low - 1); for (int j = low; j <
high; j++) { if (array[j] <= pivot)
{ i++; swap(&array[i],
&array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
} void quickSort(int array[], int low, int high)
{ if (low < high) { int pi =
partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
} void printArray(int array[], int size)
{ for (int i = 0; i < size; ++i)
{ printf("%d ", array[i]);
}
printf("\n");
} int main() { int data[] = {7, 2, 4, 3, 8, 1,
5}; int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array:\n");
printArray(data, n); quickSort(data, 0, n -
1); printf("Sorted array in ascending
order:\n"); printArray(data, n); return 0;
}

Program 15
Aim :
Program to demonstrate the use of linear search to search a
given element in an array.
52
Code:
#include <stdio.h> int main() { int
array[100], search, c, n; printf("Enter number
of elements in array\n"); scanf("%d", &n);
printf("Enter %d integer(s)\n", n); for (c = 0;
c < n; c++) { scanf("%d", &array[c]);
}
printf("Enter a number to search\n"); scanf("%d",
&search); for (c = 0; c < n; c++) { if (array[c] ==
search) { printf("%d is present at location %d.\n",
search, c + 1); break;
} }
if (c == n)
{ printf("
%d isn't
present in the
array.\n",
search);

}
return 0;
}
Program 16
Aim :
Program to demonstrate the use of binary search to search a
given element in an array of ascending order.
54
Code:
#include <stdio.h> int binarySearch(int a[], int
beg, int end, int val) { int mid; if (end >=
beg) { mid = (beg + end) / 2; if (a[mid]
== val) { return mid + 1; // return 1-based
index
} else if (a[mid] > val)
{ return binarySearch(a, beg, mid - 1,
val);
} else { return binarySearch(a,
mid + 1, end, val);
} }
return -1;
} int
main()
{ int a[]
= {17, 24,
35, 40, 55,
61, 72, 87,
90}; //
given array
int val =
40; // value
to be
searched
int n =
sizeof(a) /
sizeof(a[0]
); // size of
array int
res =
binarySear
ch(a, 0, n -
1, val); //
Store result
printf("The
elements of
the array
are - ");
for (int i =
0; i < n;
i++)
printf("%d
", a[i]);
printf("\nEl
ement to
56
be
searched is
- %d", val);
if (res == -
1)
printf("\nEl
ement is
not present
in the
array");
else
printf("\nEl
ement is
present
at %d
position of
array",
res);
return 0;
}
58

You might also like