Data Structure Management

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

DATA STRUCTURE MANAGEMENT TUTORIAL

// Develop a program to insert, delete, edit element in array

#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int array[15]; int no_el,i,choice,pos,key,new_el; clrscr(); printf("Enter the no of element :"); scanf("%d",&no_el); for(i=0;i<no_el;i++) { printf("Enter the element : "); scanf("%d",&array[i]); } while(1) { clrscr(); printf("\n 1. Insert \n 2. delete by value \n 3. edit \n 4. display \n 5. exit"); printf("\n Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1:

printf("Enter the position at which you want to insert : "); scanf("%d",&pos); printf("Enter the new element : "); scanf("%d",&new_el); pos--; for(i=no_el-1;i>=pos;i--) array[i+1]=array[i]; array[pos]=new_el; no_el++; break; case 2: printf("Enter the value to be search : "); scanf("%d",&key); for(pos=0;pos<no_el;pos++) { if(array[pos]==key) break; } if(pos==no_el) { printf("Search key not found"); break; } for(i=pos;i<no_el;i++) array[i]=array[i+1]; no_el--; break;

case 3: printf("Enter the position to be edit : "); scanf("%d",&pos); printf("Enter the new value for old position : "); scanf("%d",&array[pos-1]); break; case 4: printf("\n"); for(i=0;i<no_el;i++) printf("\nThe element is %d: %d",i+1,array[i]); break; case 5: return(0);

} getch(); } } // Write simple programs using pointers and arrays

#include<conio.h> #include<stdio.h>

int main() { char *ptr1 = "Darshak"; char *ptr2 = "Divyesh";

char *ptr3 = "Rajnik";

char* arr[3];

clrscr();

arr[0] = ptr1; arr[1] = ptr2; arr[2] = ptr3;

printf("\n [%s]\n", arr[0]); printf("\n [%s]\n", arr[1]); printf("\n [%s]\n", arr[2]);

getch(); return 0; } // Write simple programs using array of pointer

#include <stdio.h> #include <conio.h> main() { clrscr(); int *array[3]; int x = 10, y = 20, z = 30;

int i; array[0] = &x; array[1] = &y; array[2] = &z; for (i=0; i< 3; i++) { printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]), array[i]); } getch(); return 0; } #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 50

int size;

/* Defining the stack structure */ struct stack { int AR[MAX]; int top; };

/* Initializing the stack(i.e., top=-1) void init_stk(struct stack *st)

*/

{ st->top=-1; }

/* Entering the elements into stack */ void push (struct stack *st,int num) { if(st->top == size-1) { printf("\nStack overflow(i.e., stack full)."); return; }

st->top++; st->AR[st->top] = num; }

//Deleting an element from the stack. int pop(struct stack *st) { int num; if(st->top == -1) { printf("\nStack underflow(i.e., stack empty)."); return NULL; }

num=st->AR[st->top]; st->top--; return num; } void display(struct stack *st) { int i; for(i=st->top;i>=0;i--) printf("\n%d",st->AR[i]); } void main() { int ele,opt,val; struct stack ptr; clrscr(); init_stk(&ptr); printf("\nEnter Stack Size :"); scanf("%d",&size); while(1) { printf("\n\n\tSTACK PRIMITIVE OPERATIONS"); printf("\n1.PUSH"); printf("\n2.POP"); printf("\n3.DISPLAY"); printf("\n4.QUIT"); printf("\n"); printf("\nEnter your option : ");

scanf("%d",&opt); switch(opt) { case 1: printf("\nEnter the element into stack:"); scanf("%d",&val); push(&ptr,val); break; case 2: ele=pop(&ptr); printf("\nThe element popped from stack is : %d",ele); break; case 3: printf("\nThe current stack elements are:"); display(&ptr); break; case 4: getch(); exit(0); default: printf("\nEnter correct option!Try again."); } } } /* Develop an algorithm for insert and delete operations of queue and implement using array and pointer data structures. */

#include<stdio.h> #include<alloc.h> #include<conio.h> #define size 10 #define true 1 #define false 0

struct q_arr { int f,r; int num; int a[size]; };

