37
Write a program to implement Bubble sort Algorithm. #include <stdio.h> int main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) { for (d = 0 ; d < n - c - 1; d++) { if (array[d] > array[d+1]) /* For decreasing order use < */ { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order :\n"); for ( c = 0 ; c < n ; c++ )

Ds Original

Embed Size (px)

DESCRIPTION

asdfghj

Citation preview

Page 1: Ds Original

Write a program to implement Bubble sort Algorithm.

#include <stdio.h>int main(){int array[100], n, c, d, swap;printf("Enter number of elements\n");scanf("%d", &n); printf("Enter %d integers\n", n);for (c = 0; c < n; c++)scanf("%d", &array[c]); for (c = 0 ; c < ( n - 1 ); c++) {for (d = 0 ; d < n - c - 1; d++) {if (array[d] > array[d+1]) /* For decreasing order use < */ {swap = array[d];array[d] = array[d+1];array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ )

printf("%d\n", array[c]); return 0;}

Page 2: Ds Original

Output

Page 3: Ds Original

Write a program to implement Selection sort Algorithm.

#include <stdio.h>int main(){int array[100], n, c, d, position, swap; printf("Enter number of elements\n");scanf("%d", &n);  printf("Enter %d integers\n", n);  for ( c = 0 ; c < n ; c++ )scanf("%d", &array[c]);  for ( c = 0 ; c < ( n - 1 ) ; c++ ) {position = c; for ( d = c + 1 ; d < n ; d++ ) {if ( array[position] > array[d] )position = d; }if ( position != c ) {swap = array[c];array[c] = array[position];array[position] = swap; } } printf("Sorted list in ascending order:\n");for ( c = 0 ; c < n ; c++ )printf("%d\n", array[c]); return 0;}

Page 4: Ds Original

OUTPUT

Page 5: Ds Original

Write a program to implement Quick sort.

#include<stdio.h>void quicksort(int [10],int,int);int main(){int x[20],size,i;printf("Enter size of the array: ");scanf("%d",&size);printf("Enter %d elements: ",size);for(i=0;i<size;i++)scanf("%d",&x[i]);quicksort(x,0,size-1);printf("Sorted elements: ");for(i=0;i<size;i++)printf(" %d\n",x[i]);return 0;}

Page 6: Ds Original

void quicksort(int x[10],intfirst,int last){intpivot,j,temp,i;if(first<last){pivot=first; i=first; j=last;while(i<j){while(x[i]<=x[pivot]&&i<last)i++;while(x[j]>x[pivot])j--;if(i<j){temp=x[i];x[i]=x[j];x[j]=temp; } }

temp=x[pivot];x[pivot]=x[j];x[j]=temp;quicksort(x,first,j-1);

Page 7: Ds Original

quicksort(x,j+1,last);

}}

OUTPUT

Page 8: Ds Original

Create a Stack and apply the push & pop operation.#include <stdio.h>#define MAX 5int top, status;int push (int stack[], int item){ if (top == (MAX-1))

status = 0;else { status = 1;

++top;stack [top] = item;

}return 0;}int pop (int stack[])

Page 9: Ds Original

{int ret;if (top == -1) { ret = 0;

status = 0; }else { status = 1;

ret = stack [top];--top;

}return ret;}

/*FUNCTION TO DISPLAY STACK*/int display (int stack[]){ int i;printf ("\nThe Stack is: ");if (top == -1)

printf ("empty");else { for (i=top; i>=0; --i)

Page 10: Ds Original

printf ("\n--------\n|%3d |\n--------",stack[i]); }printf ("\n");return 0;}

int main(){int stack [MAX], item;intch;top = -1;

do{ do { printf ("\nMAIN MENU");

printf ("\n1.PUSH (Insert) in the Stack");printf ("\n2.POP (Delete) from the Stack");printf ("\n3.Exit (End the Execution)");printf ("\nEnter Your Choice: ");scanf ("%d", &ch);if (ch<1 || ch>3)printf ("\nInvalid Choice, Please try again");

Page 11: Ds Original

}while (ch<1 || ch>3);switch (ch) {case 1:

printf ("\nEnter the Element to be pushed : ");scanf ("%d", &item);printf (" %d", item);push (stack, item);if (status){ printf ("\nAfter Pushing ");display (stack);if (top == (MAX-1))

printf ("\nThe Stack is Full");}elseprintf ("\nStack overflow on Push");break;

case 2:item = pop (stack);if (status){ printf ("\nThe Popped item is %d. After

Popping: ");display (stack);

Page 12: Ds Original

}elseprintf ("\nStack underflow on Pop");break;

default:printf ("\nEND OF EXECUTION"); }

}while (ch != 3);getch();return 0;}

OUTPUT

Page 13: Ds Original

Write a program to create a linked list and insert no.To its beginning.

Page 14: Ds Original

#include<stdio.h>#include<malloc.h>struct node{int data;struct node *link;};int append(struct node**,int);int display(struct node*);intadd_at_beg(struct node**,int );int main(){struct node *head;int n;head=NULL;append(&head,5);append(&head,6);append(&head,7);append(&head,8);display(head);

Page 15: Ds Original

printf(" \n Enter the number you want to insert ");scanf("%d",&n);add_at_beg(&head,n);display(head);getch();return 0;}int append(struct node **q,intnum){struct node *temp,*r;temp=*q;if(temp==NULL) /* If list is empty , create first node */ {temp=(struct node*)malloc(sizeof(struct node));temp->data=num;temp->link=NULL; *q=temp; }else {temp=*q;while(temp->link !=NULL)

Page 16: Ds Original

temp=temp->link;

r=(struct node*)malloc(sizeof(struct node));r->data=num;r->link=NULL;temp->link=r; }return 0;}int display(struct node *start){printf("\n");printf(" The Linked List is :\n"); /* Traverse the entire linked list */while(start != NULL) {printf(" %d",start->data);start=start->link; }

return 0;}

