Вы находитесь на странице: 1из 34

EX.NO SUM OF THREE NUMBERS AIM: To write a C++ program to find the sum of the given number .

ALGORITHM: a. b. c. d. e. f. g. Start the program. Declare the class Declare the functions getdata(),add(),putdata(). Get the values using getdata(). Execute the statement using add(). Display the sum using putdata(). Stop the program.

DATE:

PROGRAM: #include<iostream.h> #include<conio.h> class addition { int a,b,c; public: void getdata(); void add(); void putdata(); }; void addition::getdata() { cout<<\nEnter Three Numbers\n; cin>>a>>b>>c; } void addition::add() { sum=a+b+c; } void addition::putdata() { cout<<\nThe sum is<<sum; } main() { clrscr(); addition m; m.getdata(); m.add(); m.putdata(); getch(); return 0; } EX.NO MAXIMUM OF THREE NUMBERS DATE:

AIM: To write a program to find the maximum number using oops concept. ALGORITHM: a. Start the program. b. Declare the class. c. Declare the functions getdata() & great(). d. Get the values using the getdata(). e. Use the conditional statements to find the greatest number in the great () fuction and display the result. f. Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> class grt { int a,b,c; public: void getdata(); void great(); }; void grt::getdata() { cout<<Enter the values of a,b&c; cin>>a>>b>>c; } void grt::great() { if(a>b&&a>c) { cout<<A is the greatest number; } else if(b>a&&b>c) { cout<<\nB is the greatest number; } else { cout<<\nC is the greatest number; } void main() { int a,b,c; clrscr(); grt a; a.getdata(); a.great(); getch(); }

EX.NO. FIBONACCI SERIES AIM:

DATE:

To write a program to find the Fibonacci series using oops concept. ALGORITHM: a. b. c. d. e. Start the program. Declare the class. Use the for loop to find the Fibonacci series in the function. Display the result using another function. Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> class Fibonacci { int i,x,y=0,z=1; public: void fibo(); void disp(); }; void Fibonacci::fibo() { for(i=0;i<10;i++) { x=y; y=z; z=x+z; } { void Fibonacci::disp() { cout<<\nThe Fibonacci series is\n<<y<<\n<<z<<\n; } void main() { clrscr(); Fibonacci f; f.fibo(); f.disp(); getch(); return 0; }

EX.NO.

DATE:

FACTORIAL OF A NUMBER AIM: To write a program to find the factorial of a number. ALGORITM: a. b. c. d. e. f. Start the program. Declare the class and functions. Get the number . Find the factorial using for loop. Display the result. Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> class factorial { int a,fact; public: void getdata() { cout<<Enter the value of a:; cin>>a; } void factl() { for(i=0;i<=a;i++) { fact=fact*i; int fact=1; } void disp() { cout<<\nThe factorial of the number is <<fact; } }; void main() { int a,fact; factorial f; f.getdata(); f.factl(); f.disp(); getch(); return 0; } EX.NO. ARMSTRONG NUMBER DATE:

AIM: To write a program to find the Armstrong number. ALGORITHM: a. b. c. d. e. Start the program. Declare the class,variables and function. Using the conditional statements in the function. Print the result from the statement Stop the program.

PROGRAM: #include<iostream.h> #include<conio.h> #include<math.h> class Armstrong { int a,b,c,n,ans; public: void getdata() { cout<<Enter the number:; cin>>n; } void arms() { a=n; ans=0; while(a>0) { b=a%10; ans+=(b*b*b); a=a/10; } void disp() { if(ans==n) cout<<\nThe entered number is armstrong; else cout<<\nThe entered number is not an armstrong; } }; void main() { clrscr(); Armstrong a; a.getdata(); a.arms(); a.disp(); getch(); return 0; }

EX.NO. ARRAY IMPEMENTATION OF LIST ADT AIM: To write a C program to perform array implementation of List ADT ALGORITHM:

DATE:

Step1: Define the list structure and read the choice of operation to be performe d. Step2: Insert: a) Read the element x to be inserted and position p. b) Check whether the last position of array is greater than maximum array s ize. If so, print the list is full. c) Else, check whether position p fall within the specified array range. If not, print that position does not exist. d) Otherwise, shift elements one position down starting at position p and p lace the element x at position p. Step3: Locate: a) Traverse the entire array b) Search the element x in the array c) If element x is found then return the position of the element d) Else, return zero. Step4: Delete: a) Read the element x to be deleted b) Traverse the array until the element to be removed is found. c) Delete the element x in that position. d) Shift the remaining elements after the deleted element one position up. Step5: Display: a) Traverse the entire array b) Display all elements in the list PROGRAM: LI.H #include<stdio.h> #include<conio.h> int a[25], max; void create(); void insert();