void init(struct q_arr* queue); int e_que(struct q_arr* queue); int f_que(struct q_arr* queue); int add_ele(struct q_arr* queue,int); int rem_ele(struct q_arr* queue); void display_ele(struct q_arr* queue);

/*main function*/ void main() { int ele,k; int ch;

struct q_arr *queue = (struct q_arr*)malloc(sizeof(struct q_arr)); init(queue);

while(1) { clrscr(); printf("\n\n****IMPLEMENTATION OF QUEUE USING ARRAYS****\n"); printf("============================================"); printf("\n\t\tMENU\n"); printf("============================================"); printf("\n\t[1] To insert an element"); printf("\n\t[2] To remove an element"); printf("\n\t[3] To display all the elements"); printf("\n\t[4] Exit"); printf("\n\n\t Enter your choice: "); scanf("%d",&ch);

switch(ch) { case 1: { printf("\nElement to be inserted:"); scanf("%d",&ele); add_ele(queue,ele); break; }

case 2: { if(!e_que(queue)) { k=rem_ele(queue); printf("\n%d element is removed\n",k); getch(); } else { printf("\tQueue is Empty. No element can be removed."); getch(); } break; }

case 3: { display_ele(queue); getch(); break; }

case 4: exit(0);

default: printf("\tInvalid Choice."); getch(); break; } } } /*end main*/

void init(struct q_arr* queue) { queue->f = 0; queue->r = -1; queue->num = 0; }

/* Function to check is the queue is empty*/ int e_que(struct q_arr* queue) { if(queue->num==0) return true; return false; }

/* Function to check if the queue is full*/ int f_que(struct q_arr* queue) {

if(queue->num == size) return true; return false; }

/* Function to add an element to the queue*/ int add_ele(struct q_arr* queue,int j) { if(f_que(queue)) return false;

if(queue->r == size - 1) queue->r = -1; queue->a[++queue->r] = j; queue->num++; return true; }

/* Function to remove an element of the queue*/ int rem_ele(struct q_arr* queue) { int j; if(e_que(queue)) return -9999; j = queue->a[queue->f++]; if(queue->f == size) queue->f = 0;

queue->num--; return j; }

/* Function to display the queue*/ void display_ele(struct q_arr* queue) { int j; if(e_que(queue)) { printf("Queue is Empty. No records to display."); return; } printf("\nElements present in the Queue are: "); for(j=queue->f;j<=queue->r;j++) printf("%d\t",queue->a[j]); printf("\n"); } #include<stdio.h> #include<malloc.h>

void append(); int del(int); void display(); int insert(); int insertn(); int search(int);

void rev();

int num,loc; char name[20];

struct node { int a; char n[20]; struct node *next; }; struct node *first,*last;

//first=last=NULL; int main() { int ch; clrscr();

do { printf("\n Enter the choice \n 1.Insert\n 2.delete\n 3.Search\n 4.Dispaly\n 5.exit\n \n"); scanf("%d",&ch); switch(ch) { case 1: insert();

break; case 2: printf("\n enter the value \n"); scanf("%d",&num); del(num); break; case 3: printf("\n enter the value to find \n"); scanf("%d",&num); search(num); break; case 4: display(); break; default: break; } }while(ch!=5); return 0; } int insert() { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); printf("\n enter value: "); scanf("%d",&temp->a); printf("\n enter the location: ");

scanf("%s",temp->n); if(first==NULL) { first=temp; first->next=NULL; } else { temp->next=first; first=temp; } display(); return 0; }

int del(int num) { struct node *temp,*m; temp=first;

while(temp!=NULL) { if(temp->a==num) { if(temp==first) { first=temp->next;

free(temp); return; } else { m->next=temp->next; free(temp); return; } } else { m=temp; temp=temp->next; } } }

int search(int num) { struct node *temp; temp=first; while(temp!=NULL) { if(temp->a==num) { printf("%d is exist",num);

printf("\t its name is %s",temp->n); break; } else temp=temp->next;

} if(temp->a!=num) {

printf("not exist"); } }

void display() { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp=first; printf("\n"); while(temp!=NULL) { printf("%d %s\t",temp->a,temp->n);

temp=temp->next; } printf("\n"); }

