Documente Academic
Documente Profesional
Documente Cultură
2/n,37, n, n(log(log n)), n(log n), n(log2n), n(log (n2)), n1.5, n2 , n2(log n) ,n3 ,2n/2,2n
These two growing at the same Rate are : n(log n), n(log (n2))
#include <stdio.h>
struct node
{
int data;
struct node *next;
}*top=NULL,*p,*temp;
#include <stdio.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*p,*temp;
void display()
{
if(head == NULL)
{ printf("\nList is Empty\n"); }
else
{
printf("\n\nList of elements are - \n");
for(p=head; p!=NULL; p=p->next)
printf("%d ",p->data);
}
}
5.Write an algorithm to evaluate a postfix expression.
1)Add ) to postfix expression.
2) Read postfix expression Left to Right until ) encountered
3) If operand is encountered, push it onto Stack
[End If]
6) END
6. Show the result of running shell sort on the input 9,8,7,6,5,4,3,2,1 using the
increments {1,3,7}:
Solution:
(Index) 1 2 3 4 5 6 7 8 Explanation
0
With Gap value 7:
9 8 7 6 5 4 3 2 1
We compare array value 2<9, if true, swap value 2 and 9
2 8 7 6 5 4 3 9 1 Array after swap
With Gap value 7:
2 8 7 6 5 4 3 9 1
We compare array values 1<8 ,if true, swap value 1 and 8
2 1 7 6 5 4 3 9 8 Array after swap
With Gap value 3:
2 1 7 6 5 4 3 9 8
We compare array value 6<2, if false, no swap
With Gap value 3:
2 1 7 6 5 4 3 9 8
We compare array value 5<1, if false, no swap
With Gap value 3:
2 1 7 6 5 4 3 9 8
We compare array value 5<1, if false, no swap
With Gap value 3:
2 1 7 6 5 4 3 9 8
We compare array value 3<6, if true, swap value 3 and 6
2 1 7 3 5 4 6 9 8 Array after swap
With Gap value 3:
2 1 7 3 5 4 6 9 8
We compare array value 8<4, if false, no swap
With Gap value 1:
2 1 7 3 5 4 6 9 8
We compare array value 1<2, if true, swap values 1 and 2
1 2 7 3 5 4 6 9 8 Array after swap
With Gap value 3:
1 2 7 3 5 4 6 9 8
We compare array value 2<7, if false, no swap
With Gap value 1:
1 2 7 3 5 4 6 9 8 We compare array value 3<7, if true, swap values 3 and 7 and
compare 3<2, if false no swap
1 2 3 7 5 4 6 9 8 Array after swap
With Gap value 1:
1 2 3 7 5 4 6 9 8 We compare array value 5<7, if true, swap values 5 and 7 and
compare 5<3, if false no swap
1 2 3 5 7 4 6 9 8 Array after swap
With Gap value 1:
We compare array value 4<7, if true, swap values 4 and 7 and
1 2 3 5 7 4 6 9 8
compare 4<5, if true swap values 4 and 5, and compare 4<3, if
false, no swap
1 2 3 4 5 7 6 9 8 Array after swap
With Gap value 1:
1 2 3 4 5 7 6 9 8 We compare array value 6<7, if true, swap values 6 and 7 and
compare 6<5, if false, no swap
1 2 3 4 5 6 7 9 8 Array after swap
With Gap value 1:
1 2 3 4 5 6 7 9 8
We compare array value 9<7, if false, no swap
With Gap value 1:
1 2 3 4 5 6 7 9 8 We compare array value 8<9, if true, swap values 8 and 9 and
compare 8<7, if false, no swap
1 2 3 4 5 6 7 8 9 Array after swap
1 2 3 4 5 6 7 8 9 Final Sorted array values
7. Write a program to print the element of a Single linked list.
#include <stdio.h>
struct node
{
int data;
struct node *next;
}*head=NULL,*p,*temp;
void display()
{
if(head == NULL)
{ printf("\nList is Empty\n"); }
else
{
printf("\n\nList of elements are - \n");
for(p=head; p!=NULL; p=p->next)
printf("%d ",p->data);
}
}
8.)Write a Program to find an element in a singly linked list. Do this both recursively
and non recursively.
9) Programs A & B are analyzed and found to have worst case running time no greater
than 150nLog2n And n2 respectively. Answer the fallowing question.
a. Which program has the better guarantee on the running time, for the large
value of n(n>10,000)?
b. Which program has the better guarantee on the running time, for the small
value of n(n<100)?
c. Which program will run faster on average for n = 1,000.
d. Is it possible that program B will run faster than program A on all possible
inputs?
a. For input size n>10000, Program A is better than Program B.
Proof:
Let, n = 10001.
Consider, 150 * n * Log2n
= 150 * 10001 * Log2 10001
= 150 * 10001 * 13
= 1,95,01,950
Consider, n2
= 10001 * 10001
= 10,00,20,001
b. For input size n<100, Program B is better than Program A.
Proof:
Let, n = 99
Consider, 150 * n * Log2n
= 150 * 99 * Log299
= 15 * 99 *6
= 89,100
Consider, n2
= 99 * 99
= 9,801
c. . For input size n=1000, Program B is better than Program A.
Proof:
Let, n = 1000
Consider, 150 * n * Log2n
= 150 *1000 * Log21000
=150 *1000 * 9
= 1350000.
Consider, n2
= 1000 * 1000
= 1000000
while(i<= mid)
{
temp[k++] = a[i++];
}
while(j<=high)
{
temp[k++] = a[j++];
}