void remov(); void find(); void create() { int c,i; printf("enter how many elements are to be entered in an array"); scanf("%d",&c); for(i=0;i<c;i++) { scanf("%d", &a[i]); } max=c; } void insert() { int i,c,d; printf("enter the number to be inserted & position"); scanf("%d %d",&c,&d); for(i=max;i>=d;i--) a[i+1]=a[i]; i=i+1; a[i]=c; max=max+1; } void remov() { int i,j,x,c; printf("enter the element to be removed"); scanf("%d",&x); for(i=0;i<max; i++) { if(a[i]==x) { c=max; break; } else if(i==max-1) { printf("element not found"); c=max; } } for(j=i;j<max;j++) { a[j]=a[j+1]; } max=c-1; } void find() { int i,y,b=0; printf("enter the element"); scanf("%d",&y); for(i=0;i<max;i++) { if( a[i]==y) { printf("element is found in position %d",i); b=1;

}} if(b==0) { printf("the element is not found"); }} void display() { int i; printf("the element in array\n"); for(i=0;i<max;i++) { printf("%d\n" ,a[i]); } }

LISTARRAY.C #include"li.h" void main() { clrscr(); create(); display(); insert(); display(); remov(); display(); find(); display(); getch(); }

EX.NO. LINKED LIST IMPEMENTATION OF LIST ADT AIM:

DATE:

To write a C program to perform linked list implementation of List ADT ALGORITHM: Step1: Define the node structure and read the choice of operation to be performe d. Step2: If the choice is to insert at first, then, a) Create a new node and add data to the data field. b) Assign the address of head to the address field of new node. c) Assign new node to head. Step3: If the choice is to insert at position p, then, a) Assign some temporary node temp to the head. b) Create a new node and add data to the data field. c) Traverse the list till the given position is reached. d) Temporary node temp next field is assigned to temporary node temp1 next fiel d. e) Temporary node temp1 address field must be changed to address of previous node f) Delete the temporary node temp. Step4: If the choice is locate, then, a) Read the element x to be searched b) Traverse the entire list c) If element x matches with the data field of any node, then return its po sition. d) Else, return that element is not found. Step5: If the choice is delete, then, a) Assign some temporary node temp and traverse the list till the given pos ition is reached. b) Delete the element in the data field of the current node. c) Assign temp1 next field to temp next field. Step6: Display: a) Traverse the entire list b) Display all elements in the list

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

#include<stdlib.h> int i; struct node { int data; struct node *next; }*head,*temp,*temp1; void create() { printf("\n Linear link list creation\n"); printf("Enter element :"); if(head->next==NULL) { temp=(struct node *)malloc(sizeof(struct node)); scanf("%d",&temp->data); head->next=temp; temp->next=NULL; } else { temp1=(struct node *)malloc(sizeof(struct node)); scanf("%d",&temp1->data); temp1->next=NULL; temp=temp1; } } void display() { temp=head; if((temp->next)==NULL) { printf("\n List is empty"); } else { printf("\t\t\n Elements in list\n\n"); while(temp->next!=NULL) { printf("->%d",temp->next->data); temp=temp->next; } }} void delet() { int p; temp=head; printf("\n Enter position to be deleted\n"); scanf("%d",&p); for(i=1;i<p;i++) temp=temp->next; temp->next=temp->next->next; } void insert() { int p; int c; printf("\n Linear Link List Insert\n"); temp1=(struct node *)malloc(sizeof(struct node)); printf("\n To insert 1.first 2.last 3.anywhere\n:"); scanf("%d",&c);