#include <stdio.h> #include <string.h> #include <dos.h> #include <stdlib.h> struct node { char data[15]; struct node *left, *right; }; void insert(struct node *r, struct node *p) { if ((r->right == NULL) && (strcmp(p->data, r->data) > 0)) r->right = p; else if ((r->right != NULL) && (strcmp(p->data, r->data) > 0)) insert(r->right, p); if ((r->left == NULL) && (strcmp(p->data, r->data) < 0)) r->left = p; else if ((r->left != NULL) && (strcmp(p->data, r->data) < 0)) insert(r->left, p); } void tree(struct node *r, int c) { int top, flag; struct node *w, *stack[20]; if (r != NULL) {

if (c != 4) { if (c == 1) printf(" %s ", r->data); tree(r->left, c); if (c == 2) printf(" %s ", r->data); tree(r->right, c); if (c == 3) printf(" %s ", r->data); } } if (c == 4) { top = 0; w = r; flag = 0; while ((top != - 1) && (w != NULL)) { while ((flag == 0) && (w->left != NULL)) { stack[top] = w; top++; w = w->left; } printf(" %s ", w->data); if (w->right != NULL)

{ w = w->right; flag = 0; } else { top--; w = stack[top]; flag = 1; } } } } void main() { int choice, c, i, flag; char temp = 'N', temp1[15]; struct node *s, *root, *r, *q;

clrscr(); root = NULL; do { printf("\n 1. Enter"); printf("\n 2. Delete "); printf("\n 3. Search "); printf("\n 4. Display");

printf("\n 5. Exit"); printf("\nEnter Your Choice : "); scanf("%d", &choice); switch (choice) { case 1: printf("***** Data Entry ***** "); do { s = malloc(sizeof(struct node)); s->left = NULL; s->right = NULL; printf("\nEnter Data : "); scanf("%s", &s->data); if (root == NULL) root = s; else insert(root, s); printf("\nEnter Your Elements[y/n] : "); scanf("%c", &temp); } while (temp == 'y'); break; case 2: printf("****** Delete Operation *******\n"); do {

printf("\nEnter Element To Be Deleted : "); scanf("%s", temp1); s = root; i = 0; flag = 0; do { if (strcmp(s->data, temp1) > 0) { r = s; s = s->left; i = 2; } if (strcmp(s->data, temp1) == 0) { flag = 1; if (i == 0) { if (root->right != NULL) { q = root->left; root = root->right; insert(root, q); } if (root->right == NULL) root = root->left; }

else { if (i == 1) { q = s->left; r->right = s->right; if (s->left != NULL) insert(r, q); } if (i == 2) { q = s->right; r->left = s->left; if (s->right != NULL) insert(r, q); } } } } while (flag == 0 && s != NULL); printf("\n Delete Any More[Y/N] : "); scanf("%c", &temp); } while (temp == 'y') ; break; case 3:

printf("****** Search Operation *******\n"); do { printf("\n Enter Name To Be Searched"); scanf("%s", temp1); i = 0; s = root; while (s != NULL && i == 0) { if (strcmp(s->data, temp1) < 0)s = s>right; if (strcmp(s->data, temp1) > 0) s = s->left; if (strcmp(s->data, temp1) == 0) i = 1; } if (i == 0) printf("\nElement Not Found\n"); else printf("\nElement Found\n"); printf("\nEnter More Elements[Y/N] : "); scanf("%c", &temp); } while (temp == 'y') ; break; case 4: do

{ printf( "\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4. Non Recursion \n 5. Exit"); printf("\nEnter Your Choice : "); scanf("%d", &c); if (root == NULL) printf("Tree Not Started Yet"); else tree(root, c); printf("\n Press Any Key To Continue......"); getch(); } while (c != 5); break; } } while (choice != 5) ; } // sequential search

#include<stdio.h>

void main() { int arr[50],k,i,l,pos=0;

clrscr(); printf("Enter limit For SEQUENTIAL SEARCH :- "); scanf("%d",&l); printf("\nEnter %d elements\n",l); for(i=0;i<l;i++) { scanf("%d",&arr[i]); } printf("\nEnter a number to be search:- "); scanf("%d",&k); for(i=0;i<l;i++) { if(arr[i]==k) { pos=i+1; break; } } if(pos!=0) printf("%d is found in the list, at position %d\n",k,pos); else printf("%d is not in the list\n",k); getch(); } #include<stdio.h> #include<conio.h>

