42
RAJALAKSHMI ENGINEERING COLLEGE [AUTONOMOUS] RAJALAKSHMI NAGAR, THANDALAM, CHENNAI-602105 DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no: 1 Dealing with Complex Numbers Date: 1

Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Embed Size (px)

Citation preview

Page 1: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

RAJALAKSHMI ENGINEERING COLLEGE [AUTONOMOUS]

RAJALAKSHMI NAGAR, THANDALAM, CHENNAI-602105

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CS17211 DATA STRUCTURES LABORATORY

SECOND YEAR

THIRD SEMESTER

LAB MANUAL

1

Page 2: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex no: 1 Dealing with Complex Numbers

Date:

Problem Statement:

You are given two complex numbers and you have to print the result of their addition, subtraction, multiplication, division and modulus operations. The real and imaginary precision part should be correct up to two decimal places.

Python is a fully object-oriented language. Classes includes attributes and functions.Methods with a double underscore before and after their name are considered as built-in methods. They are used by interpreters and are generally used in the implementation of overloaded operators or other built-in functionality. For complex numbers with non-zero real (A) and complex part (B), the output should be in the following format: (A+Bi) Replace the plus symbol (+) with a minus symbol (-) when B<0.

For complex numbers with a zero complex part i.e. real numbers, the output should be:  A+0.00i. For complex numbers where the real part is zero and the complex part (b) is non-zero, the output should be: 0.00+Bi

Methods

__add__ ( ) -> Can be overloaded for + operation

__sub__ ( ) -> Can be overloaded for - operation

__mul__ ( ) -> Can be overloaded for * operation

Input Format

One line of input: The real and imaginary part of a number separated by a space.

Output Format

For two complex numbers C and D, the output should be in the following sequence on separate lines:

C+D

C-D

C*D

C/D

MOD(C)

MOD(D)

2

Page 3: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Sample Input

2 1

5 6

Sample Output

7.00+7.00i

-3.00-5.00i

4.00+17.00i

0.26-0.11i

2.24+0.00i

7.81+0.00i

3

Page 4: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex No: 2 Contacts Application

Date:

Problem Statement

The application must perform two types of operations:add name, where name is a string denoting a contact name. This name must store as a new contact in the application. Find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting with partial and print the count on a new line.

Method:

Add(): n sequential names

Find() : finding the partial name in each string and displaying the count

Explanation

We perform the following sequence of operations:

Add a contact named hack.

Add a contact named hackerrank.

Find and print the number of contact names beginning with hac. There are currently two contact names in the application and both of them start with hac, so we print counton a new line.

Find and print the number of contact names beginning with hak. There are currently two contact names in the application but neither of them start with hak, so we print count on a new line.

Input Format

n= number of contact names.

Enter the names sequentially.

Enter the Partial name for comparison.

Constraints

It is guaranteed that name and partial contain lowercase English letters only.

The input doesn't have any duplicate name for the add operation.

Output Format

For each find partial operation, print the number of contact names starting with partial on a new line.

4

Page 5: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Sample Input1

2

add hack

addhackerrank

findhac

Sample Output1

2

Sample Input2

2

add hack

addhackerrank

findhak

Sample Output2

0

5

Page 6: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex no: 3 Stack

Date:

Problem Statement:

A stack is a collection of objects that supports fast last-in, first-out (LIFO) semantics for inserts and deletes. Unlike lists or arrays, stacks typically don’t allow for random access to the objects they contain. The insert and delete operations are also often called push and pop.

A useful real world analogy is checking whether a given string is Palindrome or not using Stack data structure.

Methods:

isEmpty(self) :Check Stack is Empty or not push(self, item):To insert each item inside the stack pop(self):To delete the top most element in the stack peek(self):To display the Top element in the stack size(self):To display the size of stack

Input Format

Get the String as an input

Constraints

S, is composed of lowercase English letters.

Output Format

It should Print whether the string is palindrome or not

Sample Input1

racecar

Sample Output1

The word, racecar, is a palindrome.

Sample Input2

zooming

Sample Output2

The word, zooming, is a palindrome.

6

Page 7: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex no: 4 Balancing Parenthesis using Stack

Date:

Problem Statement:

A bracket is considered to be any one of the following characters: (,), {,}, [, or]. Two brackets are

