You are on page 1of 51

Department of Computer Science &

Faculty of Engineering

MEDI-CAPS UNIVERSITY
INDORE- 453331

Data Structure

Course Code: - CS3C021


Lab Manual

BY

Ranu Sharma: EN19CS305036

Submitted to
Ms. Anusha Jain
1. Write a program to perform insertion in 1-D Array
#include<stdio.h>
int main() {
int arr[30], element, num, i, location;
printf("\nEnter no of elements :\n");
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}
printf("\nEnter the element to be inserted :\n");
scanf("%d", & element);
printf("\nEnter the location\n");
scanf("%d", &location);
for (i = num; i >= location; i--) {
arr[i] = arr[i - 1];
}
num++;
arr[location - 1] = element;
for (i = 0; i < num; i++)
printf("\n %d", arr[i]);
return (0);
}
2. Write a program to perform traversing in 1-D Array
#include <stdio.h>
void printArray(int* arr, int n)
{
int i;
printf("Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2, -1, 5, 6, 0, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
return 0;
}

3. Write a program to perform insertion in 2-D Array


#include <stdio.h>
int main()
{
int b[2][3];
int i, j, num;
printf("Enter elements into 2-D array: ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
b[0][2] = 10;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n");
}
return 0;
}

4. Write a program to perform traversing in 2-D Array.


# include<iostream>
using namespace std;
class Two_Dimension
{
private: int i, j;
float abc[10][10];
Public:
void Traverse ( int, int);
void input( int, int);
};
void Two_Dimension :: Traverse (int row, int col)
{
cout<<"\n Traversing in row \n";
for( i = 0; i < row; i++)
{
for( j = 0; j < col; j++)
{
cout<<"\n"<<&abc[i][j];
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in column \n";
for( j = 0; j < col; j++)
{
for( i = 0; i < row; i++)
{
cout<<" \n"<<&abc[i][j];
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in row \n";
for( i = 0; i < row; i++)
{
for( j = 0; j < col; j++)
{
cout<<" abc["<<i<<"]"<<"["<<j<<"] = ";
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
cout<<"\n Traversing in column \n";
for( j = 0; j < col; j++)
{
for( i = 0; i < row; i++)
{
cout<<" abc["<<i<<"]"<<"["<<j<<"] = ";
cout<<" "<<abc[i][j];
}
cout<<"\n";
}
}
void Two_Dimension :: input( int row, int col)
{
for( i = 0 ; i< row; i++)
{
for( j = 0 ; j<col; j++)
{
cout<<"\nInput Value for : "<< i+1 <<" : "<<j+1<<" : " ;
cin>>abc[i][j];
}
}
}
int main()
{
Two_Dimension Trav;
int r,c;
cout<<"\n Enter the number of rows:";
cin>>r;
cout<<" Enter the number of columns:";
cin>>c;
Trav.input(r,c);
Trav.Traverse(r,c);
}
5. Write a program to perform addition of all elements in 1-D Array.
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10],i,n,sum=0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum+=a[i];
}
printf("sum of array is : %d",sum);
return 0;
}

6. Write a program to perform addition of two matrix.


#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]); }
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
7. Write a program to insert an element at a specified index in an array.
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
for (i = 0; i < 10; i++)
arr[i] = i + 1;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
x = 50;
pos = 5;
n++;
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

8. Write a program to delete an element at specified index in array


#include <stdio.h>
#define MAX_SIZE 20
int main()
{
int arr[MAX_SIZE];
int i, size, pos;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element position to delete : ");
scanf("%d", &pos);
if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position between 1 to %d", size);
}
else
{
for(i= pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}
size--;
printf("\nElements of array after delete are : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
}
return 0;
}
9. Write a program to print Fibonacci series up to 5 position using recursion.
#include<stdio.h>
int fibonacci(int);
int main(void)
{
int terms;
printf("Enter terms: ");
scanf("%d", &terms);
for(int n = 0; n < terms; n++)
{
printf("%d ", fibonacci(n));
}
return 0;
}
int fibonacci(int num)
{
if(num == 0 || num == 1)
{
return num;
}
else
{
return fibonacci(num-1) + fibonacci(num-2);
}
}
10. Program of pointer form PPT.
#include<stdio.h>
int main()
{
int val = 100;
printf("%u\n", &val);
printf("%d\n", val);
printf("%d\n", *(&val));
return 0;
}

11. Program of pointer form PPT.


#include<stdio.h>
int main()
{
int n = 10;
int *ptr;
ptr= &n;
printf("Value of n is %d\n", n);
printf("Address of n is %x\n", &n);
printf("Address of pointer is %x\n", ptr);
printf("Value stored in pointer is %d\n", *ptr);
return 0;
}

12. Program to swap two numbers using a pointer


#include<stdio.h>
int main()
{
int r;
float a, *b;
printf("Enter the radius of circle\n");
scanf("%d", &r);
a=3.14*r*r;
b=&a;
printf("The value of a= %f\n",a);
printf("The value of a= %u\n",&a);
printf("The value of b= %u\n",b);
printf("The value of a= %f\n",*b);
return 0;
}
13. Sum of elements of 1-d array using recursion.
#include <stdio.h>
#define MAX_SIZE 10
int sum(int arr[], int start, int len);
int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;
printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}
sumofarray = sum(arr, 0, N);
printf("Sum of array elements: %d", sumofarray);
return 0;
}
int sum(int arr[], int start, int len)
{
if(start >= len)
return 0;
return (arr[start] + sum(arr, start + 1, len));
}
14. Write a program to print the factorial of a number using recursion.
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

15. Write a program to perform insertion at the beginning in singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatBegin(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the beginning of a Singly Linked
List:\n");
printf("------------------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
NodeInsertatBegin(num);
printf("\n Data after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatBegin(int num)
{
struct node *fnNode;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = stnode;
stnode = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
16. Write a program to perform insertion at the end in singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void createNodeList(int n);
void NodeInsertatEnd(int num);
void displayList();
int main()
{
int n,num;
printf("\n\n Linked List : Insert a new node at the end of a Singly Linked List :\n");
printf("-------------------------------------------------------------------------\n");
printf(" Input the number of nodes : ");
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
printf("\n Input data to insert at the end of the list : ");
scanf("%d", &num);
NodeInsertatEnd(num);
printf("\n Data, after inserted in the list are : \n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode-> num = num;
stnode-> nextptr = NULL;
tmp = stnode;
for(i=2; i<=n; i++)
{
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
break;
}
else
{
printf(" Input data for node %d : ", i);
scanf(" %d", &num);
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
}
}
}
}
void NodeInsertatEnd(int num)
{
struct node *fnNode, *tmp;
fnNode = (struct node*)malloc(sizeof(struct node));
if(fnNode == NULL)
{
printf(" Memory can not be allocated.");
}
else
{
fnNode->num = num;
fnNode->nextptr = NULL;
tmp = stnode;
while(tmp->nextptr != NULL)
tmp = tmp->nextptr;
tmp->nextptr = fnNode;
}
}
void displayList()
{
struct node *tmp;
if(stnode == NULL)
{
printf(" No data found in the empty list.");
}
else
{
tmp = stnode;
while(tmp != NULL)
{
printf(" Data = %d\n", tmp->num);
tmp = tmp->nextptr;
}
}
}
17. Write a program to perform insertion at specific location singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void insertNodeAtMiddle(int data, int position);
void displayList();
int main()
{
int n, data, position;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("nEnter data to insert at middle of the list: ");
scanf("%d", &data);
printf("Enter the position to insert new node: " );
scanf("%d", &position);
insertNodeAtMiddle(data, position);
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void insertNodeAtMiddle(int data, int position)
{
int i;
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data;
newNode->next = NULL;
temp = head;
for(i=2; i<=position-1; i++)
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
newNode->next = temp->next;
temp->next = newNode;
printf("DATA INSERTED SUCCESSFULLY\n");
}
else
{
printf("UNABLE TO INSERT DATA AT THE GIVEN POSITION\n");
}
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
18. Write a program to perform deletion of node at the beginning.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void deleteFirstNode();
void displayList();
int main()
{
int n, choice;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nData in the list \n");
displayList();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);
if(choice == 1)
deleteFirstNode();
printf("\nData in the list \n");
displayList();
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of node 1: ");
scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
}
else
{
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}
19. Write a program to perform deletion of node at the end.
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void addNode(int data) {
struct node newNode = (struct node)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
tail = newNode;
}
else{
tail->next = newNode;
tail = newNode;
}
}
void deleteFromEnd() {
if(head == NULL) {
printf("List is empty \n");
return;
}
else {
if(head != tail ) {
struct node *current = head;
while(current->next != tail) {
current = current->next;
}
tail = current;
tail->next = NULL;
}
else {
head = tail = NULL;
}
}
}
void display() {
struct node *current = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
addNode(1);
addNode(2);
addNode(3);
addNode(4);

printf("Original List: \n");


display();

while(head != NULL) {
deleteFromEnd();
printf("Updated List: \n");
display();
}
return 0;
}

20. Write a program to perform push and pop operation in stack.


#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
while(1)
{
printf("\n** Stack Menu **");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
21. Write a program to perform insertion, deletion and traversing in a queue using
an array.
#include <stdio.h>
#define MAX 10
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
22. Write a program to perform insertion, deletion and traversing in a circular queue
using an array.
#include <stdio.h>
#define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item;
}
void deletion()
{
if (front == -1)
{
printf("Queue Underflow\n");
return;
}
printf("Element deleted from queue is : %d\n", cqueue_arr[front]);
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = front + 1;
}
}
void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if (front_pos <= rear_pos)
while (front_pos <= rear_pos)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
else
{
while (front_pos <= MAX - 1)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
printf("%d ", cqueue_arr[front_pos]);
front_pos++;
}
}
printf("n");
}
int main()
{
int choice, item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Input the element for insertion in queue : \n");
scanf("%d", &item);
insert(item);
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
} while (choice != 4);
return 0;
}
23. Write a program to perform linear search and binary search.
(a) Linear Search
#include <stdio.h>
int main()
{
int array[10], 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; }
(b) Binary Search
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[10];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
24. Write a program to perform bubble sort and selection sort.
(a) Bubble Sort
#include <stdio.h>
int main()
{
int array[10], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

(b) Selection Sort


#include<stdio.h>
int main(){
int i, j, count, temp, number[10];
printf("Enter number of elements\n ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(number[i]>number[j]){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

25. Write a program to perform insertion sort.


#include <stdio.h>
int main()
{
int n, i, j, temp;
int arr[10];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}

You might also like