void main() { int a[10],n,i,j,temp; int beg,end,mid,target; clrscr(); printf("enter the total numbers:"); scanf("%d",&n); printf("enter the array elements: "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(a[j+1]<a[j]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("the sorted numbers are:"); for(i=0;i<n;i++) printf(" %d",a[i]);

beg=0;

end=n-1; mid=(beg+end)/2; printf("\nEnter the number to be searched:"); scanf("%d",&target); while(beg<=end && a[mid]!=target) { if(target<a[mid]) end=mid-1; else beg=mid+1; mid=(beg+end)/2; } if(a[mid]==target) { printf("\nThe number is found at position %d",mid+1); } else { printf("\nThe number is not found"); } getch(); }
Program of sorting using quick sort through recursion

Code:
/*Program of sorting using quick sort through recursion*/ #include<stdio.h> #include<conio.h> #define MAX 30 enum bool { FALSE,TRUE }; void main()

{ int a[MAX],n,i; clrscr(); printf("Enter the number of elements : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&a[i]); } printf("Unsorted list is :\n"); display(a,0,n-1); printf("\n"); quick(a,0,n-1); printf("Sorted list is :\n"); display(a,0,n-1); printf("\n"); }/*End of main() */ quick(int arr[],int low,int up) { int piv,temp,left,right; enum bool pivot_placed=FALSE; left=low; right=up; piv=low; /*Take the first element of sublist as piv */ if(low>=up) return; printf("Sublist : "); display(arr,low,up); /*Loop till pivot is placed at proper place in the sublist*/ while(pivot_placed==FALSE) { /*Compare from right to left */ while(arr[piv]<=arr[right] && piv!=right) right--; if( piv==right ) pivot_placed=TRUE; if( arr[piv] > arr[right] ) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } /*Compare from left to right */ while( arr[piv]>=arr[left] && left!=piv ) left++; if(piv==left) pivot_placed=TRUE; if( arr[piv] < arr[left] ) { temp=arr[piv]; arr[piv]=arr[left];

arr[left]=temp; piv=left; } }/*End of while */ printf("-> Pivot Placed is %d -> ",arr[piv]); display(arr,low,up); printf("\n"); quick(arr,low,piv-1); quick(arr,piv+1,up); }/*End of quick()*/ display(int arr[],int low,int up) { int i; for(i=low;i<=up;i++) printf("%d ",arr[i]); getch(); } Program of sorting using radix sort

Code:
/*Program of sorting using radix sort*/ # include<stdio.h> # include<malloc.h> struct { node

int info ; struct node *link; }*start=NULL; main() { struct node *tmp,*q; int i,n,item; printf("Enter the number of elements in the list : "); scanf("%d", &n); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&item); /* Inserting elements in the linked list */ tmp= malloc(sizeof(struct node)); tmp->info=item; tmp->link=NULL; if(start==NULL) /* Inserting first element */ start=tmp; else { q=start; while(q->link!=NULL) q=q->link; q->link=tmp; } }/*End of for*/

