Algo & Os
Algo & Os
AIM:
ALGORITHM:
Step 1: Start
Step 2: Initialize variables start_t and end_t using clock_t of time.h header file
Step 3: Initialize variables n, i, j, swap using integer data type
Step 4: Get the start time using clock() function and display it in the screen
Step 5: Get number of elements of the array and declare an array
Step 6: Get the elements from the user using for loop
Step 7: Using rand() function generate random elements and store it in the array using for loop
Step 8: Print the time taken for getting inputs using clock() function
Step 9: Using two for loops compare each and every element of the array
Step 10: If the element is greater than the other, swap their positions using 3rd variable swap
Step 11: If the element is not greater, move to the next element and continue Step 10
Step 12: Now, Print the sorted elements
Step 13: Print the time taken for sorting and displaying result using clock() function
Step 14: Convert the time taken to execute the program into seconds using (double)(clock() -
start_t)/CLOCKS_PER_SEC;
Step 15: Print the time taken to compile program in seconds(clock speed)
Step 16: Stop
PROGRAM:
#include <stdio.h>
#include <time.h>
int main()
{
clock_t start_t, end_t;
int n,j,swap,i;
}
}
}
printf("\nSorted elements...\n");
for(i=0;i<n;i++)
{
printf("%d\n",num[i]);
}
end_t=clock();
printf("Time after displaying output needed...,end_t=%ld\n",end_t);
OUTPUT:
RESULT:
Thus, program to find to calculate the clock speed of a program for different sizes of inputs was
executed and output was verified successfully.
AIM:
To write a C program to perform array operations like insertion, deletion, display, sorting and searching
using switch cases and do while loops.
ALGORITHM:
Step 1: Start
Step 2: Get the number of elements and array elements from the user
Step 3: Print the array before operation and show the list of operations to the user with
respective numbers
Step 4: Get the users choice if it is 1 go to Step 5, If 2 go to Step 8, If 3 go to Step 11, If 4 go to
step 12, If 5 go to Step 14, If 6 go to Step 17
Step 5: Get the element to be inserted from user and the location where the element to be inserted
Step 6: From last index move the element to (i+1)th index until the specified location is reached
Step 7: Now insert the element in that location and print the array
Step 8: Get the element to be deleted from user
Step 9: Compare the element with the elements in the array and move the element to be deleted
out of the array
Step 10: Print the modified array
Step 11: Do step 10
Step 12: Compare the array elements if it is greater than the other element swap the elements using for
loop
Step 13: Do it till the end of the array and go to Step 10
Step 14: Get the search element from the user
Step 15: Compare the elements in the array with the search element, if same print the respective
location of the element
Step 16: Else continue Step 15
Step 17: Get the location from the user
Step 18: Print the element in that location
Step 19: Ask the user whether he wants to continue or stop the operation
Step 20: Continue from Step 4 if user wants to continue or stop the loop
PROGRAM:
#include <stdio.h>
int main() {
int n, i, choice, go_on = 0, swap, count, in, loc, del, j, find;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter array elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nArray before operation: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
do {
printf("\nEnter the choice\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Display array\n");
printf("4. Sort array\n");
printf("5. Search by element\n");
printf("6. Search by location\n\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter an element to insert: ");
scanf("%d", &in);
printf("Enter location to insert: ");
scanf("%d", &loc);
loc--;
for (i = n - 1; i >= loc; i--) {
arr[i + 1] = arr[i];
}
arr[loc] = in;
n++;
break;
case 2:
printf("Enter an element to delete: ");
case 3:
printf("\nDisplaying array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 4:
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
printf("\nSorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
break;
case 5:
printf("Enter an element to search: ");
scanf("%d", &find);
count = -1;
for (i = 0; i < n; i++) {
if (arr[i] == find) {
count = i;
break;
}
}
if (count != -1) {
case 6:
printf("Enter a location: ");
scanf("%d", &loc);
if (loc >= 1 && loc <= n) {
printf("The element at location %d is: %d", loc, arr[loc - 1]);
} else {
printf("Invalid location.");
}
break;
default:
printf("Sorry! Please read the operations in the above list and give a valid choice.");
break;
}
printf("\nThank you!\n");
return 0;
}
OUTPUT:
Thus, program to find to perform array operations like insertion, deletion, display, sorting and
searching using switch cases and do while loops was executed and output was verified successfully.
AIM:
To save and delete an element and check whether the list is empty or not using dynamic implementation.
ALGORITHM:
Step 1: Start
Step 2: Create structure node with element and next pointer as its members
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6, If 2 do Step 7,8, If 3 do Step 9,10,11, If 4 do Step 12,13, If
5 do step 14,15, If 6 do step 16
Step 5: Create header for the single linked list by allocating memory to the node
Step 6: Assume the element in the header node to be zero, As it is the first node created the next pointer
is NULL
Step 7: Create a node and allocate memory to it
Step 8: Save the element entered by the user in the new node created and save the address of this node in
the next pointer of head C.
Step 9: Get the element to be deleted from the user and Check whether the list is empty or not
Step 10: If not empty, create a variable and save the address of the element to be deleted in it.
Step 11: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
typedef struct node *ptrtonode;
typedef ptrtonode List;
typedef ptrtonode Position;
struct node
{
int element;
Position next;
};
List L;
int main(void)
{
int choice, InsAfterWhich, data;
int x = 1;
Position P;
do
{
printf("1. CREATE HEADER\n2. INSERT NODE\n3. DELETE NODE\n4. DELETE LIST\n5.
DISPLAY LIST\n6. EXIT\n");
printf("Enter your option: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
L = CreateHeader();
break;
case 2:
if (L != NULL)
{
printf("Enter position after which the new node to be inserted: ");
scanf("%d", &InsAfterWhich);
P = Find(InsAfterWhich, L);
DisplayList(L);
break;
case 6:
printf("Do you want to exit (yes(0)/no(1)): ");
scanf("%d", &x);
break;
}
} while (x == 1);
return 0;
}
List CreateHeader()
{
List L;
L = (List)malloc(sizeof(struct node));
if (L == NULL)
{
printf("Memory is not allocated...\n");
return NULL;
}
L->element = 0;
L->next = NULL;
printf("Header created...\n");
return L;
}
printf("Element inserted...\n");
}
void DeleteList(List L)
{
Position P, temp;
P = L->next;
L->next = NULL;
while (P != NULL)
{
temp = P->next;
free(P);
P = temp;
}
printf("List deleted...\n");
}
void DisplayList(List L)
{
Position P = L->next;
printf("\n...LINKED LIST...\n");
while (P != NULL)
{
printf("%d->", P->element);
P = P->next;
}
printf("NULL\n");
}
OUTPUT:
Thus, program to find to save and delete an element and check whether the list is empty or not
using dynamic implementation was executed and output was verified successfully.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Create structure node with element and next, prev pointer as its members
Step 3: Get the choice to perform a specific operation from user.
Step 4: If it is 1 do Step 5,6,If 2 do Step 7,8,If 3 do Step 9,10,11,If 4 do Step 12,13,If 5 do Step 14.
Step 5: Create header for the double linked list by allocating memory to the node
Step 6: Assume the element in the header node to be zero, As it is the first node created the next pointers
value is NULL and previous pointer also points NULL
Step 7: Create a node and allocate memory to it
Step 8: Save the element entered by the user in the new node created and save the address of this node in
the next pointer of head and same address in previous pointer of new node
Step 9: Get the element to be deleted from the user and Check whether the list is empty or not
Step 10: If not empty, Create a variable and save the address of the element to be deleted in it
Step 11: Now take the address of the next element and save it in next pointer of the previous element of
the deleted element
Step 12: Display all the elements in the Linked List until the next pointer points NULL which means the
element to be printed is the last element
Step 13: And print the last element separately, Specify the first previous pointer and last next pointer as
NULL
Step 14: Ask the user whether to continue or stop, Take users choice and continue the program if 1 else
stop the program
Step 15: Stop
PROGRAM:
# include <stdio.h>
# include <malloc.h>
# include <conio.h>
typedef struct node *ptrtonode;
K.S.AJAY VARSHAAN (71812253006) 17
typedef ptrtonode List;
typedef ptrtonode Position;
List CreateHeader();
int IsEmpty(List L);
int IsLast(Position P,List L);
Position Find(int data,List L);
Position FindPrevious(int data,List L);
void Insert(int data,List L,Position P);
void Delete(int data,List L);
void DisplayList(List L);
struct node
{
int element;
Position next;
Position prev;
};
List L;
List head=NULL;
void main()
{
int choice,InsAfterWhich,data; int x=1;
Position P;
// clrscr();
do
{
printf(" 1. CREATE HEADER \n 2. INSERT NODE \n 3. DELETE NODE \n 4. DISPLAY LIST \n 5.
EXIT \n");
printf("Enter your option : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
L=CreateHeader();
break;
case 2:
if(L!=NULL)
{
printf("Enter position after which the new node to be inserted : ");
scanf("%d",&InsAfterWhich);
P=Find(InsAfterWhich,L);
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
Insert(data,L,P); break;
}
else
{
AIM:
ALGORITHM:
Step 1: Start
Step 2: Create a stack array and declare top as -1
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a stack is empty
Step 6: If empty push the element entered by the user and increment the top value
Step 7: If not print Overflow
Step 8: Check whether the stack array is empty, If so print Underflow
Step 9: If not do step 10
Step 10: To pop element reduce the value of top
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop
PROGRAM:
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1,temp=0;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack : ");
scanf("%d",&n);
do
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\nEnter your choice: \n");
scanf("%d",&choice);
OUTPUT:
Thus, the program to demonstrate the array implementation of stack ADT was done successfully and the
output is verified.
AIM:
To write a C program to demonstrate the Linked List implementation of stack ADT.
ALGORITHM:
Step 1: Start
Step 2: Create a structure with val next and head pointer as its member
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a stack is empty
Step 6: If empty push the element entered by the user and make it as head
Step 7: If not print Overflow
Step 8: Check whether the stack array is empty,If so print Underflow
Step 9: If not do step 10
Step 10: Make the previous of top element as head
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void show();
struct node
{
int val;
struct node *next;
};
struct node *head;
int n,choice,temp=0;
void main ()
K.S.AJAY VARSHAAN (71812253006)
37
{
{
ptr->val=val;
ptr->next=head;
head=ptr;
}
printf("Item pushed...");
}
}
void pop()
{
int item;
struct node *ptr;
if (head==NULL)
{
printf("Underflow...");
}
else
{
item=head->val;
ptr=head;
head=head->next;
free(ptr);
printf("Item popped...");
}
}
void show()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty...\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
OUTPUT:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Create a queue and declare front and rear as -1
Step 3: Get the choice to perform a specific operation from user
Step 4: If it is 1 do Step 5,6,7,If 2 do Step 8,9,10,If 3 do Step 11,If 4 do Step 12
Step 5: To push an element check whether a queue is empty
Step 6: If empty push the element entered by the user at the end that is rear
Step 7: If not print Overflow
Step 8: Check whether the queue array is empty,If so print Underflow
Step 9: If not do step 10
Step 10: Remove element from front
Step 11: Display the element from top to bottom
Step 12: Do the cases until the user wants to stop
Step 13: Stop
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
void enqueue();
void dequeue();
void display();
int front=-1,rear=-1,temp=1;
int queue[maxsize];
void main ()
{
OUTPUT:
AIM:
To write a C program to demonstrate the Linked List implementation of Queue ADT.
ALGORITHM:
Step 1: Create a class QNode with data members integer data and QNode* next
Step 2: A parameterized constructor that takes an integer x value as a parameter and sets data
equal to x and next as NULL
Step 3: Create a class Queue with data members QNode front and rear
Step 4: Enqueue Operation with parameter x:
Step 5: Initialize QNode* temp with data = x
Step 6: If the rear is set to NULL then set the front and rear to temp and return(Base Case)
Step 7: Else set rear next to temp and then move rear to temp
Step 8: Dequeue Operation:
Step 9: If the front is set to NULL return(Base Case)
Step 10: Initialize QNode temp with front and set front to its next
Step 11: If the front is equal to NULL then set the rear to NULL
Step 12: Delete temp from the memory
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
K.S.AJAY VARSHAAN (71812253006) 48
{
printf("\n**********Main Menu**********\n");
}
void delete ()
OUTPUT:
Thus, the program to demonstrate the Linked List implementation of queue ADT was done
successfully and the output is verified.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Initialize all the elements of the array as -1 to depict the space as empty
Step 4: Get the choice to perform a specific operation from user
Step 5: If it is 1 do Step 6,7,8,9, If 2 do Step 10,11,If 3 do Step 12,13,14,If 4 do Step 15,If 5 do Step 16
Step 6: Key is the remainder of value and size of array
Step 7: To insert an element,check if the key position is -1
Step 8: If -1 insert the element entered by the user
Step 9: Else if collision, Check for the next position and go to step 7
Step 10: To delete do step 6 and check if the value entered and value stored are equal
Step 11: If yes, change the value to -1. Now take the address of the next element and save it in next
pointer of the previous element of the deleted element
Step 12: To search do step 6 and check if the value entered and value stored are equal
Step 13: If yes, Print the key of the element
Step 14: Else, print not found
Step 15: Display all the elements of the array using for loop
Step 16: Exit the do while loop
Step 17: Stop
PROGRAM:
#include<stdio.h>
void init(int size,int arr[])
{
int i; for(i=0;i<size;i++)
{
arr[i]=-1;
}
}
K.S.AJAY VARSHAAN (71812253006) 53
void insert(int value,int size,int arr[])
{
int key=value%size; if(arr[key]==-1)
{
arr[key]=value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[key]); printf("Unable to insert
%d\n",value);
printf("Finding next free position...\n"); for(int i=key;i<size;i++)
{
if(arr[i]==-1)
{
arr[i]=value;
printf("%d inserted at arr[%d]\n", value,i); break;
}
}
}
}
void delete(int value,int size,int arr[])
{
int key=value%size; if(arr[key]==value)
{
printf("%d element deleted from position %d\n",value,key); arr[key]=-1;
}
else
{
printf("%d not present in the hash table\n",value);
}
}
void search(int value,int size,int arr[])
{
OUTPUT:
AIM:
To write a C program to implement linear and binary search using array ADT.
ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Get the choice to perform a specific operation from user
Step 5: If it is 1 do Step 6,7,8,9,If 2 do Step 10,11,12,13,14,15If 3 do Step 16,If 4 do Step 17
Step 6: Call LinearSearch() function
Step 7: Start from the first element and compare every element with search element
Step 8: If values match, Print element found
Step 9: Else, Print element not found and return to Step 5
Step 10: Call BinarySearch() function and sort elements
Step 11: Find the middle element, Compare the middle element and search element
Step 12: If same, Print element found and return to Step 5
Step 13: If greater, Find the middle element to the left of middle element (0-mid) and do
step 11
Step 14: If lesser, Find the middle element to the right of middle element(mid-n) and do Step 11
Step 15: Else, print element not found and return to Step 5.
Step 16: Display elements on the array
Step 17: Exit the loop
Step 18: Stop
PROGRAM:
#include<stdio.h>
void LinearSearch(int arr[],int,int); int BinarySearch(int arr[],int,int,int); int main()
{
int n,choice,search,search1,x=1,temp; printf("Enter number of elements in an array: "); scanf("%d",&n);
int arr[n];
printf("Enter %d elements of the array: \n",n); for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("...ARRAY SEARCHING TECHNIQUES...\n");
printf("1.LINEAR SEARCH\n2.BINARY SEARCH\n3.DISPLAY\n4.EXIT\n");
K.S.AJAY VARSHAAN (71812253006) 58
printf("Your choice: "); scanf("%d",&choice); switch(choice)
{
case 1:
printf("\nEnter an element to search: "); scanf("%d",&search); LinearSearch(arr,n,search);
break; case 2:
printf("\nEnter an element to search: "); scanf("%d",&search1);
printf("\nArray Sorting..."); for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i]; arr[i]=arr[j]; arr[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
int r=BinarySearch(arr,0,n-1,search1); if(r==-1)
{
printf("\n%d element not found...\n\n",search1);
}
break; case 3:
printf("\n...ARRAY...\n"); for(int i=0;i<n;i++)
{
printf("ARRAY[%d] = %d\n",i,arr[i]);
}
break; case 4:
printf("Do you want to exit(yes 0/no 1): "); scanf("%d",&x);
break;
}
}while(x==1);
}
void LinearSearch(int arr[],int n,int search)
{
int flag;
for(int i=0;i<n;i++)
{
if(arr[i]==search)
{
printf("\n%d element found at index %d\n\n",search,i); flag=1;
break;
}
}
if(flag!=1)
{
printf("%d element not found...\n\n",search);
}
}
OUTPUT:
AIM:
To write a C program to implement Insertion sort using array ADT.
ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Iterate from arr[i] to arr[n] over the array
Step 5: Assume the first element to be sorted, Store next element as key
Step 6: Now compare the key with all the elements in the array
Step 7: If the element is smaller than the current element move to the next element
Step 8: Else, shift greater elements in the array towards right
Step 9: Insert the element
Step 10: Continue from Step 5, Until the array is sorted
Step 11: Stop
PROGRAM:
# #include<stdio.h>
void display(int,int arr[]);
void main()
{
int n,key,j;
printf("Enter number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter array elements: \n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n...INSERTION SORT...\n");
for(int i=1;i<n;i++)
{
OUTPUT:
AIM:
ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: Iterate from arr[i] to arr[n] over the array
Step 5: Compare adjacent elements of the array
Step 6: If current element is greater than the next element, swap its position and continue
Step 7: Else, move to next element
Step 8: Continue from Step 4, Until the array gets sorted
Step 9:Stop
PROGRAM:
#include <stdio.h>
void printArray(int arr[],int); void swap(int *x, int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}
void bubbleSort(int arr[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
RESULT:
Thus, the program to demonstrate the implementation of bubble sort using Array ADT was executed
successfully and the output is verified.
AIM:
To write a C program to implement quick sort using array ADT.
ALGORITHM:
Step 1: Start
Step 2: Choose the highest index value has pivot
Step 3: Take two variables to point left and right of the list excluding pivot Step 4: Move left points to
the low index
Step 5: Move right points to the high index
Step 6: While value at left is less than pivot move right Step 7: While value at right is greater than pivot
move left
Step 8: If both step 5 and step 6 does not match swap left and right
Step 9: If left ≥ right, the point where they met is new pivot
Step 10: Stop
PROGRAM:
#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);
}
}
K.S.AJAY VARSHAAN (71812253006) 68
void printArray(int array[], int size)
{
for(int i=0;i<size;i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter size of the array: "); scanf("%d",&n);
int data[n];
printf("Enter the elements of the array:\n"); for(int i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
printf("Unsorted Array\n"); printArray(data, n); quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n"); printArray(data, n);
}
OUTPUT:
RESULT:
Thus, the program to demonstrate the implementation of Quick sort using Array ADT was executed
successfully and the output was verified.
AIM:
To write a C program to implement quick sort using array ADT.
ALGORITHM:
Step 1: Start
Step 2: Choose the highest index value has pivot
Step 3: Take two variables to point left and right of the list excluding pivot Step 4: Move left points to
the low index
Step 5: Move right points to the high index
Step 6: While value at left is less than pivot move right Step 7: While value at right is greater than pivot
move left
Step 8: If both step 5 and step 6 does not match swap left and right
Step 9: If left ≥ right, the point where they met is new pivot
Step 10: Stop
PROGRAM:
#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);
}
}
K.S.AJAY VARSHAAN (71812253006) 68
void printArray(int array[], int size)
{
for(int i=0;i<size;i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter size of the array: "); scanf("%d",&n);
int data[n];
printf("Enter the elements of the array:\n"); for(int i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
printf("Unsorted Array\n"); printArray(data, n); quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n"); printArray(data, n);
}
OUTPUT:
RESULT:
Thus, the program to demonstrate the implementation of Quick sort using Array ADT was executed
successfully and the output was verified.
AIM:
ALGORITHM:
Step 1: Start
Step 2: Get the size of the array from user and declare an array
Step 3: Get elements of the array from user
Step 4: If it is only one element it is already sorted, return
Step 5: Divide the array iteratively in equal halves until atomic values are achieved
Step 6: Merge the small lists exactly in the same way they were broken down
Step 7: Compare the elements in each list and combine them in a sorted manner into a new list
Step 8: Print the merged list after each recursion
Step 9: Print the final sorted array
Step 10: Stop
PROGRAM:
#include <stdio.h>
void printArray(int arr[],int);
void merge(int arr[], int start, int mid, int end)
{
int n1=mid-start+1; int n2=end-mid;
int first_half[n1],second_half[n2]; for(int i=0;i<n1;i++)
{
first_half[i]=arr[start+i];
}
for(int j=0;j<n2;j++)
second_half[j]=arr[mid+1+j];
}
int i, j, k; i=0;
j=0;
k=start;
while (i<n1&&j<n2)
{
if (first_half[i]<=second_half[j])
{
arr[k]=first_half[i]; i++;
mergeSort(arr, beg, mid); mergeSort(arr, mid + 1, end); merge(arr, beg, mid, end);
}
}
void printArray(int arr[], int size)
{
for (int i=0;i<size;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int size;
printf("Enter the size of the array: "); scanf("%d",&size);
int arr[size];
printf("Enter array elements: \n"); for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
mergeSort(arr, 0, size-1); printf("Sorted array: \n"); printArray(arr, size);
}
RESULT:
Thus, the program to demonstrate the implementation of merge sort using Array ADT was
executed successfully and the output was verified.