Ece-ds & Oops- Manual

41
Department of CSE EC 39 / DS and OOPS Lab M.PRADEEP EC 39 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB SYLLABUS 1. Basic Programs for C++ Concepts 2. Array implementation of List Abstract Data Type (ADT) 3. Linked list implementation of List ADT 4. Cursor implementation of List ADT 5. Stack ADT - Array and linked list implementations The next two exercises are to be done by implementing the following source files (a) Program source files for Stack Application 1 (b) Array implementation of Stack ADT (c) Linked list implementation of Stack ADT (d) Program source files for Stack Application 2 An appropriate header file for the Stack ADT should be #included in (a) and (d) 6. Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c)) 1

Transcript of Ece-ds & Oops- Manual

Page 1: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

M.PRADEEP

EC 39 - DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

SYLLABUS

1. Basic Programs for C++ Concepts

2. Array implementation of List Abstract Data Type (ADT)

3. Linked list implementation of List ADT

4. Cursor implementation of List ADT

5. Stack ADT - Array and linked list implementations

The next two exercises are to be done by implementing the following source files

(a)   Program source files for Stack Application 1

(b)   Array implementation of Stack ADT

(c)   Linked list implementation of Stack ADT

(d)   Program source files for Stack Application 2

An appropriate header file for the Stack ADT should be #included in (a) and (d) 

6. Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c))

7. Queue ADT – Array and linked list implementations

8. Search Tree ADT - Binary Search Tree

9. Heap Sort

10. Quick Sort

1

Page 2: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

List of Experiments

Exp No Name of the Experiment Page No

CYCLE – I

1. C++ basic Programs – Find biggest of n numbers and Find the given number is odd or even.

3

2. Program to perform array implementation of list ADT 5

3. Program to perform linked list implementation of list ADT 7

4. Program to perform cursor implementation of list ADT 9

5. Program to implementation stack using array and linked list. 11

6. Array and linked list implement of an infix and post fix expression using stack.

15

CYCLE – II

7. Program to implementation queue using array and linked list. 19

8. Program to implement binary search tree 23

9. Program to implement heap sort 26

10. Program to implement quick sort 28

ADDITIONAL EXPERIMENTS

11 Program to implement insertion sort 29

12 Program to implement bubble sort 30

2

Page 3: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EXNO: 1 C++ BASIC PROGRAMS

a. Program to find the biggest among ‘n’ numbers

AIM: To write a C++ program to find the biggest among ‘n’ numbers.

ALGORITHM:

Step 1: Start

Step 2: Enter the size of the array.

Step 3: Enter the elements of the array.

Step 4: Print the array elements.

Step 5: Initialize the large is equal to the first element of the array.

Step 6: Set a loop up to the array size.

Step 7: Check the next element is greater than the larger, if greater then assign next to the large.

Step 8: Print the list of largest elements.

Step 9: Stop

OUTPUT:

Enter the number of elements: 5 Enter the elements: 4 5

1 7 3 The Biggest number is : 7

RESULT:

3

Page 4: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

Thus the C++ program to find the biggest among ‘n’ numbers is written entered, executed and the results are verified.

b Program to find the given number is odd or even

AIM: To write a C++ program to find the given number is odd or even.

ALGORITHM:

Step 1: Start

Step 2: Enter the number

Step 3: Divide the number that you have entered by 2, if the remainder is 0 then print the given number is even else print the given number is odd.

Step 4: Stop

OUTPUT:

Enter the number: 5

The number is odd.

Enter the number: 8

The number is even.

RESULT:

Thus the C++ program to find the given number is odd or even is written entered, executed and the results are verified.

4

Page 5: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO: 2 PROGRAM TO PERFORM ARRAY IMPLEMENTATION OF LIST ADT

AIM: To write a C program to perform array implementation of list ADT

Problem Description:

A list is a sequence of zero or more elements of a given type a1,a2,a3,…..an ( n>=0)

n Length of Lista1 First element of list

an Last element of listn=0 Empty listElements can be linearly ordered according to their position in the list

ALGORITHM:

1. List Creation:

1. Establish the number to be created in the list and array S[0..n-1]2. If inserting new number is (ie. the first element) at the front of the list then

insert number into the first array position else

insert number into the next free array position3. Increment the size of the list

2. List Insertion:

1. Establish the number to be inserted and the position where number is to be inserted

