Advanced programming lab IT-320 NSIT

48
Q1)WAP to implement Quick Sort and Merge Sort. Ans) Merge Sort #include<stdio.h> void mergesort(int *a,int first,int last); void merge(int *a,int first,int mid,int last); int main(void) { int i,n; long a[100]; scanf("%d",&n); for( i = 0 ; i < n ; i++ ) scanf("%d",&a[i]); mergesort(a,0,n-1); for( i = 0 ; i < n ; i++) printf("%d ",a[i]); return 0; } void mergesort(int *a,int first,int last) { if( last > first) {

description

Advanced programming lab IT-320 NSIT Contains all the programs of the lab.You can use it directly.

Transcript of Advanced programming lab IT-320 NSIT

Q1)WAP to implement Quick Sort and Merge Sort.Ans)Merge Sort#includevoid mergesort(int *a,int first,int last);

void merge(int *a,int first,int mid,int last);int main(void){ int i,n; long a[100]; scanf("%d",&n); for( i = 0 ; i < n ; i++ ) scanf("%d",&a[i]); mergesort(a,0,n-1); for( i = 0 ; i < n ; i++) printf("%d ",a[i]); return 0;}void mergesort(int *a,int first,int last){ if( last > first) { int mid = ( first+last )/2; mergesort(a,first,mid); mergesort(a,mid+1,last); merge(a,first,mid,last); }}void merge(int *a,int first,int mid,int last){ int pos = 0,i = first,j = mid+1; int temp[last-first+1]; while( i link->expo >= ex) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start;}/*End of insert()*/

struct node *insert(struct node *start,float co,int ex){ struct node *ptr,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->coef=co; tmp->expo=ex; /*If list is empty*/ if(start==NULL) { tmp->link=start; start=tmp; } else /*Insert at the end of the list*/ { ptr=start; while(ptr->link!=NULL) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start;}/*End of insert()*/

void display(struct node *ptr){ if(ptr==NULL) { printf("Zero polynomial\n"); return; } while(ptr!=NULL) { printf("(%.1fx^%d)", ptr->coef,ptr->expo); ptr=ptr->link; if(ptr!=NULL) printf(" + "); else printf("\n"); }}/*End of display()*/void poly_add(struct node *p1,struct node *p2){ struct node *start3; start3=NULL;

while(p1!=NULL && p2!=NULL) { if(p1->expo > p2->expo) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } else if(p2->expo > p1->expo) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } else if(p1->expo==p2->expo) { start3=insert(start3,p1->coef+p2->coef,p1->expo); p1=p1->link; p2=p2->link; } } /*if poly2 has finished and elements left in poly1*/ while(p1!=NULL) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } /*if poly1 has finished and elements left in poly2*/ while(p2!=NULL) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } printf("Added polynomial is : "); display(start3);}/*End of poly_add() */

void poly_mult(struct node *p1, struct node *p2){ struct node *start3; struct node *p2_beg = p2; start3=NULL; if(p1==NULL || p2==NULL) { printf("Multiplied polynomial is zero polynomial\n"); return; } while(p1!=NULL) { p2=p2_beg; while(p2!=NULL) { start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo); p2=p2->link; } p1=p1->link; } printf("Multiplied polynomial is : "); display(start3);}/*End of poly_mult()*/

Q3)WAP to implement LCS using dynamic programming.#include#includeint max(int A,int B){ return (A > B) ? A : B;}int main(void){ char a[100],b[100]; int n,m,i,j; printf("Enter two strings : "); scanf("%s %s",a,b); n = strlen(a); m = strlen(b); int L[n+1][m+1]; for(i=0;icharacter !=SENTINAL)coutcharacter freq);new_node->left= min1;new_node->right= min2;//push back in minHeapminHeap.push(new_node);}print_heap(minHeap.top(), std::string(""));return 0;}

Q5)WAP to implement two integer multiplication using divide and conquer technique.#includeusing namespace std; int makeEqualLength(string &str1, string &str2){ int len1 = str1.size(); int len2 = str2.size(); if (len1 < len2) { for (int i = 0 ; i < len2 - len1 ; i++) str1 = '0' + str1; return len2; } else if (len1 > len2) { for (int i = 0 ; i < len1 - len2 ; i++) str2 = '0' + str2; } return len1; // If len1 >= len2}string addBitStrings( string first, string second ){ string result; int length = makeEqualLength(first, second); int carry = 0;