printf("Unsorted list is :\n"); display(); radix_sort(); printf("Sorted list is :\n"); display (); }/*End of main()*/ display() { struct node *p=start; while( p !=NULL) { printf("%d ", p->info); p= p->link; } printf("\n"); }/*End of display()*/ radix_sort() { int i,k,dig,maxdig,mindig,least_sig,most_sig; struct node *p, *rear[10], *front[10]; least_sig=1; most_sig=large_dig(start); for(k = least_sig; k <= most_sig ; k++) { printf("PASS %d : Examining %dth digit from right for(i = 0 ; i <= 9 ; i++) { rear[i] = NULL; front[i] = NULL ; } maxdig=0; mindig=9; p = start ; while( p != NULL) { /*Find kth digit in the number*/ dig = digit(p->info, k); if(dig>maxdig) maxdig=dig; if(dig<mindig) mindig=dig; /*Add the number to queue of dig*/ if(front[dig] == NULL) front[dig] = p ; else rear[dig]->link = p ; rear[dig] = p ; p=p->link;/*Go to next number in the list*/ }/*End while */ /* maxdig and mindig are the maximum amd minimum digits of the kth digits of all the numbers*/ printf("mindig=%d maxdig=%d\n",mindig,maxdig); /*Join all the queues to form the new linked list*/ start=front[mindig]; for(i=mindig;i<maxdig;i++)

",k,k);

{ if(rear[i+1]!=NULL) rear[i]->link=front[i+1]; else rear[i+1]=rear[i]; } rear[maxdig]->link=NULL; printf("New list : "); display(); }/* End for */ }/*End of radix_sort*/ /* This function finds number of digits in the largest element of the list */ int large_dig() { struct node *p=start ; int large = 0,ndig = 0 ; while(p != NULL) { if(p ->info > large) large = p->info; p = p->link ; } printf("Largest Element is %d , ",large); while(large != 0) { ndig++; large = large/10 ; } printf("Number of digits in it are %d\n",ndig); return(ndig); } /*End of large_dig()*/ /*This function returns kth digit of a number*/ int digit(int number, int k) { int digit, i ; for(i = 1 ; i <=k ; i++) { digit = number % 10 ; number = number /10 ; } return(digit); }/*End of digit()*/ Program of sorting using selection sort

Code:
/*Program of sorting using selection sort*/ #include<stdio.h> #include<conio.h> #define MAX 20 void main() { int arr[MAX],i,j,k,n,temp,smallest; clrscr(); printf("Enter the number of elements : ");

scanf("%d",&n); for (i = 0; i < n; i++) { printf("Enter element %d : ",i+1); scanf("%d", &arr[i]); } printf("Unsorted list is : \n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); /*Selection sort*/ for(i=0;i<n-1;i++) { /*Find the smallest element*/ smallest = i; for(k = i + 1; k < n ; k++) { if(arr[smallest] > arr[k]) smallest = k ; } if(i!=smallest) { temp=arr[i]; arr[i]=arr[smallest]; arr[smallest] = temp ; } printf("After Pass %d elements are : for (j = 0; j < n; j++) printf("%d ", arr[j]); printf("\n"); }/*End of for*/ printf("Sorted list is : \n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); getch(); }/*End of main()*/ Program of sorting using shell sort

",i+1);

Code:
/* Program of sorting using shell sort */ #include <stdio.h> #define MAX 20 main() { int arr[MAX], i,j,k,n,incr; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Unsorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\nEnter maximum increment (odd value) : ");

scanf("%d",&incr); /*Shell sort*/ while(incr>=1) { for(j=incr;j<n;j++) { k=arr[j]; for(i = j-incr; i >= 0 && k < arr[i]; i = i-incr) arr[i+incr]=arr[i]; arr[i+incr]=k; } printf("Increment=%d \n",incr); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); incr=incr-2; /*Decrease the increment*/ }/*End of while*/ printf("Sorted list is :\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); }/*End of main()*/ Heap Sort

Code:
/* HEAP SORT */ /* HEAP.C */ # include<stdio.h> void heap_sort(int *, int ); void create_heap(int *, int); void display(int *, int); /* Definition of the function */

void create_heap(int list[], int n ) { int k, j, i, temp; for(k = 2 ; k <= n; ++k) { i = k ; temp = list[k]; j = i / 2 ; while((i > 1) && (temp > list[j])) { list[i] = list[j]; i = j ; j = i / 2 ; if ( j < 1 ) j = 1 ; } list[i] = temp ; } }

/* End of heap creation function */ /* Definition of the function */ void heap_sort(int list[], int n) { int k, temp, value, j, i, p; int step = 1; for(k = n ; k >= 2; --k) { temp = list[1] ; list[1] = list[k]; list[k] = temp ; i = 1 ; value = list[1]; j = 2 ; if((j+1) < k) if(list[j+1] > list[j]) j ++; while((j <= ( k-1)) && (list[j] > value)) { list[i] = list[j]; i = j ; j = 2*i ; if((j+1) < k) if(list[j+1] > list[j]) j++; else if( j > n) j = n ; list[i] = value; } /* end of while statement */ printf("\n Step = %d ", step); step++; for(p = 1; p <= n; p++) printf(" %d", list[p]); } /* end for loop */ } /* Display function */ void display(int list[], int n) { int i; for(i = 1 ; i <= n; ++ i) { printf(" %d", list[i]); } } /* Function main */ void main() { int list[100]; int i, size = 13 ; printf("\n Size of the list: %d", size);

for(i = 1 ; i <= size ; ++i) { list[i] = rand() % 100; } printf("\n Entered list is as follows:\n"); display(list, size); create_heap(list, size); printf("\n Heap\n"); display(list, size); printf("\n\n"); heap_sort(list,size); printf("\n\n Sorted list is as follows :\n\n"); display(list,size); }

OUTPUT:

Size of the list: 13 Entered list is as follows: 41 67 34 0 69 24 78 58 Heap 81 67 78 62 64 69 34 0

62 58

64 41

5 5

45 24

81 45

Step Step Step Step Step Step Step Step Step Step Step Step

= = = = = = = = = = = =

1 2 3 4 5 6 7 8 9 10 11 12

78 67 69 62 64 45 34 0 58 41 5 24 81 69 67 45 62 64 24 34 0 58 41 5 78 81 67 64 45 62 41 24 34 0 58 5 69 78 81 64 62 45 58 41 24 34 0 5 67 69 78 81 62 58 45 5 41 24 34 0 64 67 69 78 81 58 41 45 5 0 24 34 62 64 67 69 78 81 45 41 34 5 0 24 58 62 64 67 69 78 81 41 24 34 5 0 45 58 62 64 67 69 78 81 34 24 0 5 41 45 58 62 64 67 69 78 81 24 5 0 34 41 45 58 62 64 67 69 78 81 5 0 24 34 41 45 58 62 64 67 69 78 81 0 5 24 34 41 45 58 62 64 67 69 78 81

Sorted list is as follows : 0 5 24 34 41 45 58 62 64 67 Press any key to continue 69 78 81

Bubble Sort

Code:
/* bubble.c */ #include <stdio.h> #include <stdlib.h> void bubble_sort(int array[], int size) {

int temp, i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) if (array[i] < array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } void main(void) { int values[30], i; printf("\n Unsorted list is as follows\n"); for (i = 0; i < 10; i++) { values[i] = rand() % 100; printf(" %d", rand()%100); } bubble_sort(values, 10); printf("\n Sorted list is as follows\n"); for (i = 0; i < 10; i++) printf("%d ", values[i]); }

OUTPUT: Unsorted list is as follows 67 0 24 58 64 45 27 91 42 36 Sorted list is as follows 27 34 41 61 62 69 78 81 95 Press any key to continue Insertion Sort

Code:
/* Insertion sort */ /* INSERT.C */ # include<stdio.h> # include<stdlib.h> void insertion_sort(int *, int); void display(int*, int); void insertion_sort(int l[], int n) { int pointer, temp; int i, k; l[0] = -0; for(i = 1 ; i <= n; i++) { pointer = i -1; temp = l[i]; while(temp < l[pointer])

{ l[pointer+1] = l[pointer]; pointer --; } l[pointer+1] = temp; printf("Step = %d", i); for(k = 0; k <= n; k++) printf(" %d", l[k]); printf("\n"); } } void display(int l[], int n) { int i; printf("\n Sorted list is as follows\n"); for(i = 1; i <= n; i++) printf(" %d", l[i]); } void main() { int number = 10; int list[100]; int i; printf("\n Number of elements in the list: %i", number); printf("\n Unsorted list is as follows \n"); for(i = 1; i <= number; i++) { list[i] = rand() %100; printf(" %d", rand() %100); } printf("\n"); printf("\n Step wise result is as follows \n\n"); insertion_sort(list,number); display(list, number); }

