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

DSA_Program_file1

Uploaded by

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

DSA_Program_file1

Uploaded by

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

Echelon Ins tute of Technology

Faridabad

Prac cal File Of Data Structures and Algorithms


Name : Mohammad Javed

Roll No. : 23CSEAIML030

Branch : B.Tech CSE AIML

Sec on : 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 opera on in stack.

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


linked list.

10. Write a program to implement inser on and dele on opera on in


Queue.

11. Write a program to implement Enqueue and Dequeue opera on


in Queue using linked list.

12. Write a program to implement Enqueue and Dequeue opera on


in circular Queue .

13. Write a program to implement :


1. Bubble Sort 2. Selec on Sort 3. Inser on 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++)
{
prin ("Enter a number: ");
scanf("%d", &arr[i]);
sum += arr[i]; //sum = sum + arr[i];
}
max = arr[0];
prin ("\n Entered numbers are: ");
for(i=0; i<5; i++)
{
prin ("%d ", arr[i]);
if(arr[i] > max)
max = arr[i];
}
prin ("\n The sum is %d",sum);
prin ("\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;
prin ("\n Address of array is %u", arr);
prin ("\n Address of *ptr is %u", ptr);
/*for (i=0; i < 5; i++)
{
prin ("\n arr[%d] is %d", i, arr[i]);
prin ("\n ptr[%d] is %d", i, ptr[i]);
}*/
for (i=0; i < 5; i++)
{
prin ("\n ptr[%d] is %d", i, *(ptr+i));
//prin ("\n ptr[%d] is %d", i, *ptr);
//ptr++;
}
prin ("\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;
/*prin ("\n Enter your name: ");
scanf("%[^\n]s", stud1.name);
prin ("\n Enter your age: ");
scanf("%d", &stud1.age);
prin ("\n Name: %s", stud1.name);
prin ("\n Age: %d", stud1.age);
ptr = &stud1;*/

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


prin ("\n Enter your name: ");
scanf("%[^\n]s", ptr->name);
prin ("\n Enter your age: ");
scanf("%d", &ptr->age);
prin ("\n Name: %s", ptr->name);
prin ("\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++)
{
prin ("Enter a number: ");
scanf("%d", &arr[i]);
}
prin ("\n Entered numbers are: ");
for(i=0; i<SIZE; i++)
{
prin ("%d ", arr[i]);
}
prin ("\n Enter Number to be searched: ");
scanf("%d", &key);
for(i=0; i<SIZE; i++)
{
if (key == arr[i])
{
prin ("\n Element exists");
break;
}
}
if (i == SIZE)
{
prin ("\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
{
prin ("\n ****MENU for Linked List***** ");
prin ("\n 1:Display");
prin ("\n 2:Count Nodes");
prin ("\n 3:Insert Beginning");
prin ("\n 4:Insert End");
prin ("\n 5:Insert Any Loca on");
prin ("\n 6:Delete Beginning");
prin ("\n 7:Delete End");
prin ("\n 8:Delete Any Loca on");
prin ("\n 0:Exit");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
prin ("Exi ng .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
prin (" No of nodes = %d \n", cnt);
break;
case 3:
prin ("Input element to insert: ");
scanf("%d", &ele);
insBeg(ele);
break;
case 4:
prin ("Input element to insert: ");
scanf("%d", &ele);
insEnd(ele);
break;
case 5:
prin ("Input element to insert: ");
scanf("%d", &ele);
prin ("Input posi on to insert: ");
scanf("%d", &pos);
insAny(ele, pos);
break;
case 6:
delBeg();
break;
case 7: delEnd();
break;
case 8:
prin ("Input posi on to delete: ");
scanf("%d", &pos);
delAny(pos);
break;
default:
prin ("Wrong choice");
}
}while(choice!=0);
}
void create()
{
//Adding first Node
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
prin ("\n Input info: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
head = temp;
prin ("\n Do you want to add more Nodes(1/0): ");
scanf("%d",&ch);
while(ch==1)
{
newNode = malloc(sizeof(struct Node));
prin ("\n Input info: ");
scanf("%d",&info);
newNode->info = info;
newNode->link = NULL;
temp->link = newNode;
temp = newNode;
prin ("\nDo you want to add more Nodes(1/0): ");
scanf("%d",&ch);
}
}
void display()
{
prin ("\n Linked list is ");
prin ("\n head=%u\n", head);
struct Node *temp = head;
while(temp!=NULL)
{
prin (" %d and Next Address: %u\n ", temp->info, temp->link);
//prin (" %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;
}
prin ("\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;
}
prin ("\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;
}
prin ("\n Element Inserted at posi on: %d", pos);
display();
}
else
prin ("\n Invalid posi on of inser on \n");
}
void delBeg()
{
if(head==NULL)
{
prin ("\n Underflow \n");
}
else
{
struct Node *temp = head;
head=head->link;
free(temp);
prin ("\n Element Deleted from Beginning");
display();
}
}
void delEnd()
{
if(head==NULL)
{
prin ("\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;
prin ("\n Element Deleted from END");
display();
}
}
void delAny(int pos)
{
int count;
if(head==NULL)
{
prin ("\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);
prin ("\n Element Deleted from Posi on: %d", pos);
display();
}
}
else
prin ("\n Invalid posi on of dele on \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++)
{
prin ("Enter Name: ");
// To handle the newline character that might be in the buffer
getchar(); // Clear the buffer
scanf("%[^\n]s", std[i].name);
prin ("Enter Age: ");
scanf("%d", &std[i].age);
prin ("Enter Percentage: ");
scanf("%f", &std[i].per);
}
prin ("Entered Data is: \n");
for(i = 0; i < SIZE; i++)
{
prin ("Name: %s\n", std[i].name);
prin ("Age: %d\n", std[i].age);
prin ("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;

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


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

return 0;
}
Program No. 8
Aim: Write a program to implement push pop opera on 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
{
prin ("\n Select an op on from below: ");
prin ("\n 1. Push into Stack ");
prin ("\n 2. Pop from Stack ");
prin ("\n 3. Display Stack ");
prin ("\n 4. Exit ");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
prin ("\n Input Element: ");
scanf("%d", &data);
push(Stack, data);
break;
case 2:
pop(Stack);
break;
case 3:
display(Stack);
break;
case 4:
break;
default:
prin ("\n Invalid Choice");
}
}while(choice!=4);

return 0;
}
void display(int Stack[])
{
int i;
if (Top == -1)
prin ("\n Stack is empty\n");
else
{
prin ("\n Elements in Stack are: ");
for(i=Top; i>=0; i--)
{
prin ("\t%d", Stack[i]);
}
prin ("\n");
}
}
void push(int Stack[], int data)
{
if (Top == SIZE-1)
prin ("\n Stack OVERFLOW\n");
else
{
Top++;
Stack[Top] = data;
prin (" Element Pushed: %d\n", Stack[Top]);
}
display(Stack);
}
void pop (int Stack[])
{
if (Top == -1)
prin ("\n Stack UNDERFLOW\n");
else
{
prin (" Element Popped: %d\n", Stack[Top]);
Top--;
}
display(Stack);
}
Program No. 9
Aim: Write a program to implement push pop opera on 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
{
prin ("\n ****MENU for Dynamic Stack***** ");
prin ("\n 1:Display");
prin ("\n 2:Count Nodes");
prin ("\n 3:Push");
prin ("\n 4:Pop");
prin ("\n 0:Exit");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
prin ("Exi ng .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
prin (" No of nodes = %d \n", cnt);
break;
case 3:
prin ("Input element to insert: ");
scanf("%d", &ele);
push(ele);
break;
case 4:
pop();
break;
default:
prin ("Wrong choice");
}
}while(choice!=0);
}

void create()
{
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
prin ("\n Input info for First Node: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
top = temp;
}
void display()
{
prin ("\n Linked list is ");
prin ("\n top=%u\n", top);
struct Node *temp = top;
while(temp!=NULL)
{
prin (" %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;
}
prin ("\n Element Inserted at Beginning");
display();
}
void pop()
{
if(top==NULL)
{
prin ("\n Underflow \n");
}
else
{
struct Node *temp = top;
top=top->link;
free(temp);
prin ("\n Element Deleted from Beginning");
display();
}
}
Program No. 10
Aim: Write a program to implement inser on and dele on opera on 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
{
prin ("\n Select an op on from below: ");
prin ("\n 1. Insert into Queue ");
prin ("\n 2. Delete from Queue ");
prin ("\n 3. Display Queue ");
prin ("\n 4. Exit ");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
prin ("\n Input Element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
default:
prin ("\n Invalid Choice");
}
}while(choice!=4);
return 0;
}
void display()
{
int i;
if (Front == -1)
prin ("\n Queue is empty\n");
else
{
prin ("\n Elements in Queue are: ");
for(i=Front; i<=Rear; i++)
{
prin ("\t%d", Queue[i]);
}
prin ("\n");
}
}
void enqueue(int data)
{
if (Rear == SIZE-1)
prin ("\n Queue OVERFLOW\n");
else
{
if (Front == -1)
Front++;
Rear++;
Queue[Rear] = data;
prin (" Element Enqueued: %d\n", Queue[Rear]);
}
display();
}
void dequeue ()
{
if (Front == -1)
prin ("\n Queue UNDERFLOW\n");
else
{
prin (" 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 opera on 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
{
prin ("\n ****MENU for Dynamic Queue ***** ");
prin ("\n 1:Display");
prin ("\n 2:Count Nodes");
prin ("\n 3:Enqueue");
prin ("\n 4:Dequeue");
prin ("\n 0:Exit");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
prin ("Exi ng .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
prin (" No of nodes = %d \n", cnt);
break;
case 3:
prin ("Input element to insert: ");
scanf("%d", &ele);
Enqueue(ele);
break;
case 4:
Dequeue();
break;
default:
prin ("Wrong choice");
}
}while(choice!=0);
}
void create()
{
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
prin ("\n Input info: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
front = temp;
rear = temp;
prin ("\n Do you want to add more Nodes(1/0): ");
scanf("%d",&ch);
while(ch==1)
{
newNode = malloc(sizeof(struct Node));
prin ("\n Input info: ");
scanf("%d",&info);
newNode->info = info;
newNode->link = NULL;
rear->link = newNode;
rear = newNode;
prin ("\nDo you want to add more Nodes(1/0): ");
scanf("%d",&ch);
}
}
void display()
{
prin ("\n Linked list is ");
prin ("\n front=%u\n", front);
struct Node *temp = front;
while(temp!=NULL)
{
prin (" %d and Next Address: %u\n ", temp->info, temp->link);
//prin (" %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;
}
prin ("\n Element Inserted at END");
display();
}
void Dequeue()
{
if(front==NULL)
{
prin ("\n Underflow \n");
}
else
{
struct Node *temp = front;
front=front->link;
free(temp);
prin ("\n Element Deleted from Beginning");
display();
}
}
Program No. 12
Aim: Write a program to implement Enqueue and Dequeue opera on 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 {
prin ("\nSelect an op on from below: ");
prin ("\n1. Insert into Circular Queue");
prin ("\n2. Delete from Circular Queue");
prin ("\n3. Display Circular Queue");
prin ("\n4. Exit");
prin ("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("\nInput Element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
default:
prin ("\nInvalid Choice");
}
} while (choice != 4);
return 0;
}
void display() {
int i;
if (Front == -1)
prin ("\nQueue is empty\n");
else {
prin ("\nElements in Queue are: ");
if (Front <= Rear) {
for (i = Front; i <= Rear; i++) {
prin ("\t%d", CQueue[i]);
}
prin ("\n");
} else {
for (i = Front; i < SIZE; i++) {
prin ("\t%d", CQueue[i]);
}
for (i = 0; i <= Rear; i++) {
prin ("\t%d", CQueue[i]);
}
prin ("\n");
}
}
}
void enqueue(int data) {
if ((Rear + 1) % SIZE == Front) {
prin ("\nQueue OVERFLOW\n");
} else {
if (Front == -1) // If the queue is empty
Front = 0; // Ini alize Front to 0
Rear = (Rear + 1) % SIZE; // Circular increment
CQueue[Rear] = data;
prin ("Element Enqueued: %d\n", data);
}
display();
}
void dequeue() {
if (Front == -1) {
prin ("\nQueue UNDERFLOW\n");
} else {
prin ("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. Selec on Sort
3. Inser on Sort
4. Heap Sort
5. Merge Sort
6. Quick Sort
7. Radix Sort
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include< me.h>
#define SIZE 8000
#define LIMIT 88888
void selec on_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 inser on_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 posi on
ahead of their current posi on
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 le , int right)
{
int mid;
if (le < right)
{
mid = le +(right-le )/2;
merge_sort(arr, le , mid);
merge_sort(arr, mid + 1, right);
merge(arr, le , mid, right);
}
}
void swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int par on (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 = par on(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, le _child, right_child, temp;
root = index;
le _child = 2 * index + 1;
right_child = 2 * index + 2;
if (le _child < size && arr[le _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_ me)
{
int counter;
prin (" The Sorted Array is :\n");
for (counter = 0; counter < size; counter++)
prin ("\t%d", arr[counter]);
prin ("\n\n Total Time taken for integer sor ng is %lf sec", exec_ me);
prin ("\n\n");
}
void display_original(int arr[], int size)
{
int counter;
prin (" The Original Array is :\n");
for (counter = 0; counter < size; counter++)
prin ("\t%d", arr[counter]);
prin ("\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_ me;
do
{
flag = 0;
prin ("\n This so ware sorts integers comparing different sor ng
techniques");
prin ("\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)
{
prin ("\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;
prin ("\n Enter an integer : ");
scanf("%s", temp);
for(counter2 = 0; counter2 < strlen(temp); counter2++)
{
if (temp[counter2] < 48 || temp[counter2] > 57)
{
prin ("\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
{
prin ("\n Enter the Sor ng Algorithm to be used : ");
prin ("\n 1. Bubble Sort");
prin ("\n 2. Selec on Sort");
prin ("\n 3. Inser on Sort");
prin ("\n 4. Heap Sort");
prin ("\n 5. Merge Sort");
prin ("\n 6. Quick Sort");
prin ("\n 7. Radix Sort");
prin ("\n 0. Exit");
do
{
flag = 0;
prin ("\n Enter your choice(0-7) : ");
scanf("%s", temp);
for(counter = 0; counter < strlen(temp); counter++)
{
if (temp[counter] < 48 || temp[counter] > 57)
{
prin ("\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);
prin ("\n Execu ng Bubble Sort \n");
start = clock();
bubble_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);

break;
case 2:
display_original(arr, size);
prin ("\n Execu ng Selec on Sort \n");
start = clock();
selec on_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;

case 3:
display_original(arr, size);
prin ("\n Execu ng Inser on Sort \n");
start = clock();
inser on_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;

case 4:
display_original(arr, size);
prin ("\n Execu ng Heap Sort \n");
start = clock();
heap_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;

case 5:
display_original(arr, size);
prin ("\n Execu ng Merge Sort \n");
start = clock();
merge_sort(arr, 0, size - 1);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;

case 6:
display_original(arr, size);
prin ("\n Execu ng Quick Sort \n");
start = clock();
quick_sort(arr, 0, size - 1);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;

case 7:
display_original(arr, size);
prin ("\n Execu ng Radix Sort \n");
start = clock();
radix_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
default:
prin ("\n\n Invalid Input \n\n");
}
}while(choice !=0);
return 0;
}

You might also like