2. Increment the size of the list3. If the size of the array is greater than size of the list

thenIf inserting new number is (ie. the first element) at the front of the list

thenmove all elements in the list one position down from their existing positioninsert number into the first array position

else

5

Page 6: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

move to the position specifiedmove all elements in the list one position down from their existing positioninsert number into the free array position

elseDecrement the size of the list and write array out of bounds

3. List Deletion:

1. Establish the number to be deleted 2. Search list for number to be deleted 3. If number to be deleted found then

move all elements in the list one position up from their existing positiondecrement the size of the list

elsewrite element not found

4. List Display:

Print all the numbers from the array position 0 - n-1 in the list

OUTPUT:

Enter the number of elements to be created in list: 3

Enter the elements: 2 3 4

1. Insert 2. Delete 3. Display Enter your choice: 1

Enter the position and number to insert: 1 1

1. Insert 2. Delete 3. Display Enter your choice: 2

Enter the number to delete: 4

1. Insert 2. Delete 3. Display Enter your choice: 3

List: 1 2 3

RESULT:

Thus the C program to perform array implementation of list ADT is written, entered, executed and the results are verified.

6

Page 7: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO: 3 PROGRAM TO PERFORM LINKED LIST IMPLEMENTATION OF LIST ADT

AIM:To write a C program to perform linked list implementation of list ADT.

ALGORITHM:

1. Linked List Creation:

1. Establish the number to be created and the pointer to the head of the list2. Create new node3. Store the number in the new node4. Make created node’s pointer to point to null5. If inserting new number is (ie. the first element) at the front of the list then

adjust head of the list pointer so that it points to created node at the head of list

elseadjust previous node’s pointer so it points to created node

2. Linked List Insertion:

1. Establish the number to be inserted and the pointer to the head of the list2. Initialize previous node to null and current node pointer to head of list3. Search list for the insertion position of the number to be inserted and

return pointers to their logical predecessor and successor nodes.4. Create new node5. Store the number to be inserted in the new node (inserted node)6. Adjust inserted node’s pointer so that it points to its logical successor7. If not inserting new number at the front of the list then

adjust previous node’s pointer so it points to inserted nodeelse

adjust head of the list pointer so that it points to inserted node at the head of list

3. Linked List Deletion:

1. Establish the number to be deleted and the pointer to the head of the list2. Initialize previous node to null and current node pointer to head of list

7

Page 8: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

3. Search list for number to be deleted and return pointers to their logicalpredecessor and the node itself (if found)

4. If number to be deleted found then

if node to be deleted not head of listthen

then previous node’s pointer to point to the successor of the node to be deleteddelete node

elseset list head pointer to point to the successor of the node to be deleteddelete node

dispose of the node to be deletedelse

write out that the number cannot be deleted

4. Linked List Display:

1. Establish the pointer to the head of the list2. Print the first number in the list 3. Move the pointer so that it points to the successor of the node4. Continue until the successor of the node is null

OUTPUT:

Enter the number of elements to be created in list: 3

Enter the elements: 2 3 4

1. Insert 2. Delete 3. Display Enter your choice: 1

Enter the position and number to insert: 1 1

1. Insert 2. Delete 3. Display Enter your choice: 2

Enter the number to delete: 4

1. Insert 2. Delete 3. Display Enter your choice: 3

List: 1 -> 2 -> 3 ->

RESULT:

Thus the C program to perform linked list implementation of list ADT is written entered, executed and the results are verified.

8

Page 9: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO: 4 PROGRAM TO PERFORM CURSOR IMPLEMENTATION OF LIST ADT

AIM: To write a C program to perform cursor implementation of list ADT.

ALGORITHM:

1. Cursor List Insertion:

1. Establish the number to be created and the position where number is to be inserted

2. Find the new free cell position from the list 3. Store the number in that position4. Adjust the position to be inserted next pointer to new number’s next

pointer5. Store the position to be inserted next pointer with new free cell position

2. Cursor List Deletion:

1. Establish the number to be deleted2. Find the previous element position from the list3. Retrieve the previous element position’s next pointer and store its next

pointer value intoprevious element position’s next pointer

4. Now include the retrieved position into the free list

3. Cursor List Display

1. While the next pointer is equal to 0do

print the number in the list move to the position in the next pointer

9

Page 10: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

OUTPUT:

Enter the number of elements to be created in list : 6Enter next position of 0: 3 Enter next position of 1: 0 Enter next position of 2: 5Enter next position of 3: 2 Enter next position of 4: 1 Enter next position of 5: 41. Insert 2. Delete 3. Display Enter your choice: 1Enter the element to insert: aSlot Element Next0 - 51 - 02 a 03 Header 24 - 15 - 41. Insert 2. Delete 3. Display Enter your choice : 1Enter the element to insert : bSlot Element Next0 - 41 - 02 a 53 Header 24 - 15 b 01. Insert 2. Delete 3. Display Enter your choice : 2Enter the element to delete : bSlot Element Next0 - 51 - 02 a 03 Header 24 - 15 - 4

RESULT:

10

Page 11: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

Thus the C program to perform cursor implementation of list ADT is written entered, executed and the results are verified.

EX NO: 5a PROGRAM TO PERFORM ARRAY IMPLEMENTATION OF STACK ADT

AIM: To write a C program to perform array implementation of stack ADT.

DESCRIPTION:

A collection of items in which only the most recently added item may be removed. The latest added item is at the top. Also known as "last-in, first-out" or LIFO.

The operations of the stack are,

Push - Insert an item into the stack

Pop - Delete an item from the stack

Peep – Display the top item from the stack

ALGORITHM:

1. Stack Creation / Pushing an item onto the stack:

1. Establish data to be pushed onto the stack2. If stack not full

thenEnter element into stack and increment top of stack by 1

elsewrite stack overflow

2. Stack Deletion / Popping an item from the stack:

1. If stack not emptythen

Remove element from stack and decrement top of stack by 1else

write stack empty2. Return data from top of stack

3. Stack Display:

11

Page 12: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

Print the elements from the top of stack by decrementing the top of stack value

till it reaches 0

OUTPUT:

Enter the number of elements to be created in stack : 6

Enter the elements: 1 2 3 4 5 6

1. Push 2. Pop 3. Display Enter your choice : 1

Enter the element: 7

1. Push 2. Pop 3. Display Enter your choice : 1

Enter the element: 8

1. Push 2. Pop 3. Display Enter your choice : 2

Popped element : 8

1. Push 2. Pop 3. Display Enter your choice : 3

Stack contents : 1 2 3 4 5 6 7

RESULT:

Thus the C program to perform array implementation of stack ADT is written entered, executed and the results are verified.

12

Page 13: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO:5b PROGRAM TO PERFORM LINKED LIST IMPLEMENTATION OF STACK ADT AIM:

To write a C program to perform linked list implementation of stack ADT.

DEFINITION OF STACK:

A collection of items in which only the most recently added item can be removed. The latest added item is at the top. Also known as ‘last-in first-out’ or LIFO.

ALGORITHM:

1. Stack Creation / Pushing an item onto the stack:

1. Establish data to be pushed onto the stack2. Create new node and store data3. Establish link from new element back to its successor4. Update pointer to top of stack

2. Stack Deletion / Popping an item from the stack:

1. If top of stack pointer not null then

get pointer to top elementretrieve data from top of stackreset top of stack pointer to successor of top element and delete noderemove top element from stack

elsewrite stack empty

2. Return data removed

3. Stack Display:

1. Print the top element from the stack 2. Reset top of stack pointer to successor of top element3. Continue until the pointer to the top of stack is null

13

Page 14: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

OUTPUT:

Enter the number of elements to be created in stack : 6

Enter the elements : 1 2 3 4 5 6

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 7

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 8

1. Push 2. Pop 3. Display

Enter your choice : 2

Popped element : 8

1. Push 2. Pop 3. Display

Enter your choice : 3

Stack contents : 1 2 3 4 5 6 7

RESULT:

Thus the C program to perform linked list implementation of stack ADT is written, entered, executed and the results are verified.

14

Page 15: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX: NO: 6a ARRAY IMPLEMENTATION OF INFIX EXPRESSION USING STACK

AIM:

To write a C program to implementation an infix expression in array using stack.

ALGORITHM:

Step 1: Create a source file and write some expressions in the file

Step 2: Read the expression one by one from the file and push on to the stack

1. Stack Creation / Pushing an item onto the stack:

Establish data to be pushed onto the stack If stack not full

then Enter element into stack and increment top of stack by 1

else write stack overflow

Step 3: After Pushing all elements view the stack

2. Stack Display:

Print the elements from the top of stack by decrementing the top of stack value till it reaches 0