Page 17: Ds Original

intadd_at_beg(struct node**q,int no){struct node *temp;temp=(struct node*)malloc(sizeof(struct node));temp->data=no;temp->link=*q; *q=temp;return 0;}

Page 18: Ds Original

OUTPUT

Page 19: Ds Original

Write a program to create a Binary tree.

#include<stdlib.h>#include<stdio.h>structtree_el {intval;structtree_el * right, * left;};typedefstructtree_el node;int insert(node ** tree, node * item) {if(!(*tree)) { *tree = item;return; }if(item->val<(*tree)->val)insert(&(*tree)->left, item);else if(item->val>(*tree)->val)insert(&(*tree)->right, item);return 0;}

Page 20: Ds Original

int printout(node * tree) {if(tree->left) printout(tree->left);printf("%d\n",tree->val);if(tree->right) printout(tree->right);return 0;}

int main() {node * curr, * root;int i;

root = NULL;

for(i=1;i<=10;i++) {curr = (node *)malloc(sizeof(node));curr->left = curr->right = NULL;curr->val = rand();insert(&root, curr); }printout(root);return 0;

Page 21: Ds Original

}

OUTPUT:

Page 22: Ds Original

Write a program to multiply matrices.

#include <stdio.h>int main(){int m, n, p, q, c, d, k, sum = 0;int first[10][10], second[10][10], multiply[10][10];printf("Enter the number of rows and columns of first matrix\n");scanf("%d%d", &m, &n);printf("Enter the elements of first matrix\n");for ( c = 0 ; c < m ; c++ )for ( d = 0 ; d < n ; d++ )scanf("%d", &first[c][d]);printf("Enter the number of rows and columns of second matrix\n");scanf("%d%d", &p, &q);if ( n != p )printf("Matrices with entered orders can't be multiplied with each other.\n");else

Page 23: Ds Original

{printf("Enter the elements of second matrix\n");for ( c = 0 ; c < p ; c++ )for ( d = 0 ; d < q ; d++ )scanf("%d", &second[c][d]);for ( c = 0 ; c < m ; c++ ) {for ( d = 0 ; d < q ; d++ ){for ( k = 0 ; k < p ; k++ ){sum = sum + first[c][k]*second[k][d]; }multiply[c][d] = sum;sum = 0; }}printf("Product of entered matrices:-\n");for ( c = 0 ; c < m ; c++ ){for ( d = 0 ; d < q ; d++ )printf("%d\t", multiply[c][d]);printf("\n"); }}return 0;}

Page 24: Ds Original

OUTPUT

Page 25: Ds Original

Write a program to calculate factorial of a given no, using recursion.

#include<stdio.h>int factorial(int n);int main(){int n;printf("Enter an positive integer: ");scanf("%d",&n);printf("Factorial of %d = %ld", n, factorial(n));return 0;}int factorial(int n){if(n!=1)return n*factorial(n-1);}

Page 26: Ds Original

OUTPUT

Page 27: Ds Original

Write a program for binary search algorithm.

#include <stdio.h>int main(){intarr[20],start,end,middle,n,i,item;printf("How many elements you want to enter in the array : ");scanf("%d",&n);for(i=0; i < n; i++) {printf("Enter element %d : ",i+1);scanf("%d",&arr[i]); }printf("Enter the element to be searched : ");scanf("%d",&item);start=0;end=n-1;

Page 28: Ds Original

middle=(start+end)/2;while(item != arr[middle] && start <= end) {if(item >arr[middle])start=middle+1;elseend=middle-1;middle=(start+end)/2; }if(item==arr[middle])printf("%d found at position %d\n",item,middle+1);if(start>end)printf("%d not found in array\n",item);return 0;}

Page 29: Ds Original

OUTPUT

Page 30: Ds Original

MADHAV INSTITUTE OF TECHNOLOGY AND SCIENCE

Page 31: Ds Original

DATA STRUCTURE FILE

Submitted to Submitted by

Prof. Abhilash SonkarVirendra S Pawar

Prof. Jamvant S Kumare CS12061

MADHAV INSTITUTE OF TECHNOLOGY AND SCIENCE

Page 32: Ds Original

DATA STRUCTURE FILE

Submitted to Sunbmited by

Prof. Abhilash SonkarVinod Damar

Prof. Jamvant S Kumare CS12058