considered to be a matched pair if the opening bracket (i.e., (, [, or {) occurs to the left of a closing

bracket (i.e.,),], or}) of the exact same type. There are three types of matched pairs of brackets: [], {},

and ().A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For

example, {[(])} is not balanced because the contents in between {and} are not balanced. The pair of

square brackets encloses a single, unbalanced opening bracket, (, and the pair of parentheses encloses a

single, unbalanced closing square bracket,].

Some examples of balanced brackets are [] {} (), [({})] {} () and ({() {} []}) [].

Explanation

1. The string {[()]} meets both criteria for being a balanced string, so we print YES on a new line.2. The string {[(])} is not balanced, because the brackets enclosed by the matched pairs [(] and (]) are not

balanced. Thus, we print NO on a new line.3. The string {{[[(())]]}} meets both criteria for being a balanced string, so we print YES on a new line.

Constraints:

It contains no unmatched brackets.

The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.

Given n strings of brackets, determine whether each sequence of brackets is balanced. If a string is balanced, print YES on a new line; otherwise, print NO on a new line.

Input Format

The first line contains a single integer,n , denoting the number of strings. 

Each line i of the n subsequent lines consists of a single string, s, denoting a sequence of brackets.

Output Format

For each string, print whether or not the string of brackets is balanced on a new line. If the brackets

are balanced, print YES; otherwise, print NO.

7

Page 8: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Sample Input

3

{[()]}

{[(])}

{{[[(())]]}}

Sample Output

YES

NO

YES

8

Page 9: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex No: 5 QUEUE

Date:

Problem Statement:

Creating a Queue for Vehicles on toll-tax Bridge: The vehicle that comes first to the toll tax booth leaves

the booth first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out (FIFO)

strategy of queue.

Explanation:The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded

programming when information must be exchanged safely between multiple threads. The Queue class in

this module implements all the required locking semantics. In a FIFO queue, the first tasks added are the

first retrieved.

Methods: Queue(maxsize=0): Mention the maximum size of Queue

queue.Empty:specify Queue is empty or full

queue.Full Queue is empty or full

qsize():current size of queue

Queue.get(item) inserting the element in queue

Input Format:First line: Specify the total number of elements in list

Second line: Enter the elements one by one

Output format:Inserting new element in the Queue.

Deleting an element in the Queue

Display the element

Input Format13

10

20

30

Output Format 140

10

[20, 30, 40]

9

Page 10: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex no: 6 DEQUEDate:Problem Statement:A dequeue is a double-ended queue. It can be used to add or remove elements from both ends.

Dequeue support thread safe, memory efficient appends and pops from either side of the dequeue with

approximately the same O (1) performance in either direction.

. Method: dequeue() methods

append()

pop()

popleft() 

appendleft()

Input Format:

The first line contains an integer N, the number of operations. The next N lines contains the space

separated names of methods and their values.

Constraints:0<N<=100

Output Format

Print the space separated elements of dequeue d.

Sample Input

6

append 1

append 2

append 3

appendleft 4

pop

popleft

Sample Output

10

Page 11: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

1

12

123

4123

412

12

11

Page 12: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex. no: 7 Linked List Operations

Date: Singly Linked List

A singly linked list, each node’s address part contains the information about the location of the next node. This forms a series of chain or links. The first node of the linked list is kept track by the head pointer. The last node points to None.

A node consists of two parts:

1. Data part - contains the data2. Address part - this is pointer that points to location of the next node

ExplanationCreating classes

Firstly, you must create a node in order to create a singly linked list. To do this, we create a class Node with data and nextNode attributes. 

Operation to be performed

Inserting a Node

Print Nodes

Delete Nodes Merging two Linked list

Input FormatGet the number of elements to be added in list

Insert set of elements in List

Insert element in index 0

Insert element in index n

Insert element at position m

Merge two list L1 and L2

Delete element in index 0

Delete element in index n

Delete element at position m

Output FormatDisplay the list after each operation

Sample Input

12

Page 13: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Null

3

1->2->3->null

InsertBeg 4

InsertMid 7 at position 2

InsertEnd 10

L1=4->1->7->2->3->10

L2=17->18

DeleteBeg

Deleteend

Delete at position 2

Sample Output

1->2->3->null

4->1->2->3->null

4->1->7->2->3->null

4->1->7->2->3->10->null

Merge=4->1->7->2->3->10->17->18

