Sie sind auf Seite 1von 16

EXPERIMENT NO.

1A

AIM: Write a menu driven program for linear and binary search THEORY:

LINEAR SEARCH
Linear search or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. A linear search looks down a list, one item at a time, without jumping. In complexity terms this is an O(n) search - the time taken to search the list gets bigger at the same rate as the list does.

BINARY SEARCH
a binary search is an algorithm for locating the position of an item in a sorted array The idea is simple: compare the target to the middle item in the list. If the target is the same as the middle item, you've found the target. If it's before the middle item, repeat this procedure on the items before the middle. If it's after the middle item, repeat on the items after the middle. The method halves the number of items to check each time, so it finds it or determines it's not present in logarithmic timeIn complexity terms this is an O(log n) search - the number of search operations grows more slowly than the list does, because you're halving the "search space" with each operation.

EXPERIMENT NO. 1B

AIM: Write a menu driven program for Insertion and Bubble sort. THEORY:

INSERTION SORT
Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quick sort, heap sort, or merge sort the time complexity is O(n + d), where d is the number of inversions.

BUBBLE SORT
Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, this indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. time complexity of bubble sort is given as O(n2).

EXPERIMENT NO. 1C

AIM: Write a menu driven program for insertion and deletion in Array THEORY:
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. insertion n deletion in arrays: The array to which the element is to be inserted or deleted can be of two types unordered (unsorted) and ordered (sorted). Here we will be discussing about the insertion and deletion of element in an unordered or unsorted array. For insertion in these types of arrays, we need to have two information, the element to be inserted and the position to which it will be inserted. For deletion, we only need the position. Algorithm for Insertion of an element in an array Suppose, the array to be arr[max], pos to be the position at which the element num has to be inserted. For insertion, all the elements starting from the position pos are shifted towards their right to make a vacant space where the element num is inserted. FOR I = (max-1) TO pos arr[I] = arr[I-1] arr[I] = num Algorithm for Deletion of an element in an array Suppose, the array to be arr[max], pos to be the position from which the element has to be deleted. For deletion, all the elements to the right of the element at position pos are shifted to their left and the last vacant space is filled with 0. FOR I = pos TO (max-1) arr[I-1] = arr[I] arr[I-1] = 0

EXPERIMENT NO. 2

AIM: WAP to implement singly linked list(CREATION, INSERTION and DELETION) THEORY: In computer science, a linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues, associative arrays, and symbolic expressions. The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations. On the other hand, linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations such as obtaining the last node of the list, or finding a node that contains a given datum, or locating the place where a new node should be inserted may require scanning most of the list elements.

EXPERIMENT NO. 3

AIM: WAP to implement Stacks as (I) ARRAYS (II) LINKED LIST THEORY: a stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list. A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are typically those that have been in the list the longest The array implementation aims to create an array where the first element (usually at the zero-offset) is the bottom. That is, array[0] is the first element pushed onto the stack and the last element popped off TIME COMPLEXITY: time complexity of stack is O(n)

EXPERIMENT NO. 4

AIM: WAP to implement Queues THEORY: A Circular queue is another form of a linear queue in which the last position is connected to the first position of the list. The circular queue is similar to linear queue has two ends, the front end and the rear end. The rear end is where we insert elements and front end is where we delete elements. You can traverse in a circular queue in only one direction. Initially the front and rear ends are at same positions. When you insert elements the rear pointer moves one by one until the front end is reach end. If the next position of the rear is front, the queue is said to be fully occupied. Beyond this you cannot insert any data. But if you delete any data, you can insert the data accordingly. When you delete the elements the front pointer moves one by one until the rear pointer is reached. If the front pointer reaches the rear pointer, both their positions are initialized to -1. And the queue is said to be empty. Queue->Circular queue->Insert Operation Haven't you realised that the regular, static queues we saw have a very big drawback that once the queue is FULL, even though we delete few elements from the "front" and relieve some occupied space, we are not able to add anymore elements, as the "rear" has already reached the Queue's rear most position. The solution lies in a queue in which the moment "rear" reaches the Queue's watermark; the "first" element will become the queue's new "rear".

EXPERIMENT NO. 5

AIM: WAP to implement Circular Linked List THEORY:

In linear linked lists if a list is traversed (all the elements visited) an external Circular linked lists can be used to help the traverse the same list again and

pointer to the list must be preserved in order to be able to reference the list again.

again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.

In a circular linked list there are two methods to know if a node is the first Either a external pointer, list, points the first node or A header node is placed as the first node of the circular list. The header node can be separated from the others by either heaving a sentinel

node.

value as the info part or having a dedicated flag variable to specify if the node is a header node or not.

EXPERIMENT NO. 6A

AIM: WAP to implement Doubly Grounded Linked List THEORY:

In a doubly-linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, or next and prev(ious).

Double-linked lists require more space per node and their elementary operations are more expensive; but they are often easier to manipulate because they allow sequential access to the list in both directions. In particular, one can insert or delete a node in a constant number of operations given only that node's address. To do the same in a singly-linked list, one must have the previous node's address. Some algorithms require access in both directions. On the other hand, doubly-linked lists do not allow tail-sharing and cannot be used as persistent data structures.