OUTPUT:

Number of elements in the list: 10 Unsorted list is as follows 67 0 24 58 64 45 27 91 42 36 Step wise result is as follows Step Step Step Step Step Step Step Step Step = = = = = = = = = 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 41 34 69 78 62 5 34 41 69 78 62 5 34 41 69 78 62 5 34 41 69 78 62 5 34 41 62 69 78 5 5 34 41 62 69 78 5 34 41 62 69 78 5 34 41 61 62 69 5 34 41 61 62 69 81 81 81 81 81 81 81 78 78 61 61 61 61 61 61 61 81 81 95 95 95 95 95 95 95 95 95 27 27 27 27 27 27 27 27 27

Step = 10 0 5 27 34 41 61 62 69 78 81 95 Sorted list is as follows 5 27 34 41 61 62 69 78 81 95Press any key to continue Merge Sort

Code:
/* MERGE SORT */ /* merge.c */ # include<stdio.h> # include<stdlib.h> void merge_sort(float *, int , int , int ); void merge_pass(float *, int , int ); /* Definition of the function */ void merge_sort(float l[], int top, int size, int bottom) { float temp[1000]; int f = top; int s = size +1 ; int t = top ; int upper; while(( f <= size)&&(s <=bottom)) { if(l[f] <= l[s]) { temp[t] = l[f] ; f ++; } else { temp[t] = l[s] ; s ++; } t ++; } if(f <=size) { for(f = f ; f<=size; f++) { temp[t] = l[f]; t++; } } else { for(s = s ; s<= bottom ; s++) { temp[t]=l[s]; t++; } }