printf("Enter element to be inserted\n"); scanf("%d",&temp1->data); switch(c) { case 1: temp1->next=head->next; head->next=temp1; break; case 2: temp=head->next; while(temp->next!=NULL) temp=temp->next; temp->next=temp1; temp1->next=NULL; break; case 3: printf("Enter position\n"); scanf("%d",&p); temp=head->next; for(i=1;i<p;i++) temp=temp->next; temp1->next=temp->next; temp->next=temp1; } }

void locate() { int n,flag=0; temp=head; printf("\n Enter the element to be located\n"); scanf("%d",&n); for(i=0;temp->next!=NULL;i++) { temp=temp->next; if(temp->data==n) { printf("\n Element is at position %d\n",i); flag=1; } } if(flag==0) { printf("Element not found\n"); } }

#include"linked.h" void main() { int ch; void create(); void display(); void delet(); void locate(); clrscr(); head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; while(1) { printf("\n \n 1.create\n 2.display\n 3.delete\n4.insert\n 5.locate\n6.exit"); printf("\n\n Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: display(); break; case 3: delet(); break; case 4: insert(); break; case 5: locate(); break; case 6:

EX.NO. CURSOR IMPEMENTATION OF LIST ADT AIM: To write a C program to perform cursor implementation of List ADT ALGORITHM:

DATE:

Step1: Define the node structure and read the choice of operation to be performe d. Step2: If the choice is to insert then, the first node (after the header ) is removed from the freelist and the element is inserted to this node. Step3: If the choice is locate, then, a) Read the element x to be searched b) Traverse the cursorspace c) If element x matches with the data field of any node, then return its po sition. d) Else, return that element is not found. Step5: If the choice is delete then, the deleted node is added at the front of t he freelist. Step6: Display: a) Traverse the cursorspace b) Display all elements in the cursorspace

PROGRAM: #include<stdio.h> #include<conio.h> struct node { int element; int next; }; int position,list; struct node cursorspace[10]; void initialisecursor() { int i; for(i=0;i<9;i++) { cursorspace[i].element=0; cursorspace[i].next=i+1; }

cursorspace[9].element=0; cursorspace[9].next=-1; } int cursoralloc() { int p; p=cursorspace[0].next; cursorspace[0].next=cursorspace[p].next; cursorspace[p].element=-1; cursorspace[p].next=-1; return p; } void cursorfree(int p) { cursorspace[p].next=cursorspace[0].next; cursorspace[0].next=p; cursorspace[p].element=0; } int islast(int p) { if(cursorspace[p].next==-1) return 1; else return 0; } int isempty(int l) { if(cursorspace[l].next==-1) return 1; else return 0; } int find(int x,int l) { int p; p=cursorspace[l].next; while(p!=-1&&cursorspace[p].element!=x) p=cursorspace[p].next; return p; } int findprevious(int x,int l) { int p; p=l; while(p!=-1&&cursorspace[cursorspace[p].next].element!=x) p=cursorspace[p].next; return p; } void delete(int x,int l) { int p,tmpcell; p=findprevious(x,l); if(!islast(p)) { tmpcell=cursorspace[p].next; cursorspace[p].next=cursorspace[tmpcell].next; cursorfree(tmpcell); } } void insert(int x,int p)