Step 4: To delete an item or element from the stack perform the following steps,

3. Stack Deletion / Popping an item from the stack:

If stack not empty then

Remove element from stack and decrement top of stack by 1 else

write stack empty Return data from top of stack

15

Page 16: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

OUTPUT:

Stack input read from the file: ((3 + 2) * 4) / (5 - 1)

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 3

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 2

1. Push 2. Pop 3. Display

Enter your choice : 3

Stack contents : 3 2

1. Push 2. Pop 3. Display

Enter your choice : 2

Popped element : 2

1. Push 2. Pop 3. Display

Enter your choice : 3

Stack contents : 3

RESULT:

Thus the C program to implementation an infix expression in array using stack is written,

entered, executed and the results were verified.

16

Page 17: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX: NO: 6b LIST IMPLEMENTATION OF POSTFIX EXPRESSION USING STACK

AIM:

To write a C program to implementation a postfix expression in linked list using stack.

ALGORITHM:

Step 1: Create a source file and write some expressions in the file

Step 2: Read the expression one by one from the file and push on to the stack

1. Stack Creation / Pushing an item onto the stack:

Establish data to be pushed onto the stack Create new node and store data Establish link from new element back to its successor Update pointer to top of stack

Step 3: After Pushing all elements view the stack

2. Stack Display:

Print the top element from the stack Reset top of stack pointer to successor of top element Continue until the pointer to the top of stack is null

Step 4: To delete an item or element from the stack perform the following steps,

3. Stack Deletion / Popping an item from the stack:

If top of stack pointer not null then

get pointer to top elementretrieve data from top of stackreset top of stack pointer to successor of top element and delete noderemove top element from stack

elsewrite stack empty

Return data removed

17

Page 18: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

OUTPUT:

Stack input read from the file: 321-+

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 3

1. Push 2. Pop 3. Display

Enter your choice : 1

Enter the element : 2

1. Push 2. Pop 3. Display

Enter your choice : 3

Stack contents : 3 2

1. Push 2. Pop 3. Display

Enter your choice : 2

Popped element : 2

1. Push 2. Pop 3. Display

Enter your choice : 3

Stack contents : 3

RESULT:

Thus the C program to implementation a postfix expression in linked list using stack is

written, entered, executed and the results were verified.

18

Page 19: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO:7a PROGRAM TO PERFORM ARRAY IMPLEMENTATION OF QUEUE ADT

AIM:

To write a C program to perform array implementation of queue ADT.

DEFINITION OF QUEUE:

A collection of items in which only the earliest added item can be accessed. Basic operations are add or enqueue and delete or dequeue. Delete returns the item removed. Also known as “First-In First-Out or FIFO.

ALGORITHM:

1. Queue Creation / Enqueue an item into the queue:

1. Establish the data for insertion2. Advance the rear according to mechanism for keeping it within array

bounds3. If front not equal to rear then

theninsert data at next available position in the queue

elsequeue is full so write out full queuerestore rear marker to its previous value taking into account array bounds.

2. Queue Deletion / Dequeue an item from the queue:

1. If front not equal to rearthen

advance front according to mechanism for keeping it within array boundsremove element from front of queue

elsewrite queue empty

2. Return deleted element

3. Queue Display:

Print the elements present between front to rear marker positions

19

Page 20: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

OUTPUT:

Enter the number of elements to be created in queue : 6

Enter the elements : 1 2 3 4 5 6

1. Enqueue 2. Dequeue 3. Display Enter your choice : 1

Enter the element : 7

1. Enqueue 2. Dequeue 3. Display Enter your choice : 1

Enter the element : 8

1. Enqueue 2. Dequeue 3. Display Enter your choice : 2

Enqueued element : 1

1. Enqueue 2. Dequeue 3. Display Enter your choice : 3

Queue contents : 2 3 4 5 6 7 8

RESULT:

Thus the C program to perform array implementation of queue ADT is written entered, executed and the results are verified.

20

Page 21: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO: 7b PROGRAM TO PERFORM LINKED LIST IMPLEMENTATION OF QUEUE ADT AIM:

To write a C program to perform linked list implementation of queue ADT.

DESCRIPTION:

A collection of items in which only the earliest added item may be accessed. Basic operations are add (to the tail) or enqueue and delete (from the head) or dequeue. Delete returns the item removed. Also known as "first-in, first-out" or FIFO.

