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

DSA File

Uploaded by

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

DSA File

Uploaded by

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

Echelon Institute of Technology

Faridabad

Practical File Of MATLAB FILE


Name : MOHAMMAD JAVED

Roll No. : 23CSEAIMl026

Branch : B.Tech CSE AIML

Section : D
Index
S.No Program/AIM Page No.
1. Write a program to find max number and sum using Array

2. Write a program to show address of pointer and array.

3. Write a program to make pointer structure.

4. Write a program to implement linear search.

5. Write a program to create circular linked list.

6. Write a program to make structure array.

7. Write a program to find the address of the pointer.

8. Write a program to implement push pop operation in stack.

9. Write a program to implement push pop operation in stack using


linked list.

10. Write a program to implement insertion and deletion operation in


Queue.

11. Write a program to implement Enqueue and Dequeue operation


in Queue using linked list.

12. Write a program to implement Enqueue and Dequeue operation


in circular Queue .

13. Write a program to implement :


1. Bubble Sort 2. Selection Sort 3. Insertion Sort
4. Heap Sort 5. Merge Sort 6. Quick Sort
7. Radix Sort
Program No. 1
Aim: Write a program to find max number and sum using Array.
#include <stdio.h>
int main() {
int arr[5], i, max, sum = 0;
for(i=0; i<5; i++)
{
printf("Enter a number: ");
scanf("%d", &arr[i]);
sum += arr[i]; //sum = sum + arr[i];
}
max = arr[0];
printf("\n Entered numbers are: ");
for(i=0; i<5; i++)
{
printf("%d ", arr[i]);
if(arr[i] > max)
max = arr[i];
}
printf("\n The sum is %d",sum);
printf("\n The greatest number is %d",max);
return 0;
}
Program No. 2
Aim: Write a program to show address of pointer and array.
#include <stdio.h>
int main()
{
int i, arr[5] = {10, 20, 30, 40, 50};
//int *ptr = &arr[0];
int *ptr = arr;
printf("\n Address of array is %u", arr);
printf("\n Address of *ptr is %u", ptr);
/*for (i=0; i < 5; i++)
{
printf("\n arr[%d] is %d", i, arr[i]);
printf("\n ptr[%d] is %d", i, ptr[i]);
}*/
for (i=0; i < 5; i++)
{
printf("\n ptr[%d] is %d", i, *(ptr+i));
//printf("\n ptr[%d] is %d", i, *ptr);
//ptr++;
}
printf("\n ptr[%d] is %d", i, *ptr);
return 0;}
Program No. 3
Aim: Write a program to make pointer structure.
#include <stdio.h>
#include<stdlib.h>
struct student
{
char name[40];
int age;
};
int main()
{
struct student stud1, *ptr;
/*printf("\n Enter your name: ");
scanf("%[^\n]s", stud1.name);
printf("\n Enter your age: ");
scanf("%d", &stud1.age);
printf("\n Name: %s", stud1.name);
printf("\n Age: %d", stud1.age);
ptr = &stud1;*/

ptr = malloc(1 * sizeof(struct student));


printf("\n Enter your name: ");
scanf("%[^\n]s", ptr->name);
printf("\n Enter your age: ");
scanf("%d", &ptr->age);
printf("\n Name: %s", ptr->name);
printf("\n Age: %d", ptr->age);
return 0;
}
Program No. 4
Aim: Write a program to implement linear search.
#include <stdio.h>
#define SIZE 5
int main() {
int arr[SIZE], i, key;
for(i=0; i<SIZE; i++)
{
printf("Enter a number: ");
scanf("%d", &arr[i]);
}
printf("\n Entered numbers are: ");
for(i=0; i<SIZE; i++)
{
printf("%d ", arr[i]);
}
printf("\n Enter Number to be searched: ");
scanf("%d", &key);
for(i=0; i<SIZE; i++)
{
if (key == arr[i])
{
printf("\n Element exists");
break;
}
}
if (i == SIZE)
{
printf("\n Element does NOT exists");
}
return 0;
}
Program No. 5
Aim: Write a program to create circular linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *link;
}*head
void create();
void display();
int countNodes();
void insBeg(int ele);
void insEnd(int ele);
void insAny(int ele, int pos);
void delBeg();
void delEnd();
void delAny(int pos);
int main()
{
int choice, ele, pos;
create();
do
{
printf("\n ****MENU for Linked List***** ");
printf("\n 1:Display");
printf("\n 2:Count Nodes");
printf("\n 3:Insert Beginning");
printf("\n 4:Insert End");
printf("\n 5:Insert Any Location");
printf("\n 6:Delete Beginning");
printf("\n 7:Delete End");
printf("\n 8:Delete Any Location");
printf("\n 0:Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
printf("Exiting .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
printf(" No of nodes = %d \n", cnt);
break;
case 3:
printf("Input element to insert: ");
scanf("%d", &ele);
insBeg(ele);
break;
case 4:
printf("Input element to insert: ");
scanf("%d", &ele);
insEnd(ele);
break;
case 5:
printf("Input element to insert: ");
scanf("%d", &ele);
printf("Input position to insert: ");
scanf("%d", &pos);
insAny(ele, pos);
break;
case 6:
delBeg();
break;
case 7: delEnd();
break;
case 8:
printf("Input position to delete: ");
scanf("%d", &pos);
delAny(pos);
break;
default:
printf("Wrong choice");
}
}while(choice!=0);
}
void create()
{
//Adding first Node
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
printf("\n Input info: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
head = temp;
printf("\n Do you want to add more Nodes(1/0): ");
scanf("%d",&ch);
while(ch==1)
{
newNode = malloc(sizeof(struct Node));
printf("\n Input info: ");
scanf("%d",&info);
newNode->info = info;
newNode->link = NULL;
temp->link = newNode;
temp = newNode;
printf("\nDo you want to add more Nodes(1/0): ");
scanf("%d",&ch);
}
}
void display()
{
printf("\n Linked list is ");
printf("\n head=%u\n", head);
struct Node *temp = head;
while(temp!=NULL)
{
printf(" %d and Next Address: %u\n ", temp->info, temp->link);
//printf(" %d \n", temp->info);
temp = temp->link;
}
}
int countNodes()
{
int count = 0;
struct Node *temp = head;
while(temp!=NULL)
{
count = count + 1;
temp = temp->link;
}
return count;
}
void insBeg(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;

if(head == NULL)
head = temp;
else
{
temp->link = head;
head = temp;
}
printf("\n Element Inserted at Beginning");
display();
}
void insEnd(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;

if(head == NULL)
head = temp;
else
{
struct Node *last = head;
while(last->link != NULL)
last=last->link;
last->link=temp;
}
printf("\n Element Inserted at END");
display();
}
void insAny(int ele, int pos)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
int count=countNodes();
if(pos>=1 && pos<=count+1)
{
if(pos==1)
insBeg(ele);
else if(pos==count+1)
insEnd(ele);
else
{
struct Node *prev = head;
int cnt=1;
while(cnt<pos-1)
{
cnt++;
prev = prev->link;
}
temp->link = prev->link;
prev->link = temp;
}
printf("\n Element Inserted at position: %d", pos);
display();
}
else
printf("\n Invalid position of insertion \n");
}
void delBeg()
{
if(head==NULL)
{
printf("\n Underflow \n");
}
else
{
struct Node *temp = head;
head=head->link;
free(temp);
printf("\n Element Deleted from Beginning");
display();
}
}
void delEnd()
{
if(head==NULL)
{
printf("\n Underflow \n");
return;
}
if(head->link==NULL)
delBeg();
else
{
struct Node *temp = head;
// Move to last but one node
while(temp->link->link != NULL)
{
temp=temp->link;
}
free(temp->link);
temp->link=NULL;
printf("\n Element Deleted from END");
display();
}
}
void delAny(int pos)
{
int count;
if(head==NULL)
{
printf("\n Underflow \n");
return;
}
count=countNodes();

if(pos >= 1 && pos <= count)


{
if(pos==1)
{
delBeg();
}
else if(pos==count)
{
delEnd();
}
else
{
struct Node *del, *temp = head;
int cnt = 1;
// Move to one node before node to be deleted
while(cnt<pos-1)
{
cnt++;
temp=temp->link;
}
del = temp->link;
temp->link = temp->link->link;
free (del);
printf("\n Element Deleted from Position: %d", pos);
display();
}
}
else
printf("\n Invalid position of deletion \n");
}
Program No. 6
Aim: Write a program to make structure array.
#include <stdio.h>
#define SIZE 3
int main()
{
int i;
struct student
{
char name[40];
int age;
float per;
};
struct student std[SIZE];
for(i = 0; i < SIZE; i++)
{
printf("Enter Name: ");
// To handle the newline character that might be in the buffer
getchar(); // Clear the buffer
scanf("%[^\n]s", std[i].name);
printf("Enter Age: ");
scanf("%d", &std[i].age);
printf("Enter Percentage: ");
scanf("%f", &std[i].per);
}
printf("Entered Data is: \n");
for(i = 0; i < SIZE; i++)
{
printf("Name: %s\n", std[i].name);
printf("Age: %d\n", std[i].age);
printf("Percentage: %.2f\n", std[i].per); // Format percentage to 2 decimal
places
}
return 0;
}
Program No. 7
Aim: Write a program to find the address of the pointer.
#include <stdio.h>
int main()
{
int a = 10;
int b = 100;
int *ptr = &a;

printf("\n Address of a is %u", &a);


printf("\n Address of b is %u", &b);
printf("\n Address of *ptr is %u", ptr);
printf("\n The value of a is %d", a);
printf("\n The value of b is %d", b);
printf("\n The value of *ptr is %d", *ptr);
a = a+ 10;
printf("\n The value of a is %d", a);
printf("\n The value of b is %d", b);
printf("\n The value of *ptr is %d", *ptr);
ptr = &b;
printf("\n Address of a is %u", &a);
printf("\n Address of b is %u", &b);
printf("\n Address of *ptr is %u", ptr);
*ptr = *ptr - 5;
printf("\n The value of a is %d", a);
printf("\n The value of b is %d", b);
printf("\n The value of *ptr is %d", *ptr);

return 0;
}
Program No. 8
Aim: Write a program to implement push pop operation in stack.
#include <stdio.h>
#define SIZE 5
int Top = -1;
void display(int Stack[]);
void push (int Stack[], int data);
void pop (int Stack[]);
int main()
{
int Stack[SIZE], data, choice;
do
{
printf("\n Select an option from below: ");
printf("\n 1. Push into Stack ");
printf("\n 2. Pop from Stack ");
printf("\n 3. Display Stack ");
printf("\n 4. Exit ");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n Input Element: ");
scanf("%d", &data);
push(Stack, data);
break;
case 2:
pop(Stack);
break;
case 3:
display(Stack);
break;
case 4:
break;
default:
printf("\n Invalid Choice");
}
}while(choice!=4);

return 0;
}
void display(int Stack[])
{
int i;
if (Top == -1)
printf("\n Stack is empty\n");
else
{
printf("\n Elements in Stack are: ");
for(i=Top; i>=0; i--)
{
printf("\t%d", Stack[i]);
}
printf("\n");
}
}
void push(int Stack[], int data)
{
if (Top == SIZE-1)
printf("\n Stack OVERFLOW\n");
else
{
Top++;
Stack[Top] = data;
printf(" Element Pushed: %d\n", Stack[Top]);
}
display(Stack);
}
void pop (int Stack[])
{
if (Top == -1)
printf("\n Stack UNDERFLOW\n");
else
{
printf(" Element Popped: %d\n", Stack[Top]);
Top--;
}
display(Stack);
}
Program No. 9
Aim: Write a program to implement push pop operation in stack using
linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *link;
}*top;
void create();
void display();
int countNodes();
void push(int ele);
void pop();
int main()
{
int choice, ele, pos;
create();
do
{
printf("\n ****MENU for Dynamic Stack***** ");
printf("\n 1:Display");
printf("\n 2:Count Nodes");
printf("\n 3:Push");
printf("\n 4:Pop");
printf("\n 0:Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
printf("Exiting .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
printf(" No of nodes = %d \n", cnt);
break;
case 3:
printf("Input element to insert: ");
scanf("%d", &ele);
push(ele);
break;
case 4:
pop();
break;
default:
printf("Wrong choice");
}
}while(choice!=0);
}

void create()
{
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
printf("\n Input info for First Node: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
top = temp;
}
void display()
{
printf("\n Linked list is ");
printf("\n top=%u\n", top);
struct Node *temp = top;
while(temp!=NULL)
{
printf(" %d and Next Address: %u\n ", temp->info, temp->link);
temp = temp->link;
}
}
int countNodes()
{
int count = 0;
struct Node *temp = top;
while(temp!=NULL)
{
count = count + 1;
temp = temp->link;
}
return count;
}
void push(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
if(top == NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
printf("\n Element Inserted at Beginning");
display();
}
void pop()
{
if(top==NULL)
{
printf("\n Underflow \n");
}
else
{
struct Node *temp = top;
top=top->link;
free(temp);
printf("\n Element Deleted from Beginning");
display();
}
}
Program No. 10
Aim: Write a program to implement insertion and deletion operation in
Queue.
#include <stdio.h>
#define SIZE 5
int Queue[SIZE];
int Front = -1;
int Rear = -1;
void display();
void enqueue (int data);
void dequeue ();
int main()
{
int data, choice;
do
{
printf("\n Select an option from below: ");
printf("\n 1. Insert into Queue ");
printf("\n 2. Delete from Queue ");
printf("\n 3. Display Queue ");
printf("\n 4. Exit ");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n Input Element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\n Invalid Choice");
}
}while(choice!=4);
return 0;
}
void display()
{
int i;
if (Front == -1)
printf("\n Queue is empty\n");
else
{
printf("\n Elements in Queue are: ");
for(i=Front; i<=Rear; i++)
{
printf("\t%d", Queue[i]);
}
printf("\n");
}
}
void enqueue(int data)
{
if (Rear == SIZE-1)
printf("\n Queue OVERFLOW\n");
else
{
if (Front == -1)
Front++;
Rear++;
Queue[Rear] = data;
printf(" Element Enqueued: %d\n", Queue[Rear]);
}
display();
}
void dequeue ()
{
if (Front == -1)
printf("\n Queue UNDERFLOW\n");
else
{
printf(" Element Removed: %d\n", Queue[Front]);
Front++;
if (Front > Rear)
Front = Rear = -1;
}
display();
}
Program No. 11

Aim: Write a program to implement Enqueue and Dequeue operation in


Queue using linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *link;
}*front, *rear;
void create();
void display();
int countNodes();
void Enqueue(int ele);
void Dequeue();
int main()
{
int choice, ele, pos;
create();
do
{
printf("\n ****MENU for Dynamic Queue ***** ");
printf("\n 1:Display");
printf("\n 2:Count Nodes");
printf("\n 3:Enqueue");
printf("\n 4:Dequeue");
printf("\n 0:Exit");
printf("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
printf("Exiting .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
printf(" No of nodes = %d \n", cnt);
break;
case 3:
printf("Input element to insert: ");
scanf("%d", &ele);
Enqueue(ele);
break;
case 4:
Dequeue();
break;
default:
printf("Wrong choice");
}
}while(choice!=0);
}
void create()
{
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
printf("\n Input info: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
front = temp;
rear = temp;
printf("\n Do you want to add more Nodes(1/0): ");
scanf("%d",&ch);
while(ch==1)
{
newNode = malloc(sizeof(struct Node));
printf("\n Input info: ");
scanf("%d",&info);
newNode->info = info;
newNode->link = NULL;
rear->link = newNode;
rear = newNode;
printf("\nDo you want to add more Nodes(1/0): ");
scanf("%d",&ch);
}
}
void display()
{
printf("\n Linked list is ");
printf("\n front=%u\n", front);
struct Node *temp = front;
while(temp!=NULL)
{
printf(" %d and Next Address: %u\n ", temp->info, temp->link);
//printf(" %d \n", temp->info);
temp = temp->link;
}
}
int countNodes()
{
int count = 0;
struct Node *temp = front;
while(temp!=NULL)
{
count = count + 1;
temp = temp->link;
}
return count;
}
void Enqueue(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
if(front == NULL)
{
front = temp;
rear = temp;
}
else
{
rear->link=temp;
}
printf("\n Element Inserted at END");
display();
}
void Dequeue()
{
if(front==NULL)
{
printf("\n Underflow \n");
}
else
{
struct Node *temp = front;
front=front->link;
free(temp);
printf("\n Element Deleted from Beginning");
display();
}
}
Program No. 12
Aim: Write a program to implement Enqueue and Dequeue operation in
circular Queue .
#include <stdio.h>
#define SIZE 5
int CQueue[SIZE];
int Front = -1;
int Rear = -1;
void display();
void enqueue(int data);
void dequeue();
int main() {
int data, choice;
do {
printf("\nSelect an option from below: ");
printf("\n1. Insert into Circular Queue");
printf("\n2. Delete from Circular Queue");
printf("\n3. Display Circular Queue");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nInput Element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\nInvalid Choice");
}
} while (choice != 4);
return 0;
}
void display() {
int i;
if (Front == -1)
printf("\nQueue is empty\n");
else {
printf("\nElements in Queue are: ");
if (Front <= Rear) {
for (i = Front; i <= Rear; i++) {
printf("\t%d", CQueue[i]);
}
printf("\n");
} else {
for (i = Front; i < SIZE; i++) {
printf("\t%d", CQueue[i]);
}
for (i = 0; i <= Rear; i++) {
printf("\t%d", CQueue[i]);
}
printf("\n");
}
}
}
void enqueue(int data) {
if ((Rear + 1) % SIZE == Front) {
printf("\nQueue OVERFLOW\n");
} else {
if (Front == -1) // If the queue is empty
Front = 0; // Initialize Front to 0
Rear = (Rear + 1) % SIZE; // Circular increment
CQueue[Rear] = data;
printf("Element Enqueued: %d\n", data);
}
display();
}
void dequeue() {
if (Front == -1) {
printf("\nQueue UNDERFLOW\n");
} else {
printf("Element Removed: %d\n", CQueue[Front]);
if (Front == Rear) { // If there is only one element
Front = Rear = -1; // Reset the queue
} else {
Front = (Front + 1) % SIZE; // Circular increment
}
}
display();
}
Program No. 13
Aim: Write a program to implement :
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Heap Sort
5. Merge Sort
6. Quick Sort
7. Radix Sort
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define SIZE 8000
#define LIMIT 88888
void selection_sort(int arr[], int size)
{
int counter1, counter2, temp;
for(counter1 = 0; counter1 < size - 1; counter1++)
{
for(counter2 = counter1 + 1; counter2 < size; counter2++)
{
if (arr[counter1] > arr[counter2])
{
temp = arr[counter2];
arr[counter2] = arr[counter1];
arr[counter1] = temp;
}
}
}
}
void bubble_sort(int arr[], int size)
{
int counter1, counter2, flag, temp;
for(counter1 = size - 1; counter1 > 0; counter1--)
{
flag = 0;
for(counter2 = 0; counter2 < counter1; counter2++)
{
if (arr[counter2] > arr[counter2 + 1])
{
temp = arr[counter2];
arr[counter2] = arr[counter2 + 1];
arr[counter2 + 1] = temp;
flag = 1;
}
}
if (flag == 0)
break;
}
}
void insertion_sort(int arr[], int size)
{
int counter1, counter2, key;
for(counter1 = 1; counter1 < size; counter1++)
{
key = arr[counter1];
// Move elements of arr[0..i-1], that are greater than key, to one position
ahead of their current position
for(counter2 = counter1 - 1; counter2 >= 0 && arr[counter2] > key;
counter2--)
{
arr[counter2 + 1] = arr[counter2];
}
arr[counter2 + 1] = key;
}
}
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; // Index of first subarray
j = 0; // Index of second subarray
k = l; // Index of merged subarray
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 merge_sort(int arr[], int left, int right)
{
int mid;
if (left < right)
{
mid = left+(right-left)/2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
int j;

for (j = low; j <= high- 1; j++)


{
if (arr[j] <= pivot)
{
i++; // increment index of greater element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quick_sort(int arr[], int low, int high)
{
if (low < high)
{
int pi;
pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}
void countSort(int arr[], int n, int exp)
{
int output[n]; // output array
int i, count[10] = {0};
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%10 ]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radix_sort(int arr[], int size)
{
int exp, counter, max;
max = arr[0];
for (counter = 1; counter < size; counter++)
max = arr[counter] > max ? arr[counter] : max;
for (exp = 1; max/exp > 0; exp *= 10)
countSort(arr, size, exp);
}
void heapify(int arr[], int size, int index)
{
int root, left_child, right_child, temp;
root = index;
left_child = 2 * index + 1;
right_child = 2 * index + 2;
if (left_child < size && arr[left_child] > arr[root])
if (right_child < size && arr[right_child] > arr[root])
root = right_child;
if (root != index)
{
temp = arr[index];
arr[index] = arr[root];
arr[root] = temp;
heapify(arr, size, root);
}
}
void heap_sort(int arr[], int size)
{
int counter, temp;
for (counter = size / 2 - 1; counter >= 0; counter--)
heapify(arr, size, counter);
for (counter = size - 1; counter >= 0; counter--)
{
temp = arr[0];
arr[0] = arr[counter];
arr[counter] = temp;
heapify(arr, counter, 0);
}
}
void display(int arr[], int size, double exec_time)
{
int counter;
printf(" The Sorted Array is :\n");
for (counter = 0; counter < size; counter++)
printf("\t%d", arr[counter]);
printf("\n\n Total Time taken for integer sorting is %lf sec", exec_time);
printf("\n\n");
}
void display_original(int arr[], int size)
{
int counter;
printf(" The Original Array is :\n");
for (counter = 0; counter < size; counter++)
printf("\t%d", arr[counter]);
printf("\n\n");
}
int main(int argc, char *argv[])
{
int size, choice, counter, counter2, flag, arr[SIZE], copy[SIZE];
char temp[LIMIT];
clock_t start, end;
double exec_time;
do
{
flag = 0;
printf("\n This software sorts integers comparing different sorting
techniques");
printf("\n Enter the number of integers to be sorted : ");
scanf("%s", temp);
for(counter = 0; counter < strlen(temp); counter++)
{
if (temp[counter] < 48 || temp[counter] > 57)
{
printf("\n Invalid Data Type Entered .. Please input valid data type ... ");
flag = 1;
break;
}
}
size = atoi(temp);
}while(flag == 1);
for (counter = 0; counter < size; counter++)
{
do
{
flag = 0;
printf("\n Enter an integer : ");
scanf("%s", temp);
for(counter2 = 0; counter2 < strlen(temp); counter2++)
{
if (temp[counter2] < 48 || temp[counter2] > 57)
{
printf("\n Invalid Data Type Entered .. Please input valid data type ... ");
flag = 1;
break;
}
}
arr[counter] = atoi(temp);
copy[counter] = arr[counter];
}while(flag == 1);
}
do
{
printf("\n Enter the Sorting Algorithm to be used : ");
printf("\n 1. Bubble Sort");
printf("\n 2. Selection Sort");
printf("\n 3. Insertion Sort");
printf("\n 4. Heap Sort");
printf("\n 5. Merge Sort");
printf("\n 6. Quick Sort");
printf("\n 7. Radix Sort");
printf("\n 0. Exit");
do
{
flag = 0;
printf("\n Enter your choice(0-7) : ");
scanf("%s", temp);
for(counter = 0; counter < strlen(temp); counter++)
{
if (temp[counter] < 48 || temp[counter] > 57)
{
printf("\n Invalid Data Type Entered .. Please input valid data type ... ");
flag = 1;
break;
}
}
choice = atoi(temp);
}while(flag == 1);
for(counter = 0; counter < size; counter++)
arr[counter] = copy[counter];
switch(choice)
{
case 0:
break;
case 1:
//Applying Bubble Sort to sort the data
display_original(arr, size);
printf("\n Executing Bubble Sort \n");
start = clock();
bubble_sort(arr, size);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);

break;
case 2:
display_original(arr, size);
printf("\n Executing Selection Sort \n");
start = clock();
selection_sort(arr, size);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;

case 3:
display_original(arr, size);
printf("\n Executing Insertion Sort \n");
start = clock();
insertion_sort(arr, size);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;

case 4:
display_original(arr, size);
printf("\n Executing Heap Sort \n");
start = clock();
heap_sort(arr, size);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;

case 5:
display_original(arr, size);
printf("\n Executing Merge Sort \n");
start = clock();
merge_sort(arr, 0, size - 1);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;

case 6:
display_original(arr, size);
printf("\n Executing Quick Sort \n");
start = clock();
quick_sort(arr, 0, size - 1);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;
case 7:
display_original(arr, size);
printf("\n Executing Radix Sort \n");
start = clock();
radix_sort(arr, size);
end = clock();
exec_time = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_time);
break;
default:
printf("\n\n Invalid Input \n\n");
}
}while(choice !=0);
return 0;
}

You might also like