{ int tmpcell; tmpcell=cursoralloc(); if(tmpcell==-1) printf("out of space"); else if(cursorspace[p].element==0) printf("\nPosition is not in the list"); else { cursorspace[tmpcell].element=x; cursorspace[tmpcell].next=cursorspace[p].next; cursorspace[p].next=tmpcell; } } void display() { int i; printf("\n Slot Element Next\n"); for(i=0;i<10;i++) { printf("%d\t%d\t%d\t",i,cursorspace[i].element,cursorspace[i].next); printf("\n"); } } void main() { int ch; int l=-1,p,x; clrscr(); while(1) { printf("\n1.Create"); printf("\n2.Insert"); printf("\n3.Find"); printf("\n4.Delete"); printf("\n5.Display"); printf("\n6.Exit"); printf("\nEnter ur choice"); scanf("%d",&ch); switch(ch) { case 1: if(l==-1) { initialisecursor(); l=1; printf("\nList is Created Successfully"); } else printf("\nList is already created"); break; case 2: if(l==-1) printf("\nList is not yet initialised"); else { printf("Enter the Element to insert"); scanf("%d",&x); insert(x,1); }

break; case 3: if(l==-1) printf("\nList is not yet initialised"); else { printf("\nEnter which element to find"); scanf("%d",&x); p=find(x,l); if(p==-1) printf("\nThe Element is not in the list"); else printf("\nThe Element is in %d position",p); } break; case 4: if(l==-1) printf("\nList is not yet initialised"); else { printf("Enter the element to delete:"); scanf("%d",&x); p=find(x,l); if(p==-1) printf("\nThe Element is not in the list"); else delete(x,l); } break; case 5: display(); break; case 6: exit(0); default: printf("\nWrong choice"); } } }

EX.NO. ARRAY IMPEMENTATION OF STACK ADT AIM: To write a C program to perform array implementation of Stack ADT ALGORITHM: Step1: Define the stack structure and read the choice of operation to be performed. Step2: If the choice is to push then, a) Read the element x to be inserted

DATE:

b) Check whether the pointer current top position is greater than maximum sta ck size. If so print stack is full. c) Else, increment the top and place the element x at the top position. Step3: If the choice is to pop then, a) If top position is less than zero then print stack is empty b) Delete the element in top position c) Decrement the top position. Step4: Display: a) From start position, display all elements in the stack

PROGRAM: #include<stdio.h> #include<conio.h> int i,num,top=0,stack[10]; void push() { if(top==10) { printf("The stack is full"); } else { printf("Enter the number\n"); scanf("%d",&num); stack[top]=num; top=top+1; } } void pop() { if(top==10) { printf("The stack is full"); } else { num=stack[top-1]; top=top-1; printf("the popped number is %d\n",num); } } void display() { if(top==10) { printf("The stack is full"); } else { for(i=0;i<top;i++) { printf("%d\n",stack[i]); }

}} Stackarray.c #include"stack.h" void main() { int choice; void push(),pop(),display(); clrscr(); for(;;) { printf("\n 1.Push"); printf("\n2.Pop"); printf("\n3.Display"); printf("\n4.Exit"); printf("\nEnter your choice"); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); break; default: printf("\nEnter the choice from menu"); } } }

EX.NO.8 LINKED LIST IMPEMENTATION OF STACK ADT AIM:

DATE:

To write a C program to perform linked list implementation of Stack ADT ALGORITHM: Step1: Define the stack structure and read the choice of operation to be performed. Step2: If the choice is to push then,

a) Read the element x to be inserted b) Check whether the pointer current top position is greater than maximum sta ck size. If so print stack is full. c) Else, increment the top and place the element x at the top position. Step3: If the choice is to pop then, a) If top position is less than zero then print stack is empty b) Delete the element in top position c) Decrement the top position. Step5: Display: a) From start position, display all elements in the stack

PROGRAM: STACK.H #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node { int data; struct node *link; }; struct node *cur,*first; void void void void create(); push(); pop(); display();