The operations of queue are,Enqueue : Inserts an element at the end of the queue.Dequeue : Removes an element at the front of the queue.Display : Display the list of elements present in the queue.

ALGORITHM:

1. Queue Creation / Enqueue an item into the queue:

1. Establish data to be inserted into the queue2. Create new node and store data3. Make created node’s pointer to point to null4. If not inserting data at the front of the queue then

adjust the rear node’s pointer so that it points to inserted node

adjust the rear pointer so that it points to inserted node

else

adjust front and rear pointers so that it points to the inserted node

2. Queue Deletion / Dequeue an item from the queue:

1. If front not equal to null then

21

Page 22: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

retrieve data from front nodeadjust the front pointer so that it points to front node’s pointer and delete node

elsewrite queue empty

2. Return data removed

3. Queue Display:

1. Print the element from front node 2. Adjust the front pointer so that it points to front node’s pointer3. Continue until the front equal to rear

OUTPUT:

Enter the number of elements to be created in queue : 6

Enter the elements : 1 2 3 4 5 6

1. Enqueue 2. Dequeue 3. Display Enter your choice : 1

Enter the element : 7

1. Enqueue 2. Dequeue 3. Display Enter your choice : 1

Enter the element : 8

1. Enqueue 2. Dequeue 3. Display Enter your choice : 2

Enqueued element : 1

1. Enqueue 2. Dequeue 3. Display Enter your choice : 3

Queue contents : 2 3 4 5 6 7 8

RESULT:

Thus the C program to perform linked list implementation of queue ADT is written entered, executed and the results are verified.

22

Page 23: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

EX NO: 8 PROGRAM TO IMPLEMENT BINARY SEARCH TREE

AIM:

To write a C program to implement binary search tree.

DESCRIPTION:

A Tree is a widely-used data structure that emulates a tree structure with a set of linked nodes. It is an acyclic and connected graph. The topmost node in a tree is called the root node.

Leaf nodesNodes at the bottommost level of the tree are called leaf nodes. Since they are at the

bottommost level, they do not have any children.A binary tree is a finite set of nodes which is either empty or consists of a root and two

disjoint binary trees called the left subtree and the right subtree.

ALGORITHM:

1. Binary Tree Creation / Insertion:

1. Establish number to be inserted2. Find where new number is to be placed in the tree using search algorithm3. Create a new node and store in it the number to be inserted. At the time of

insertion thisnew node will have no subtrees so its left and right pointes must be set to null

4. If tree is not initially empty then

if tree number comes numerically later than new number then

adjust previous node’s left pointer so that it points to the new nodeelse

adjust previous node’s right pointer so that it points to the new nodeelse

adjust root pointer to point to root node

23

Page 24: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

2. Binary Tree Deletion:

1. Establish number to be deleted2. Search tree for number to be deleted and return pointers to the number to

be deleted and its predecessor if it is present3. If node to be deleted is found

thenif node to be deleted is in the left subtree thenthen

invoke procedure for deleting node from left subtreeelse

invoke procedure for deleting node from right subtreeelse

report number to be deleted was not present

3. Deletion from right subtree:

1. Establish pointers to the current node to be deleted and its predecessor2. Link the predecessor node of the current node to its right subtree3. Get pointer to the root of the right subtree of the current node and set it as

the new current node pointer4. Find leftmost node in right subtree by following down a set of left links

until a null is encountered5. If the node to be deleted has a right subtree

thenmake the left subtree of the node to be deleted into the left subtree of the leftmost node in the right subtree and adjust the root if necessary

elsemake the left subtree of the node to be deleted become the right subtree of the predecessor of the node to be deleted and adjust the root if necessary

6. Dispose of the deleted node

4. Binary Tree Search:

1. Establish the search key2. Set the not found state and make the current node pointer point to the root

and theprevious nose pointer to null

3. While search key not found and there is still a valid path to follow in the tree

24

Page 25: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

doif search key is not found at the current node then

save pointer to current node in previous node pointerif search key is in the left subtree then

adjust current node pointer to point to the root of left subtree

elseadjust current node pointer to point to the root of right subtree

elsemake the not found condition false to indicate termination of search

4. Establish whether or not search key found5. Return the status found or not

5. Binary Tree Display:

1. While the tree pointer not null2. Recursively print the elements in the left subtree3. Recursively print the elements in the right subtree

