Sunteți pe pagina 1din 8

Home assignment 4

1. Order the following functions by growth rate: n,n1.5,n2,n(logn), nlog(logn), n(log2n),


nlog(n2), 2/n,2n,2 n/2,37,n2(logn),n3 Indicate which function grow at the same Rate:

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))

2. Design an algorithm for quick sort.

Step 1 − Choose the highest index value has pivot


Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

3. Write Push and Pop routines in Stack ADT?

#include <stdio.h>
struct node
{
int data;
struct node *next;
}*top=NULL,*p,*temp;

void push(int value)


{
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = value;
temp->next = NULL;
if(top == NULL)
{
top=p= temp;
}
else
{
temp->next = top;
top =p= temp;
printf("\nInsertion is Success!!!\n");
}
}
void pop()
{
int ele;
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
p=top;
top = p->next;
ele=p->data;
free(p);
printf(“Deleted element = %d”, ele);
}
}

4. 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);
}

}
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]

4) If operator is encountered, Pop two elements


i) A -> Top element
ii) B-> Next to Top element
iii) Evaluate B operator A
push B operator A onto Stack
5) Set result = pop

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.

a.) #include <stdio.h>


struct node
{
int data;
struct node *next;
}*head=NULL,*p,*temp;

void search(int key)


{
int flag=0;
for ( p=head; p->next !=NULL; p= p->next)
{
if (p->data == key)
{
flag=1;
break;
}
}
if(flag==1)
printf(“Element is FOUND”);
else
printf(“Element is NOT FOUND”);
}
With Recursion
b.) int search(struct node *start,int key)
{
if (start == NULL)
return 0;
else if (start->data == key)
return 1;
else
return (search(start->next, key));
}
At main function:
if(flag==1)
printf(“Element is FOUND”);
else
printf(“Element is NOT FOUND”);

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

10. Covert the following infix to postfix and evaluate it.


a=2,b=3,c=4,d=1,e=6,f=3 and g=10. a+b*c+(d*e+f)*g
Scanning Character Stack Postfix Expression
a a
+ + a
b + ab
* +* Ab
c +* abc
+ + abc*+
( +( abc*+
d +( abc*+d
* +(* abc*+d
e +(* abc*+de
+ +(+ abc*+de*
f +(+ abc*+de*f
) + abc*+de*f+
* +* abc*+de*f+
g +* abc*+de*f+g
abc*+de*f+g*+
Post fix expression: abc*+de*f+g*+ 6M
Assign values of a,b,c,d,e,f,g in above postfix expression : 234*+1 6 * 3 +10*+
Scanning Character Top of the Stack Stack
2 2 2
3 3 23
4 4 234
* 4*3=12 2 12
+ 2+12=14 14
1 1 14 1
6 6 14 1 6
* 1*6=6 14 6
3 3 14 6 3
+ 6+3=9 14 9
10 10 14 9 10
* 9*10 = 90 14 90
+ 14+90 =104 104
Result = 104. 6M

`11. Sort 3, 1, 4, 1, 5, 9, 2, 6 using mergesort and calculate the running time.


void partition(int a[], int low, int high)
{
int mid;
if(low < high)
{
mid = (low + high) / 2;
partition(a, low, mid);
partition(a, mid + 1, high);
merge(a, low, mid, high);
}
}

void merge(int a[],int low,intmid,int high)


{
int i, j, k, temp[50];
i = low;
j = mid + 1;
k = low;
while ((i<= mid) && (j <= high))
{
if (a[i] <= a[j])
{
temp[k++] = a[i++];
}
else
{
temp[k++] = a[j++];
}
}

while(i<= mid)
{
temp[k++] = a[i++];
}

while(j<=high)
{
temp[k++] = a[j++];
}

for (k = low; k <= high; k++)


{
a[k] = temp[k];
}
}

S-ar putea să vă placă și