void create() { printf("\nENTER THE FIRST ELEMENT: "); cur=(struct node *)malloc(sizeof(struct node)); scanf("%d",&cur->data); cur->link=NULL; first=cur; } void display() { cur=first; printf("\n"); while(cur!=NULL) { printf("%d\n",cur->data); cur=cur->link; } } void push() { printf("\nENTER THE NEXT ELEMENT: "); cur=(struct node *)malloc(sizeof(struct node)); scanf("%d",&cur->data);

cur->link=first; first=cur; } void pop() { if(first==NULL) { printf("\nSTACK IS EMPTY\n"); } else { cur=first; printf("\nDELETED ELEMENT IS %d\n",first->data); first=first->link; free(cur); } } LINKEDST.C #include "stack.h" void main() { int ch; clrscr(); while(1) { printf("\n\n 1.CREATE \n 2.PUSH \n 3.POP \n 4.EXIT \n"); printf("\n ENTER YOUR CHOICE : "); scanf("%d",&ch); switch(ch) { case 1: create(); display(); break; case 2: push(); display(); break; case 3: pop(); display(); break; case 4: exit(0); } } } EX.NO. BALANCED PARANTHESIS USING ARRAY IMPLEMENTATION OF STACK ADT AIM: To write a C program to check the balanced paranthesis using stack ALGORITHM: Step1: Define the stack structure and read the expression.

DATE:

Step2: While not reaching the null character ( \0 ) recursively process the following a) If the character is open paranthesis ( then push the character into the to p of the stack. b) If the character is close paranthesis ) then pop the topmost element from the stack. c) If the return value is 1 then exit from the loop. d) Increment the loop by 1. Step3: If the stack is empty and the return value is not equal to 1 then display as Balanced paranthesis else display as Not Balanced paranthesis.

PROGRAM: BAL.H #include<stdio.h> #include<conio.h> #include<process.h> #define max 20 struct stack { int top; char a[max]; }s; void push(char n) { if(s.top==max) { printf("\n Stack is full\n"); } else { s.top++; s.a[s.top]=n; } } char pop() { int t; if(s.top<0) return 1; else s.top--; return 0; }

BALANCE.C #include<stdio.h> #include<conio.h> #include "bal.h" void main() { int i,x; char a[20]; clrscr(); s.top = -1; printf("Enter any expression\n"); scanf("%s",&a); i=0; while(a[i]!= \0 ) { if(a[i]== ( ) push( ( ); if(a[i]== ) ) x=pop(); if(x==1) break; i++; } if((s.top<0)&&(x!=1)) printf("\t Balanced Paranthesis\n"); else printf("\t Not Balanced Paranthesis\n"); getch(); }

EX.NO. ARRAY IMPLEMENTATION OF QUEUE ADT

DATE:

AIM: To write a C program to perform array implementation of Queue ADT ALGORITHM: Step1: Define the queue structure and read the choice of operation to be performed Step2: Enqueue: a) Read the element x to be inserted b) Check whether the current rear position is greater then maximum queue si ze. If so, print queue is full. c) Else, increment the rear position and place the element x at the rear posi tion. Step3:Dequeue: a) If front position is less than 0 then print queue is empty. b) Delete the element in front position c) Increment the front position Step4: Display: From the start position, display all elements in the queue.

PROGRAM: #include"qarray.h" void main() { int choice; q.rear=-1; q.front=-1; clrscr(); while(1) { printf("\nMENU:\n1.Enqueue\t2.Dequeue\t3.Display\t4.Exitn"); printf("\nEnter your choice\t"); scanf("%d",&choice); switch(choice) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); } } }

Qarray.h #include<stdio.h> #include<conio.h> #define max 5 struct queue { int a[max]; int rear,front; }q; void enqueue() { int x; if(q.rear==max-1) printf("\nQueue is full\n"); else { printf("\nEnter the element to be inserted\n"); scanf("%d",&x); q.rear=q.rear+1; q.a[q.rear]=x; } } void dequeue() { int x; if(q.front==q.rear) printf("\nQueue is empty\n"); else { q.front=q.front+1; x=q.a[q.front]; printf("\nDeleted element from queue is %d\t",x); } } void display() { int i; if(q.front==q.rear) printf("\nQueue is empty\n"); else { for(i=q.front;i<=q.rear;i++) { if(i==q.front+1) printf("\n%d<--FRONT",q.a[i]); else if(i==q.rear) printf("\n%d<--REAR",q.a[i]); else printf("\n%d",q.a[i]); } }

} EX.NO. LINKED LIST IMPLEMENTATION OF QUEUE ADT AIM: To write a C program to perform linked list implementation of Queue ADT. ALGORITHM:

DATE:

Step1: Define the queue structure and read the choice of operation to be performed Step2: Enqueue: a) Read the element t to be inserted b) Check whether the current rear position is greater then maximum queue si ze. If so, print queue is full. c) Else, increment the rear position and place the element t at the rear posi tion. Step3: Dequeue: a) If front position is less than 0 then print queue is empty. b) Delete the element in front position c) Increment the front position Step4: Display: From the start position, display all elements in the queue.

PROGRAM: #include"qlink.h" void main() { int ch; clrscr(); front=rear=NULL; do { printf("\n\n1.Enqueue\t2.Dequeue\t3.Display\t4.Exit\n"); printf("\nEnter your choice\t"); scanf("%d",&ch); switch(ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: exit(0); } } while(ch<=4); getch(); } Qlink.h

#include<stdio.h> #include<conio.h> #include<process.h> #include<malloc.h> struct node { int data; struct node *next; }*front,*rear,*t,*cur; void enqueue() { t=(struct node *)malloc(sizeof(struct node)); printf("\nEnter the data to be inserted\n"); scanf("%d",&t->data); t->next=NULL; if(front==NULL) { front=t; rear=t; } else { rear->next=t; rear=t; } } void dequeue() { if(front==NULL) printf("\nQueue is empty\n"); else { cur=front; front=front->next; printf("\nDeleted element from queue is %d\n",cur->data); free(cur); } } void display() { cur=front; if(cur==NULL) printf("\nQueue id empty\n"); else { printf("\nContents of the Queue are\n"); while(cur!=NULL) { printf("%d\t",cur->data); cur=cur->next; } }

EX.NO. SEARCH TREE ADT - BINARY SEARCH TREE AIM: To write a C program to implement Binary Search Tree. ALGORITHM:

DATE:

Step1: Define the BST structure and read the choice to perform the opera tions. Step2: If the choice is insert, a) Create a node and assign input value in it. b) Make its left and right child pointer as NULL c) If the inserted value is less than the root, then place it in the left s ubtree. d) Else, place it in the right subtree. Step3: If the choice is findmin, a) Traverse the left subtree b) Last value in the left subtree is the minimum value. Step4: If the choice is findmax, a) Traverse the right subtree b) Last value in the right subtree is the maximum value. Step5: If the choice is find, a) Read the element to be searched. b) If the search element is less than the root value, continue the search i n left subtree c) If the search element is more than the root value, continue the search i n right subtree d) If the search element is found then display element Found Step6: If the choice is display a) Display all elements in BST from root to leaves

PROGRAM: #include<stdio.h> #include<conio.h> #include<process.h> #include<malloc.h> struct node { int data; struct node *left,*right; }*root,*t,*p,*q; typedef struct node *nodeptr; //nodeptr root,t,p,q; void add(); void search(); void findmin();

void findmax(); void disp(nodeptr,int,int,int); void main() { int ch; clrscr(); root = NULL; while(1) { printf("\n1.Add\n2.Search\n3.Display\n4.Findmin\n5.Findmax\n6.Exit\n"); printf("\nEnter the choice: \t"); scanf("%d",&ch); switch(ch) { case 1: add(); break; case 2: search(); break; case 3: clrscr(); disp(root,40,7,16); break; case 4: findmin(); break; case 5: findmax(); break; case 6: exit(0); } } } void add() { int x; t=(nodeptr)malloc(sizeof(struct node)); printf("enter data:"); scanf("%d",&x); t->data=x; t->left=NULL; t->right=NULL; if(root==NULL) root=t; else { p=q=root; while(q!=NULL && x!=p->data) { p=q; if(x<p->data) q=p->left; else q=p->right; } if(x==p->data) printf("%d is duplicate number\n",x); else if(x<p->data) p->left=t; else p->right=t; } }