. Two variables, say head and tail, are used to keep track of the list elements. One of them refers to the first element of the list, the other refers to the last. The first element of the list has no predecessor, therefore that reference is null. Similarly, the last element has no successor and the corresponding reference is also null. In effect, we have two overlapping singly-linked lists which go in opposite directions. In an empty list, the head and tail variables are both null.

Insertion in doubly linked list:

Deletion in doubly linked list:

EXPERIMENT NO. 6B

AIM: WAP to implement Doubly Circular Linked List THEORY:

A circular list is formed by making use of variables which would otherwise be null: The last element of the list is made the predecessor of the first element; the first element, the successor of the last. The upshot is that we no longer need both a head and tail variable to keep track of the list. Even if only a single variable is used, both the first and the last list elements can be found in constant time.

Finally, Figure (c) shows a circular, doubly-linked list which has a single sentinel. This variation is similar to the preceding one in that both the first and the last list elements can be found in constant time. This variation has the advantage that no special cases are required when dealing with an empty list. Figure shows that the empty list is represented by a list with exactly one element--the sentinel. In the case of the empty list, the sentinel is both is own successor and predecessor. Since the sentinel is always present, and since it always has both a successor and a predecessor, the code for adding elements to the empty list is identical to that for adding elements to a non-empty list.

EXPERIMENT NO. 7
AIM: WAP to implement convert INFIX to POSTFIX THEORY:

Consider the sum of A and B. We think of applying the operator + to the operands A and B to write the sum as A+B. This particular representation is called Infix notation. There are two alternate notations for expressing the sum of A and B using the symbols A,B and +.These are +AB AB+ Prefix notation Postfix notation

The prefixes pre-, post- and in- refer to the relative position of the operator with respect to the two operands. In Prefix notation the operator precede the two operands, In Postfix notation the operator follows the two operands and In Infix notation the operator is between the two operands. The evaluation of the expression A + B * C, as written in standard infix notation, requires knowledge of which of the two operations, + or *, is to be performed first. In the case of + and *, we know that multiplication is to be done before addition (in the absence of parenthesis to the contrary). Thus A+B*C is interpreted as A + ( B * C ) unless otherwise specified. We say that multiplication takes precedence over addition. Suppose that we want to rewrite A + B * C in postfix. Applying the rules of precedence, we first convert the

portion of the expression that is evaluated first, namely the multiplication. By doing this conversion in stages, we obtain : A + ( B * C ) Paranthesis for emphasis A + ( BC* ) Convert the multiplication A ( BC* ) + Convert the addition ABC*+ Postfix form

The only rules to remember during the conversion process are thatoperations with highest precedence are converted first and that after a portion of the expression has been converted to postfix, it is to be treated as a single operand. Consider the same example with the precedence of operators reversed by the deliberate insertion of paranthesis.

( A + B ) * C Infix form ( AB+ ) * C Convert the addition ( AB+ ) C * Convert the multiplication AB+C* Postfix form

In the above example the addition is converted before the multiplication because of the parenthesis. In going from ( A + B ) * C to ( AB+ ) * C, A and B are the operands and + is the operator. In going from ( AB+ ) * C to ( AB+ ) C *, ( AB+ ) and C are the operands and * is the operator. The rules for converting from infix to postfix are simple, providing that you know the order of precedence.

EXPERIMENT NO. 8
AIM: WAP to implement BINARY Search Tree THEORY:

A binary search tree (BST) or ordered binary tree is a node-based binary tree data structure which has the following properties:[1]

The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's

key.

Both the left and right subtrees must also be binary search trees.

Generally, the information represented by each node is a record rather than a single data element. However, for sequencing purposes, nodes are compared according to their keys rather than any part of their associated records. The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient. Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets, multisets, and associative arrays.

EXPERIMENT NO. 9A

AIM: WAP to implement Depth first Search(DFS) THEORY:


DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the searchbacktracks, returning to the most recent node it hasn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a stack for exploration. The time and space analysis of DFS differs according to its application area. In theoretical computer science, DFS is typically used to traverse an entire graph, and takes time O(| V| + |E|), linear in the size of the graph. In these applications it also uses space O(|V|) in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices. Thus, in this setting, the time and space bounds are the same as forbreadth first search and the choice of which of these two algorithms to use depends less on their complexity and more on the different properties of the vertex orderings the two algorithms produce.

EXPERIMENT NO. 9B
AIM: WAP to implement Breadth first Search(BFS) THEORY:

BFS is an uninformed search method that aims to expand and examine all nodes of a graph or combination of sequences by systematically searching through every solution. In other words, it exhaustively searches the entire graph or sequence without considering the goal until it finds it. It does not use a heuristic algorithm.

From the standpoint of the algorithm, all child nodes obtained by expanding a node are added to a FIFO (i.e., First In, First Out) queue. In typical implementations, nodes that have not yet been examined for their neighbors are placed in some container (such as a queue or linked list) called "open" and then once examined are placed in the container "closed".

EXPERIMENT NO. 10
AIM: WAP to implement addition of two polynomials (i) (ii) THEORY: using arrays using linked list

Das könnte Ihnen auch gefallen