for(upper = top; upper <= bottom ; upper++) { l[upper]=temp[upper]; } } /* Definition of the function */ void merge_pass( float append[], int m, int n ) { if( m!= n) { int mid = (m+n)/2; merge_pass( append, m, mid ); merge_pass( append, mid+1, n ); merge_sort( append, m, mid, n ); } } /* main function */ void main() { float list[1000]; int i, n = 30; printf("\n Size of the list: %d", n); for(i = 0 ; i < n ; i++) { list[i] = (float) (rand() %100); } printf("\n Entered list as follows:\n"); for( i = 0 ; i<n ; i++) printf(" %d ", (int) list[i]); i = 0 ; merge_pass(list, i, n-1); printf("\n Merge sorted list is as follows:\n"); for( i = 0 ; i<n ; i++) printf(" %d", (int) list[i]); }

OUTPUT:

Size of the list: 30 Entered list as follows: 41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36 9 1 4 2 53 92 82 21 16 18 95 Merge sorted list is as follows: 0 2 4 5 16 18 21 24 27 27 34 36 41 42 45 53 58 61 62 64 67 69 78 81 82 91 91 92 95 95

Press any key to continue

1.

#include<conio.h> #include<stdio.h> void main() { char a[25],b[25],j[100]; int i,c,l,pos,k; clrscr(); printf("Enter Your choice \n1.Find Lenght of String\n2.Copy One string to other\n3.Concate two string\n4.Find substring \n\n Your Choice:-"); scanf("%d",&c); switch(c) { case 1: printf("Enter String:- "); scanf("%s",a); i=0; while(a[i]!='\0') i++; //counts no of chars till encountering null char printf("string length=%d",i); break; case 2: printf("Enter String:- "); scanf("%s",a);

for(i=0;b[i]!='\0';i++) b[i]=a[i]; b[i]='\0'; printf("Copied Stringb = %s",b); break; case 3: printf("Enter first String:- "); scanf("%s",a); printf("\nEnter second String:- "); scanf("%s",b); i=0;l=0;

while(a[i]) { j[i]=a[i]; ++i; } j[i]=' '; ++i;

while(b[l]) { j[i]=b[l]; ++i; ++l; } j[i]='\0';

printf("\nconcated string= "); printf(j); break;

case 4: printf("Enter Sting= "); scanf("%s",a); printf("Enter Positon and lenght of string: "); scanf("%d%d",&pos,&l);

for(i=0;a[i]!='\0';i++); if(pos>i) { printf("Invalid Position!!"); getch(); exit (0); } c=i-pos+1; if(l>c) { printf("Invalid SubString"); getch(); exit (0); } c=0; c=pos-1; for(k=0;k<l;k++)

{ j[k]=a[c]; c++; } j[k]='\0'; printf("substring= %s",j); break; case 5: break; default: printf("Invalid Selection"); break; } getch(); }

You might also like