1->7->2->3->10->17->18->null

1->7->2->3->10->17->null

1->7-->3->10->17->null

Ex. no: 8 Doubly Linked List

13

Page 14: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Double Linked list is a linear data structure and it is used to store elements in sequence and maintains the links to previous and next element using pointers.

Data Pointer to previous element Pointer to next element

Explanation:

Creating classes

Firstly, you must create a node in order to create a singly linked list. To do this, we create a class Node with data, nextNode attributes, prevNode attribute.

Operation to be performed

Inserting a Node

Print Nodes

Delete Nodes Merging two Linked list

Input Format Get the number of elements to be added in list

Insert set of elements in List

Insert element in index 0

Insert element in index n

Insert element at position m

Merge two list L1 and L2

Delete element in index 0

Delete element in index n

Delete element at position m

Output FormatDisplay the list after each operation

Sample InputNull

3

Null->1<->2<->3<->null

InsertBeg 4

14

Page 15: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

InsertMid 7 at position 2

InsertEnd 10

L1=4<->1<->7<->2<->3<->10

L2=17<->18

DeleteBeg

Deleteend

Delete at position 2

Sample Output

null<->1<->2<->3<->null

null<->4<->1<->2<->3<->null

null<->4<->1<->7<->2<->3<->null

null<->4<->1<->7<->2<->3<->10<->null

Merge=4->1<->7<->2<->3<->10<->17->18

null<->1<->7<->2<->3<->10<->17<->18<->null

null<->1<->7<->2<->3<->10<->17<->null

null<->1<->7<-->3<->10<->17<->null

Ex. no: 9 Circular Linked List

15

Page 16: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked list Data

Pointer to previous element Pointer to next element

Explanation:

Creating classes

Firstly, you must create a node in order to create a singly linked list. To do this, we create a class Node with data, nextNode attributes, prevNode attribute.

Operation to be performed

Inserting a Node

Print Nodes

Delete Nodes Merging two Linked list

Input Format Get the number of elements to be added in list

Insert set of elements in List

Insert element in index 0

Insert element in index n

Insert element at position m

Merge two list L1 and L2

Delete element in index 0

Delete element in index n

Delete element at position m

Output FormatDisplay the list after each operation

Sample InputNull

3

head ->1<->2<->3<->head

16

Page 17: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

InsertBeg 4

InsertMid 7 at position 2

InsertEnd 10

L1=4<->1<->7<->2<->3<->10

L2=17<->18

DeleteBeg

Deleteend

Delete at position 2

Sample Output

head<->1<->2<->3<-> head

head<->4<->1<->2<->3<-> head

head<->4<->1<->7<->2<->3<-> head

head<->4<->1<->7<->2<->3<->10<-> head

Merge=4->1<->7<->2<->3<->10<->17->18

head<->1<->7<->2<->3<->10<->17<->18<-> head

head<->1<->7<->2<->3<->10<->17<-> head

head<->1<->7<-->3<->10<->17<-> head

Ex. no: 10 Binary Search Tree

17

Page 18: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

A binary search tree (BST) or ordered binary tree is a node-based binary tree data structure which has

the following properties:

The left sub tree of a node contains only nodes with keys less than the node’s key. The right sub tree of a node contains only nodes with keys greater than the node’s key. Both the left and right sub trees must also be binary search trees.

Explanation:

Create a BST with following node operation:

Left node Right node Node’s data (same as key in the definition above.)

Operations to be performed:

Insert() Display() Delete()

Input Format

Enter the total number of elements in the list

Enter the n number of elements inside the list

Constraints

No. of nodes in the tree  500

Output Format

Return the root of the binary search tree after inserting the value into the tree.

Sample Input

4

/ \

2 7

/ \

1 3

18

Page 19: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

The value to be inserted is 6.

Sample Output

4

/ \

2 7

/ \ /

1 3 6

Ex. no: 11 Tree Traversal

19

Page 20: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date: In order Traversal

Problem Statement:

Process all nodes of a tree by recursively processing the left sub tree, then processing the root, and finally the right sub tree.

Explanation:

1. Check if the current node is empty / null.2. Traverse the left sub tree by recursively calling the in-order function.3. Display the data part of the root (or current node).4. Traverse the right sub tree by recursively calling the in-order function.

In a binary search tree, in-order traversal retrieves data in sorted order.