void search() { int x; printf("Enter the element to search\n"); scanf("%d",&x); p=q=root; while(q!=NULL && x!=p->data) { p=q; if(x<p->data) q=p->left; else q=p->right; } if(x==p->data) printf("Element is present\n"); else printf("\nElement is not present"); } void disp(nodeptr root,int col,int row,int wid) { gotoxy(col,row); if(root!=NULL) { printf("%d\t",root->data); disp(root->left,col-wid,row+2,wid/2); disp(root->right,col+wid,row+2,wid/2); } } void findmax() { t=root; while(t->right!=NULL) t=t->right; printf("\nMaximum %d",t->data); } void findmin() { t=root; while(t->left!=NULL) t=t->left; printf("\nMinimum %d",t->data); }

EX.NO. IMPLEMENTATION OF HEAP SORT AIM: To write a C program to implement heap sort . ALGORITHM:

DATE:

Step1: Read the number of elements to perform sorting. Step2: Insert element into the heap using build function. Build function: StepI: The array is first converted into a heap StepII: It then sorts the data in reverse by repeatedly placing the largest uns orted element into its correct place. StepIII: It does so by repeatedly (1)removing the maximum value in the heap(the value in root node) (2) putting that value into the sorted array and (3) rebuil ding the heap with fewer elements stepIV: Heap sort uses the same array for the heap and the sorte d array. Step3: Display the result

PROGRAM: #include<stdio.h> #include<stdlib.h> #include<conio.h> void SortHeap(int[],int); void main() { int i,n,arr[50]; clrscr(); printf("\nEnter the no. of elements\n"); scanf("%d",&n); printf("\nEnter the elements::\n"); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } for(i=n;i>1;i--) SortHeap(arr,i-1); printf("\nThe sorted array\n------------------\n"); for(i=0;i<n;i++) printf("%d\n",arr[i]); getch();

} void SortHeap(int arr[],int arr_ubound) { int i,m; int lChild,rChild,mChild,root,temp; root=(arr_ubound-1)/2; for(m=root;m>=0;m--) { for(i=root;i>=0;i--) { lChild=(2*i)+1; rChild=(2*i)+2; if((lChild<=arr_ubound)&&(rChild<=arr_ubound)) { if(arr[rChild]>=arr[lChild]) mChild=rChild; else mChild=lChild; } else { if(rChild>arr_ubound) mChild=lChild; else mChild=rChild; } if(arr[i]<arr[mChild]) { temp=arr[i]; arr[i]=arr[mChild]; arr[mChild]=temp; } } } temp=arr[0]; arr[0]=arr[arr_ubound]; arr[arr_ubound]=temp; return; }

EX.NO. IMPLEMENTATION OF QUICK SORT AIM: To write a C program to implement quick sort . ALGORITHM:

DATE:

Step1: Read the number of elements to perform sorting Step2: Pick an element in the array to serve as a pivot point. Step3: Split the array into two parts one with elements larger than the pivot and the other with elements smaller than the pivot. Step4: Elements that are less than or equal to pivot will move towards t he left and elements that are greater than or equal to pivot will move towards the right. Step5: Recursively repeat the algorithm for both halves of the original array. Step6: Display the result.

PROGRAM: #include<conio.h> #include<stdio.h> int qsort[25]; void sort(int low,int high); void sort(int low,int high) { int temp; if(low<high) { int i=low+1; int j=high;

int t=qsort[low]; while(1) { while(qsort[i]<t) i++; while(qsort[j]>t) j--; if(i<j) { temp=qsort[i]; qsort[i]=qsort[j]; qsort[j]=temp; i++; j--; } else break; } qsort[low]=qsort[j]; qsort[j]=t; sort(low,j-1); sort(j+1,high); } else return; }

void main() { int n,i; clrscr(); printf("\n\t\t\t\tQUICK SORT"); printf("\n\t\t\t\t***** ****\n\n\n"); printf("\nenter no of elements:"); scanf("%d",&n); printf("\nenter elements to be sorted"); for(i=1;i<=n;i++) scanf("%d",&qsort[i]); sort(1,n); printf("\nSORTED ELEMENT :"); for(i=1;i<=n;i++) printf("%d\t",qsort[i]); getch(); }

Вам также может понравиться