for (int i = length-1 ; i >= 0 ; i--) { int firstBit = first.at(i) - '0'; int secondBit = second.at(i) - '0'; int sum = (firstBit ^ secondBit ^ carry)+'0'; result = (char)sum + result; carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry); } if (carry) result = '1' + result; return result;}int multiplyiSingleBit(string a, string b){ return (a[0] - '0')*(b[0] - '0'); }long int multiply(string X, string Y){ int n = makeEqualLength(X, Y); if (n == 0) return 0; if (n == 1) return multiplyiSingleBit(X, Y); int fh = n/2; int sh = (n-fh); string Xl = X.substr(0, fh); string Xr = X.substr(fh, sh); string Yl = Y.substr(0, fh); string Yr = Y.substr(fh, sh); long int P1 = multiply(Xl, Yl); long int P2 = multiply(Xr, Yr); long int P3 = multiply(addBitStrings(Xl, Xr), addBitStrings(Yl, Yr)); return P1*(10); if (n == 1) { C[c.ra][c.ca] += A[a.ra][a.ca] * B[b.ra][b.ca]; return; } // Create the 12 smaller matrix indexes: // A00 A01 B00 B01 C00 C01 // A10 A11 B10 B11 C10 C11 for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { find_corner(a, i, j, &aii[i][j]); find_corner(b, i, j, &bii[i][j]); find_corner(c, i, j, &cii[i][j]); } } p.ra = p.ca = 0; p.rb = p.cb = m / 2; #define LEN(A) (sizeof(A)/sizeof(A[0])) for (i = 0; i < LEN(P); i++) set(P[i], p, 0); #define ST0 set(S,p,0); set(T,p,0) // (A00 + A11) * (B00+B11) = S * T = P0 ST0; add(A, A, S, aii[0][0], aii[1][1], p); add(B, B, T, bii[0][0], bii[1][1], p); mul(S, T, P[0], p, p, p); // (A10 + A11) * B00 = S * B00 = P1 ST0; add(A, A, S, aii[1][0], aii[1][1], p); mul(S, B, P[1], p, bii[0][0], p); // A00 * (B01 - B11) = A00 * T = P2 ST0; sub(B, B, T, bii[0][1], bii[1][1], p); mul(A, T, P[2], aii[0][0], p, p); // A11 * (B10 - B00) = A11 * T = P3 ST0; sub(B, B, T, bii[1][0], bii[0][0], p); mul(A, T, P[3], aii[1][1], p, p); // (A00 + A01) * B11 = S * B11 = P4 ST0; add(A, A, S, aii[0][0], aii[0][1], p); mul(S, B, P[4], p, bii[1][1], p); // (A10 - A00) * (B00 + B01) = S * T = P5 ST0; sub(A, A, S, aii[1][0], aii[0][0], p); add(B, B, T, bii[0][0], bii[0][1], p); mul(S, T, P[5], p, p, p); // (A01 - A11) * (B10 + B11) = S * T = P6 ST0; sub(A, A, S, aii[0][1], aii[1][1], p); add(B, B, T, bii[1][0], bii[1][1], p); mul(S, T, P[6], p, p, p); // P0 + P3 - P4 + P6 = S - P4 + P6 = T + P6 = C00 add(P[0], P[3], S, p, p, p); sub(S, P[4], T, p, p, p); add(T, P[6], C, p, p, cii[0][0]); // P2 + P4 = C01 add(P[2], P[4], C, p, p, cii[0][1]); // P1 + P3 = C10 add(P[1], P[3], C, p, p, cii[1][0]); // P0 + P2 - P1 + P5 = S - P1 + P5 = T + P5 = C11 add(P[0], P[2], S, p, p, p); sub(S, P[1], T, p, p, p); add(T, P[5], C, p, p, cii[1][1]); }int main(){ mat A, B, C; corners ai = { 0, N, 0, N }; corners bi = { 0, N, 0, N }; corners ci = { 0, N, 0, N }; // identity(A,bi); identity(B,bi); // set(A,ai,2); set(B,bi,2); randk(A, ai, 0, 2); randk(B, bi, 0, 2); print(A, ai, "A"); print(B, bi, "B"); set(C, ci, 0); // add(A,B,C, ai, bi, ci); mul(A, B, C, ai, bi, ci); print(C, ci, "C"); return 0;}

Q7)WAP to implement DFS and BFS.DFS#includeusing namespace std;struct AL{ int data; struct AL *next;};int V[10] = {0};void addEdge(vector &G,int start,int end){ struct AL *temp = (struct AL *)malloc(sizeof(struct AL)); temp->data = end; temp->next = G[start]; //G[start] initially contains NULL G[start] = temp; //Now G[start] contains value at temp}void dfs(vector &G,int i){ V[i] = 1; printf("%d ",i); struct AL *temp = G[i]; while(temp) { if(!V[temp->data]) dfs(G,temp->data); temp = temp->next; }}void bfs(vector &G,int i){ queue Q; Q.push(i);

//V[i] = 1; while(!Q.empty()) { int j = Q.front(); printf("%d ",j); V[j] = 1; Q.pop(); struct AL *temp = G[j]; while(temp) { if(!V[temp->data]) { Q.push(temp->data); // V[temp->data] = 1; } temp = temp->next; } }}int main(void){ vector Graph; int v = 4,i; for(int i=0;iweight = weight; temp->data = end; temp->next = G[start]; //G[start] initially contains NULL G[start] = temp; //Now G[start] contains value at temp}

void dijkstra(vector &G,int start,int N){ int distance[N+1],v[N+1],i; for(i=0;idata] > distance[u] + temp->weight)distance[temp->data] = distance[u] + temp->weight;PQ.push(make_pair(temp->data,distance[temp->data]));temp = temp->next;}}for(i=0;inext = G[start]; //G[start] initially contains NULLtemp->weight = weight; G[start] = temp; //Now G[start] contains value at temp

}void BellmanFord(vector G,int start,int N){int dist[N+1];for(int i=0;iweight))dist[temp->data] = dist[j] + temp->weight;temp = temp->next;}}}printf("Vertex Distance\n");for(int i=0;i