Input FormatLine 1: Specify the number of nodes in a List

Line 2: Insert the element in the List

Output Format

Print the tree's in order traversal as a single line of space-separated values.

Sample Input

1

\

2

\

5

/ \

3 6

\

4

Sample Output

12 3 4 5 6

Ex. no: 12 Preorder Traversal

20

Page 21: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement:

Preorder traversal process all nodes of a tree by processing the root, then recursively processing all sub trees. Also known asprefix traversal.

Explanation:

1. Check if the current node is empty / null.2. Display the data part of the root (or current node).3. Traverse the left sub tree by recursively calling the pre-order function.4. Traverse the right sub tree by recursively calling the pre-order function.

Input Format

Our hidden tester code passes the root node of a binary tree to your preorder function.

Constraints Nodes in the tree 

Output Format

Print the tree's preorder traversal as a single line of space-separated values.

Sample Input 1

\

2

\

5

/ \

3 6

\

4

Sample Output

1 2 5 3 4 6

Ex. no: 13 Post order Traversal

21

Page 22: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Process all nodes of a tree by recursively processing all sub trees, then finally processing the root. Also known as postfix traversal.

Explanation

1. Check if the current node is empty / null.2. Traverse the left sub tree by recursively calling the post-order function.3. Traverse the right sub tree by recursively calling the post-order function.4. Display the data part of the root (or current node).

Input Format

Our hidden tester code passes the root node of a binary tree to your post Order function.Constraints1 Nodes in the tree 500

Output Format

Print the tree's post order traversal as a single line of space-separated values.

Sample Input

1

\

2

\

5

/ \

3 6

\

4

Sample Output

4 3 6 5 2 1

Ex. no: 14 SIMILAR PAIR

22

Page 23: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement:

A pair of nodes, (a, b), is a similar pair if both of the following conditions are true:

Node is the ancestor of node abs (a-b) <=k.Given a tree where each node is labeled from 1 to n, can you find the number of similar pairs in the tree?

Explanation

The similar pairs are (3,2) ,(3,1) ,(3,4) ,(3,5) and , so we print  as our answer. Observe that (1, 4) and (1, 5) are not similar pairs because they do not satisfy abs (a-b) <=k.

Input Format

The first line contains two space-separated integers, n (the number of nodes) and k (the similar pair qualifier), respectively. Each line i of the (n-1) subsequent lines contains two space-separated integers defining an edge connecting nodes pi and ci, where node pi is a parent to node.

Output Format

Print a single integer denoting the number of similar pairs in the tree.

Sample Input

5 2

3 2

3 1

1 4

1 5

Sample Output

4

Ex. no: 15 SHORTEST PATH

23

Page 24: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem statement

Consider an undirected graph consisting of n nodes where each node is labeled from 1 to n and the edge between any two nodes is always of length 6. We define node s to be the starting position for a BFS.

Given q queries in the form of a graph and some starting node, s, perform each query by calculating the shortest distance from starting node s to all the other nodes in the graph. Then print a single line of n-1 space-separated integers listing node s 's shortest distance to each of the n-1 other nodes (ordered sequentially by node number); if  is disconnected from a node, print  -1  as the distance to that node.

Explanation

We perform the following two queries:

The given graph can be represented as: 