OUTPUT:

Enter the number of elements to be created in the tree : 6

Enter the elements : 5 9 6 3 4 1

1. Insert 2. Delete 3. Display Enter your choice : 1

Enter the element : 7

1. Insert 2. Delete 3. Display Enter your choice : 1

Enter the element : 8

1. Insert 2. Delete 3. Display Enter your choice : 3

The elements in tree : 1 3 4 5 6 7 8 9

1. Insert 2. Delete 3. Display Enter your choice : 2

Enter the element : 8

1. Insert 2. Delete 3. Display Enter your choice : 3

The elements in tree : 1 3 4 5 6 7 9

25

Page 26: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

RESULT:

Thus the C program to implement binary search tree is written, entered, executed and the results are verified.

EX NO: 9 PROGRAM TO IMPLEMENT HEAP SORT

AIM: To write a C program to implement heap sort.

DESCRIPTION:

A heap sort is a complete binary tree which is either empty or satisfies the following conditions:

The priority of the root is higher than (or equal to) that of its children. The left and right sub trees of the root are heap-trees.

An alternative definition: A heap tree is a complete binary tree such that the priority of every node is higher that (or equal to) that of all its descendents. Yet another say is that every path as every one goes down in the tree, the values become smaller. The first obvious difference to binary search trees is that the biggest number now occurs at the root at the root (rather than the right most nodes). Secondly, where as binary search trees, the left and right child of a node played very important role, whereas now they will be interchangeable.

Example:

26

21

11 6

3

Page 27: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

Design Approach:

Name : Heap sort

Operations:

Build() : To insert the elements into the heap

Sort() : To sort the elements in the heap

ALGORITHM:

Step 1: Read the number of elements to perform the sorting

Step 2: Insert the element into the heap using build function.

Step 3: Sort the list of elements in the heap.

Step 4: Display the result.

OUTPUT:

Enter the number of Elements: 7Enter the Elements:6352971

The Sorted array1235679

RESULT:

27

Page 28: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

Thus the C program to implement heap sort is written, entered, executed and the results are verified.

EX NO: 10 PROGRAM TO IMPLEMENT QUICK SORT

AIM:

To write a C program to implement Quick sort.

ALGORITHM:

Step 1: Start

Step 2: Create a class ‘complex’ and declare the necessary variables.

Step 3: Generate the complex numbers randomly by the specified functions in the class

Step 4: Write the complex numbers into a file along with an operator.

Step 5: Read the complex numbers written to the file and perform the addition, subtraction, multiplication and division of the complex numbers.

Step 6: Print the manipulation of the complex numbers in the file.

Step 7: Stop.

OUTPUT:

Enter the number of Elements: 6Enter the Elements:935271

The Sorted array

28

Page 29: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

123579

RESULT:

Thus the C program to implement Quick sort is written, entered executed and the results are verified.

EX NO: 11 PROGRAM TO IMPLEMENT INSERTION SORT

AIM:

To write a C program to implement Insertion sort.

ALGORITHM:

Step 1: Start

Step 2: Read the total number of elements and say it as ‘n’.

Step 3: Set the value of i=0.

Step 4: Compare the adjacent elements.

Step5: Repeat step 4 for all the elements.

Step 6: Increment the value of i and repeat step4, 5 for i<n.

Step 7: Print the sorted list of elements.

Step 8: Stop.

OUTPUT:

Enter the number of elements: 5Enter the elements: 538710Sorted list:3

29

Page 30: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

57810

RESULT:

Thus the C program to implement insertion sort is written, entered executed and the results are verified.

EX NO: 12 PROGRAM TO IMPLEMENT BUBBLE SORT

AIM:

To write a C program to implement Bubble sort.

ALGORITHM:

Step 1: Start

Step 2: Read the total number of elements and say it as ‘n’.

Step 3: Set the value of i=0.

Step 4: Compare the adjacent elements.

Step5: Repeat step 4 for all the elements.

Step 6: Increment the value of i and repeat step4, 5 for i<n.

Step 7: Print the sorted list of elements.

Step 8: Stop.

OUTPUT:

Enter the number of elements: 5Enter the elements: 52891Sorted list:

30

Page 31: Ece-ds & Oops- Manual

Department of CSE EC 39 / DS and OOPS Lab

12589

RESULT:

Thus the C program to implement bubble sort is written, entered executed and the results are verified.

31