You are on page 1of 50

Exp No.

Sorting using Arrays

Introduction
Sorting Algorithms
Sorting is a fundamental task in computing. There are several sorting algorithms
developed decades ago, but new algorithms continue to appear. In this experiment, the focus is to
study a few classical sorting algorithms.
Primarily be using arrays for our algorithms as it is simple and illustrative to do so. It
will cover merge sort, insertion sort, and quick sort in this experiment.
Objective

To Understand various sorting algorithms using arrays

AIM
To simulate various sorting algorithms using arrays.
Algorithms
Merge Sort Algorithm:
The idea of merge sort is to shift the onus to the combine step. To this end, we
come up with the following design
Divide the n element input into two n/2 element sequences.
Sort the n/2 element sequences recursively.
Merge the two n/2 element sequences into a sorted n element sequence.
1. Procedure Merge(L,R,A) 1. i = 1, j = 1
2. for k = 1 to l + r do
3. if L[i] < R[j] then
4. A[k] = L[i]; i = i + 1
5. else
6. A[k] = R[j]; j = j + 1;
7. End

Quick Sort Algorithm:


1

Algorithms based on the divide and conquer strategy called the quick sort that can
sort in place.
Divide: Divide the input into 3 parts L, E, and R where L < E < R based on a
pivot.
Conquer: Solve the sorting problem recursively on L and R. Assuming that all
the items are distinct, so that |E| = 1 hence already sorted.
Combine: Produce the sorted L, E, and R in that order.
1. Procedure Parition(A, l, h)
2. pivot = A[h];
3. i = l - 1; 4. for j = p to h - 1 do
5. if A[j] <= pivot
6. i = i + 1;
7. swap A[i] with A[j]
8. swap A[i + 1] with A[h]
9. End Procedure

Given a pivot, partition the elements of the array such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array.

Best case:

10

Worst Case:

11

12

13

14

15

16

17

Enter random input array of n integers to sort:

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

RESULT
The Sorting using Arrays was executed successfully.

Exp No.2

Expression Evaluation using Stacks

Introduction
33

Expression Evaluation
This experiment guides you using stacks in a practical example. Stacks are an important
data structure and find applications where essentially the data item that is stored latest is what
needs to be accessed first. Examples of applications of stacks in natural settings include a stack
of books on a table. Typically, as you pile books on a desk, the book that you can access instantly
is the one that you placed at the latest time. A good computing application of stacks is to use it to
evaluating arithmetical expressions. In this experiment, we will go through the entire expression
evaluation process. The attached notes and the slides describe the technical details. The
animation can help you understand the process via several examples. Use these resources to
develop code for your own expression evaluator. To add a good interface, you can consider a
small web interface where in one can type an expression and ask for it to be evaluated. Think of
Google which can do this for most expressions.
The Stack Data Structure
The stack data structure is characterized by its last-in-first-out access mechanism. Thus,
the element that is added most recently will be the element that is removed first on a remove call.
Such an order of addition and deletion of items occurs in several natural settings. Consider for
example, a stack of plates in a cafeteria. The first plate that is taken out is the plate that is on the
top of the pile. This plate would be the one that is added to the pile most recently.
Another example could be as follows. We are all used to text editors that allow for undo
and redo operations. In such an editor, suppose S is the present text and a word w1 is deleted to
get S. Then a word w2 is deleted. Now, the user wishes to undo some of the changes. To get the
correct result, it is possible only if all the operations done after w1 is deleted are also undone.
Moreover, these operations must be performed in their reverse order. Thus, we need a
mechanism to associate a stored set of items. Irrespective of the implementation mechanism, the
stack data structure has the following basic operations. Let S be a stack.

create :Create an empty stack.

push(element) : Pushes the item element to the top of the stack.

pop() : Returns the item that is most recently pushed.

size() : Return the size of the stack.


34

Applications of Stacks
Expression Evaluation
One of the prominent applications of stacks is to expression evaluation. Consider a table
calculator which evaluates arithmetic expressions involving addition, multiplication, subtraction,
and division. For example, 2+3 * 5 - 7 is a valid expression. The result of the above expression is
10 as multiplication has precedence over addition and subtraction. To disambiguate, one also
uses parenthesis and write the same expression as 2+ (3*5) - 7. However, it would be quite
cumbersome to use parentheses when especially, the precedence is known. Hence, one needs to
first convert a given expression into a non-ambiguous model so that evaluation can be done
easily. There are three ways to write an expression. The above way of writing expressions is
called the infix notation because the operators are placed in between the two operators. There are
other ways of writing an expression. In the prefix notation, operators precede the operands. So
the above expression would be written as -+*3 5 2 7. In the postfix notation, the operators are
written after the operands. The postfix equivalent way of writing the above expression would be
3 5* 2 + 7 -. It turns out that the postfix and prefix notations are free of ambiguity. We will how
that is the case first and then see how to convert a given expression in the infix form to its prefix
form.
Evaluating a Postfix Expression
Consider an expression given in the postfix notation. We now see how to evaluate such
an expression. For example, ab * c + is a postfix expression. Since the operators follow the
operands, it is intuitive to see if the previously available two operands are the corresponding
operands for a given operator. This intuition serves well and is correct. So, when processing a
postfix expression from left to right, when we encounter an operator, we have to apply the
operation to the two most recent operands. This suggests that the operands should be placed on a
stack.

The Queue Data Structure


In the previous section, we have seen a new way of access model to the array can result
in good solutions to important applications. A queue is another such data structure which
supports operations such as insert and delete. One can think of analogies to several situations
35

such as a queue at a ticket reservation office, an operating system job queue, or a queue of aero
planes ready to take off. In all such settings, the element or the object that is first inserted is the
first one to come out of the queue also. Thus, the queue is a First-In- First-Out (FIFO) data
structure. This suggests that there are two quantities associated with a queue, the front and the
rear. The rear indicates the position where the new elements would be added. The front indicates
the position from where elements can be deleted from the queue. Formally, a queue is a data
structure which supports the following operations.

& insert: Insert an element to the rear of the queue

delete: Delete an element from the front of the queue.

size: Return the size of the queue

Objective

To understand the Use stacks to evaluate expressions.

AIM
To Simulate the Use stacks to evaluate expressions.
PROCEDURE
STEP1:

36

37

38

39

40

41

42

43

44

45

46

47

48

49

RESULT
The Expression Evaluation using Stacks was simulated successfully.

50

You might also like