Professional Documents
Culture Documents
ALGORITHM:
Selection_Sort (A[ ] , N)
Step 1 : start Begin
Step 2 : Set POS = K
Step 3 : Repeat for J = K + 1 to N –1
Begin
If A[ J ] < A [ POS ]
Set POS = J
Step 4 : End For Swap A [ K ] End For with A [ POS ]
Step 5 : Stop
CONCLUSION:
Time complexity:
Worst case=average case=best case=O(n^2)
Space complexity:
Any extra data structure apart from the input array is not used, the space
complexity is O(1).
DESCRIPTION: Radix sort is a sorting algorithm that sorts the elements by first
grouping the individual digits of the same place value. Then, sort the elements
according to their increasing/decreasing order.
CONCLUSION:
Time complexity:
Let K be the number of digits in the maximum element of the array and b be the base of
array elements.
It takes O(N + b) time to sort a particular digit, and the algorithm runs the counting sort
for all the K digits. Therefore, the total time complexity is O(K * (N + b))
Space complexity:
O(n+k) to hold counts, indices and output arrays.
ALGORITHM:
merge_sort(arr, beg, end)
Step 1:if beg < end
Step 2:set mid = (beg + end)/2
Step 3:merge_sort(arr, beg, mid)
step 4:merge_sort(arr, mid + 1, end)
step 5:merge (arr, beg, mid, end)
Step 6:end of if
Merge(a, l, m, h )
Page 9 Roll no:160121748021
Step 1:i=l,j=m+1,k=l
Step 2:while i<=m and j<=h do
if a[i]<=m then c[k++]=a[i++]
else c[k++]=a[j++]
end if
Step 3:end while
Step 4: while j<=h do c[k++]=a[j++]
Step 5:end while
Step 6:for i=l to k-1 do a[i]=c[i]
Step 7:end for
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
} k+
+;
}
while (i < n1) {
arr[k] = L[i];
i++;
Page 10 Roll no:160121748021
k++;
CONCLUSION:
Time complexity:
Worst case=average case=best case=O(nlogn)
The array gets divided into subarrays recursively which will create O(nlogn) rows
where each element is present in each row, it takes O(n) time to merge.That is, It
takes O(nlogn) time to complete the process.
Space complexity:
An auxiliary array of size at most n is used to store the merged subarray, the space
complexity is O(n).
PARTITION (array A, start, end)
Step 1: pivot =A[start]
Page 14 Roll no:160121748021
Step 2:i = start-1
Step 3: for j = start to end -1
do if (A[j] < pivot)
then i = i + 1
swap A[i] with A[j]
Step 4:swap A[i+1] with A[end]
Step 5: return i+1
PROGRAM:
//Quick sort in C
#include<stdio.h>
void printArray(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void quickSort(int a[],int first,int last)
{
int p,i,j,temp;
i = first;
j = last;
if(i<j)
{
p = first;
while(i<j)
{
while(a[i] <= a[p] && i<last)
{
i++;
}
while(a[j] > a[p])
{
j--;
}
if(i<j){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[j];
Page 15 Roll no:160121748021
a[j] = a[p];
a[p] = temp;
quickSort(a,first,j-1);
quickSort(a,j+1,last);
}
//printArray(a,n);
}
void main(){
int n,a[100],i;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int first = 0,last = n-1;
quickSort(a,first,last);
printArray(a,n);
}
OUTPUT:
Average and best case O(nlogn): The array is divided into two subarrays recursively
which will create O(log n) rows where each element is present in each row exactly
once. For each row, it takes O(n) time to merge every pair of subarrays. So the overall
time complexity becomes O(n log n).
Space complexity:
Worst case:O(n)
Best case:O(logn)
Additional space is required for creating stack frames in recursive call
OUTPUT:
To access the i-th element, to traverse all elements , O(√N * N) time is used and to
insert element at current point, delete current element , Insert element at front ,O(1)
time is used and to insert the element at end, O(√N * N) time is used.
Space Complexity:
O(1)
Extra space is not needed beyond a fixed number of variables.
DESCRIPTION:Doubly linked list is a type of linked list in which each node apart
from storing its data has two links. The first link points to the previous node in the
list and the second link points to the next node in the list. The first node of the list
has its previous link pointing to NULL similarly the last node of the list has its next
node pointing to NULL.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
}*head,*tail,*new,*temp;
struct node *head=NULL;
void create(){
struct node *new; int data,c=1;
while(c){
new=(struct node *)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&data);
new->data=data;
new->next=NULL;
if(head==NULL){
head=new;
tail=new;
}
else{
tail->next=new;
new->prev=tail;
tail=new;
}
printf("do you want to cotinue(1-yes/0-no)");
scanf("%d",&c);
}
}
void traverse(){
int count=0;
struct node *temp;
if(head==NULL) {
printf("List is empty\n");
return;
}
else{
temp=head;
printf("The List elements are:");
while(temp!=tail){
Page 28 Roll no:160121748021
printf("%d ",temp->data );
temp=temp->next ; count+
+;
}
printf("%d ",temp->data );
}
printf("\nNo of elements:%d\n",count+1);
}
void deletion(){
int z,p,i;
struct node *temp;
temp=head;
if (head==NULL){
printf("The list is empty\n"); return
}
printf("Delete element at 1.beginning 2.ending 3.specific position:");
scanf("%d",&z);
if (z==1){
head=head->next;
head->prev=NULL;
temp->next=NULL;
return;
}
if(z==2){
while (temp->next!=tail){
temp=temp->next;
}
temp->next=NULL;
tail->prev=NULL;
tail=temp;
return;
}
if (z==3){
printf("Enter the position:");
scanf("%d",&p);
temp=head;
for (i=0;i<p-1;i++){
temp=temp->next;
}
temp->next=temp->next->next;
temp->next->next->prev=temp;
return;
}
}
OUTPUT:
ALGORITHM:
algorithm for push operation:
step 1:allocate memory for the new node and name it as new_node
step 2:set new_node->data=val
step 3:if top=null
set new_node->next=null
set top=new_node
else
set new_node->next=top
set top=new_node
step 4:end if
step 5:end
algorithm for pop operation:
step 1:if top=null
print “underflow”
goto step 5
step 2:end if
step 3:set ptr=top
step 4:set top=top->next
step 5:free ptr
step 6:end
Page 34 Roll no:160121748021
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int main()
{
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
OUTPUT:
CONCLUSIONS:
Θ(n) time is taken to reverse a linked list since n elements are traversed.
Space complexity:
Θ(1) space is required as no new space is used.
print overflow
step 2:end if
step 5: end
print underflow
Page 38 Roll no:160121748021
step 2:end if
step 5: end
goto step 3
step 3: end
PROGRAM:
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
while(choice != 4)
{
printf("MENU LIST\n");
printf("1.Push\n2.Pop\n3.Show\n4.Exit");
printf("Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
Page 39 Roll no:160121748021
break;
}
case 3:
{
show();
break;
}
case 4:
{
printf("Exiting");
break;
}
default:
{
printf("Enter a valid choice ");
}
};
}
}
void push ()
{
int x;
if (top==n )
printf("\n Too many elements");
else
{
printf("Enter the value");
scanf("%d",&x);
top=top +1;
stack[top]=x;
}
}
void pop ()
{
if(top==-1)
printf("No elements");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
{ printf("%d\n",stack[i]);
}
Page 40 Roll no:160121748021
if(top == -1)
{
printf("Stack is empty");
}
OUTPUT:
Space Complexity:
O(1), As no extra space is being used
void push(){
new=(struct node*)malloc(sizeof(struct node));
printf("enter the data:\n");
scanf("%d",&new->data);
if(top==NULL){
new->next=NULL;
top=new;
}
else{
new->next=top;
top=new;
}
}
void pop(){
if(top==NULL){
printf("Stack is empty\n");
}
else{
temp=top;
top=top->next;
temp->next=NULL;
printf("element deleted:%d",temp->data);
}
}
void Top(){
if(top==NULL){
printf("Stack is empty\n");
}
else{
printf("Top most element is %d\n",top->data);
}
}
void display(){
temp=top;
if(temp==NULL){
printf("Stack is empty\n");
Page 44 Roll no:160121748021
}
else{
while(temp->next!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void main(){
int choice = 1,c;
while(choice){
printf("What do you want to perform?\n");
printf("1.Push\n2.Pop\n3.Top\n4.Display\n");
scanf("%d",&c);
switch(c){
case 1 :push();
break;
case 2 : pop();
break;
case 3 : Top();
break;
case 4 : display();
break;
default : printf("Invalid number\n");
}
printf("\nDo you want to continue?\n");
printf("1.YES\n0.NO\n");
scanf("%d",&choice);
}
}
Space Complexity:
No extra space is used to insert, delete, and access the elements.
Page 47 Roll no:160121748021
Hence, O(1) space is used
10.AIM: Converting Infix expression to postfix expression.
DESCRIPTION:
When the operator is written in between the operands, then it is known as infix
notation. The postfix expression is an expression in which the operator is written after
the operands. To convert infix expression to postfix expression, stack data structure is
used. By scanning the infix expression from left to right, when we will get any
operand, simply add them to the postfix form, and for the operator and parenthesis, add
them in the stack maintaining the precedence of them.Here only {+, −, ∗,/, ^} operators
are considered, other operators are neglected.
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char s[100];
int top=-1;
void push(char z){
top=top+1;
s[top]=z;
}
char pop(){
if(top == -1)
return -1;
else
return s[top--];
}
int priority(char p){
switch(p){
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':return 3;
default:return 0;
Page 48 Roll no:160121748021
}
}
void main(){
char ex[100],*e,c;
printf("Enter the expression: ");
scanf("%s",&ex);
e=ex;
printf("Postfix expression :");
while(*e!='\0'){
if (isalnum(*e)){
printf("%c",*e);
}
else if (*e=='('){
push(*e);
}
else if (*e==')'){
while((c = pop()) != '(')
printf("%c", c);
}
else{
while(priority(s[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e=e+1;
}
while (top!=-1){
printf("%c",pop());
}
}
OUTPUT:
Space Complexity:
Θ(n)
For storing Infix expression of n literals the space complexity is O(n) and for stack to
hold atmost n literals the space complexity is O(n)
DESCRIPTION:
Postfix notation is a notation for writing arithmetic expressions in which the operands
appear before their operators. There are no precedence rules to learn, and parentheses
are never needed. Evaluation of postfix expression can be easily done using stack.
From the postfix expression, when some operands are found, pushed them in the stack.
When some operator is found, two items are popped from the stack and the operation is
performed in correct sequence. After that, the result is also pushed in the stack for
future use. After completing the whole expression, the final result is also stored in the
stack top.
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char s[100];
int top=-1;
void push(char z){
top=top+1;
s[top]=z;
}
char pop(){
if(top == -1)
return -1;
else
return s[top--];
}
Page 51 Roll no:160121748021
int priority(char p){
switch(p){
case '(':return 0;
case '+':
case '-':return 1;
case '*':
case '/':return 2;
case '^':return 3;
default:return 0;
}
}
void main(){
char ex[100],*e,c;
printf("Enter the expression: ");
scanf("%s",&ex);
e=ex;
printf("Postfix expression :");
while(*e!='\0'){
if (isalnum(*e)){
printf("%c",*e);
}
else if (*e=='('){
push(*e);
}
else if (*e==')'){
while((c = pop()) != '(')
printf("%c", c);
}
else{
while(priority(s[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e=e+1;
}
while (top!=-1){
printf("%c",pop());
}
OUTPUT:
Space complexity: Θ(n)
For storing Infix expression of n literals the space complexity is O(n) and for stack to
hold atmost n literals the space complexity is O(n)