where our start node, 1, is node. The shortest distances from to the other nodes are one edge to node, one edge to node, and an infinite distance to node (which it's not connected to). Space-separated integers: 6, 6, -1 as node distance.

The given graph can be represented as: 

where our start node, 2, is node. There is only one edge here, so node is unreachable from node and node has one edge connecting it to node. We then print node’s distance to nodes and (respectively) as a single line of space-separated integers: -1 6.

Note: Recall that the actual length of each edge is 6, and we print -1 as the distance to any node that's unreachable from s.

Input Format

24

Page 25: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

The first line contains an integer, q, denoting the number of queries. The subsequent lines describe each query in the following format:

The first line contains two space-separated integers describing the respective values of n (the number of nodes) andm (the number of edges) in the graph.

Each line of the m subsequent lines contains two space-separated integers, u and v, describing an edge connecting node u to node v.The last line contains a single integer, s, denoting the index of the starting node.

Constraints

1<=q<=10

2<=n<=1000

1<=m<=n-(n-1)/2

1<=u,v,s<=n

Output Format

For each of the q queries, print a single line of n-1 space-separated integers denoting the shortest distances to each of the n-1 other nodes from starting position s. These distances should be listed sequentially by node number (i.e.1, 2… n), but should not include node s. If some node is unreachable from s, print -1 as the distance to that node.

Sample Input

2

4 2

1 2

1 3

1

3 1

2 3

2

Sample Output

6 6 -1

-1 6

SORTING

25

Page 26: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Ex no: 16 Insertion sort

Date:

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. Efficient for small data sets, much like other quadratic sorting algorithms.Problem Statement: The method insertion Sort takes in one parameter an unsorted array. Use an Insertion Sort Algorithm to sort the entire array.

Input Format There will be two lines of input:

s - The size of the list

l1 – list elements

Output Format On each line, output the entire array at every iteration.

Sample Input

6

1 4 3 5 6 2

Sample Output

1 4 3 5 6 2

1 3 4 5 6 2

1 3 4 5 6 2

1 3 4 5 6 2

1 2 3 4 5 6

Ex no: 17 Bubble sort

26

Page 27: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement: Given an element list, L= [a0, a1….an-1], of distinct elements, sort array A in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines:

List is sorted in numSwapsswaps.wherenumSwaps is the number of swaps that took place.

First Element: firstElement, where firstElement is the first element in the sorted array.

Last Element: lastElement, where lastElement is the last element in the sorted array.

Input Format

The first line contains an integer,n, denoting the number of elements in List.  The second line contains elements in a list.

Output Format

Print no of swaps occurred for sorting

Print first element of list

Print last element of list

Sample Input 0

3

1 2 3

Sample Output 0

Array is sorted in 0 swaps.

First Element: 1

Last Element: 3

Sample Input 1

3

3 2 1

Sample Output 1

Array is sorted in 3 swaps.

First Element: 1

Last Element: 3

Ex no: 18 Selection sort

27

Page 28: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

 Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O (n2) time complexity, making it inefficient on large lists.

Problem Statement:

Get the set of elements of list in an unordered manner .Using selection sort compare the elements and rearrange the elements in sorted order.The algorithm divides the input list into two parts: the sub list of items already sorted, which is built up from left to right at the front (left) of the list, and the sub list of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sub list is empty and the unsorted sub list is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sub list, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sub list boundaries one element to the right.

Explanation

The initial array of strings is 

Unsorted= [31415926535897932384626433832795, 1, 3, 10, 3, 5].

When we order each string by the real-world integer value it represents, we get:

1<=3<=3<=5<=10<=31415926535897932384626433832795

We then print each value on a new line, from smallest to largest.

Input Format

The first line contains an integer, n, denoting the number of strings in unsorted. Each of the n subsequent lines contains a string of integers describing an element of the array.

Constraints

1<=n<=2 x 105

Each string is guaranteed to represent a positive integer without leading zeros.

The total number of digits across all strings in unsorted is between 1 and 106 (inclusive).

Output Format

Print each element of the sorted array on a new line.

Sample Input

28

Page 29: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

6

31415926535897932384626433832795

1

3

10

3

5

Sample Output

1

3

3

5

10

31415926535897932384626433832795

Ex no: 19 Quick Sorts

29

Page 30: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement:

Based on divide-and-conquer algorithm called Quicksort (also known as Partition Sort).

Step 1: Divide Choose some pivot element, , and partition your unsorted array,ar , into three smaller arrays:left ,right  and equal , where each element in left <p, each element inright>p , and each element in equal=p .

Challenge Given ar and p=ar [0], partition ar into left, right, and equal using the Divide instructions above. Then print each element in left followed by each element in equal, followed by each element in right on a single line. Your output should be space-separated.

Note: There is no need to sort the elements in-place; you can create two lists and stitch them together at the end.

Explanation

 ar=[4,5,3,7,2]Pivot: p=ar[0]=4. left={};equal={4} ;right={} 

ar[1]=5>=p, so it's added to right . left={};equal={4} ;right={5} ; 

ar[2]=3<p,, so it's added to left . left={3};equal={4} ;right={5} ; 

ar[3]=7>=p, so it's added to right . left={3};equal={4} ;right={5,7} ; 

ar[4]=2<p, so it's added to left. left={3,2};equal={4} ;right={5,7} ; 

We then print the elements of left, followed by equal, followed by right, we get: 3 2 4 5 7.

This example is only one correct answer based on the implementation shown, but it is not the only correct answer (e.g.: another valid solution would be 2 3 4 5 7).

Input Format

The first line contains n size of list.The second line contains n space-separated integers are added inside list  

Output Format

30

Page 31: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

On a single line, print the partitioned numbers each integer should be separated by a single space.

Sample Input

5

4 5 3 7 2

Sample Output

3 2 4 5 7

Ex no: 20 Merge Sort

31

Page 32: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement:

Top-Down Approach

This divide and conquer algorithm has three steps:

Divide. Split an unordered n-element sequence into two unordered subsequences of n/2 items.

Conquer. Recursively sort the two subsequences until such a time as you hit the base case of one item (i.e., a list containing a single element is sorted with respect to itself).

Stitch. Merge the two sorted subsequences into a single sorted list.

Implement Merge algorithm to sort an integer array in ascending order,

Input Format

First line: the size of the array

Second line: the array to be sorted (numbers separated by spaces)

Output Format

Output between two brackets and each number separated by a coma.

Sample Input

6

1 9 3 5 4 7

Sample Output

[1, 3, 4, 5, 7, 9]

Ex no: 21 LINEAR SEARCH

32

Page 33: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Linear search, also known as sequential search, is a process that checks every element in the list sequentially until the desired element is found. The computational complexity for linear search is O (n), making it generally much less efficient than binarysearch (O (log n)).

Problem Statement:

Each time Sunny and Johnny take a trip to the Ice Cream Parlor, they pool together  m dollars for ice cream. On any given day, the parlor offers a line of N flavors. Each flavor, i, is numbered sequentially with a unique ID number from 1 to n and has a cost, ci, associated with it.

Given the value of m and the cost of each flavor for t trips to the Ice Cream Parlor, help Sunny and Johnny choose two flavors such that they spend their entire pool of money (m) during each visit. For each trip to the parlor, print the ID numbers for the two types of ice cream that Sunny and Johnny purchase as two space-separated integers on a new line. You must print the smaller ID first and the larger ID second.

Note: Two ice creams having unique IDs i and j may have the same cost (i.e., Ci equivalent to Cj).

Sunny and Johnny make the following two trips to the parlor:

The first time, they pool together m=4 dollars. There are five flavors available that day and flavors 1 and 4 have a total cost of 1+3=4. Thus, we print 1 4 on a new line.

The second time, they pool together ==4 dollars. There are four flavors available that day and flavors 1 and 2 have a total cost of 2+2=4. Thus, we print 1 2 on a new line.

Input Format

The first line contains an integer, t, denoting the number of trips to the ice cream parlor. The 3t subsequent lines describe all of Sunny and Johnny's trips to the parlor; each trip is described as follows:

The first line contains m.

The second line contains n.

The third line contains n space-separated integers denoting the cost of each respective flavor. The ith integer corresponding to the cost, ci, for the ice cream with ID number i.

Output Format

Print two space-separated integers denoting the respective ID numbers for the flavors they choose to purchase, where the smaller ID is printed first and the larger ID is printed second. Recall that each ice cream flavor has a unique ID number in the inclusive range from 1 to n .

Sample Input

33

Page 34: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

2

4

5

1 4 5 3 2

4

4

2 2 4 3

Sample Output

1 4

1 2

Exno: 22 Binary Search

34

Page 35: Inserting a Node · Web viewDEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CS17211 DATA STRUCTURES LABORATORY SECOND YEAR THIRD SEMESTER LAB MANUAL Ex no:1Dealing with Complex Numbers

Date:

Problem Statement:

Binary search is the most prominent example of a Divide and Conquer algorithm. Suppose you are given an array of size N in sorted order and you have to return the index of anelement.

Explanation:

Basic algorithm isinitialize low = 0, high = N, mid = (low + high)/2

check if element is at position mid; if found, return imid else depending upon value of mid is lower or greater, we explore the right side of the array or the left side.

Constraints:

No. of nodes in the tree N <= 500

Input Format:

Line1: input the array size

Line 2: Input the array elements

Output Format:

Return the root of the binary search tree after inserting the value into the tree.

Sample Input

4

/ \

2 7

/ \

1 3

The value to be inserted is 6.

Sample Output

4

/ \

2 7

/ \ /

1 3 6

35