OOPS & DS Virtual Lab

download OOPS & DS Virtual Lab

of 50

Transcript of OOPS & DS Virtual Lab

  • 7/26/2019 OOPS & DS Virtual Lab

    1/50

    Exp No.1 Sorting using Arrays

    Introduction

    Sorting AlgorithmsSorting 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 nderstand 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

    ! "ivide the n element input into two n#$ element sequences.

    ! Sort the n#$ element sequences recursively.

    ! %erge the two n#$ element sequences into a sorted n element sequence.

    &. Procedure %erge'(,),*+ &. i &, - &

    $. for k & to l r do

    /. if (0i1 2 )0-1 then

    3. *0k1 (0i14 i i &

    5. else

    6. *0k1 )0-14 - - &47. 8nd

    !uic" Sort Algorithm

    1

  • 7/26/2019 OOPS & DS Virtual Lab

    2/50

    *lgorithms based on the divide and conquer strategy called the quick sort that can

    sort in place.

    ! "ivide9 "ivide the input into / parts (, 8, and ) where ( 2 8 2 ) based on a

    pivot.

    ! :onquer9 Solve the sorting problem recursively on ( and ). *ssuming that all

    the items are distinct, so that ;8; & hence already sorted.

    ! :ombine9 Produce the sorted (, 8, and ) in that order.

    &. Procedure Parition'*, l, h+

    $. pivot *0h14

    /. i l < &4 3. for - p to h < & do

    5. if *0-1 2 pivot

    6. i i &4

    7. swap *0i1 with *0-1

    =. swap *0i &1 with *0h1

    >. 8nd Procedure

    ?iven a pivot, partition the elements of the array such that the resulting array consists of9

    &. @ne sub

  • 7/26/2019 OOPS & DS Virtual Lab

    3/50

    Best case9

    3

  • 7/26/2019 OOPS & DS Virtual Lab

    4/50

    4

  • 7/26/2019 OOPS & DS Virtual Lab

    5/50

    5

  • 7/26/2019 OOPS & DS Virtual Lab

    6/50

    6

  • 7/26/2019 OOPS & DS Virtual Lab

    7/50

    7

  • 7/26/2019 OOPS & DS Virtual Lab

    8/50

    8

  • 7/26/2019 OOPS & DS Virtual Lab

    9/50

    9

  • 7/26/2019 OOPS & DS Virtual Lab

    10/50

  • 7/26/2019 OOPS & DS Virtual Lab

    11/50

    Corst :ase9

    11

  • 7/26/2019 OOPS & DS Virtual Lab

    12/50

    12

  • 7/26/2019 OOPS & DS Virtual Lab

    13/50

    13

  • 7/26/2019 OOPS & DS Virtual Lab

    14/50

    14

  • 7/26/2019 OOPS & DS Virtual Lab

    15/50

    15

  • 7/26/2019 OOPS & DS Virtual Lab

    16/50

    16

  • 7/26/2019 OOPS & DS Virtual Lab

    17/50

    17

  • 7/26/2019 OOPS & DS Virtual Lab

    18/50

    8nter random input array of n integers to sort9

    18

  • 7/26/2019 OOPS & DS Virtual Lab

    19/50

    19

  • 7/26/2019 OOPS & DS Virtual Lab

    20/50

    20

  • 7/26/2019 OOPS & DS Virtual Lab

    21/50

    21

  • 7/26/2019 OOPS & DS Virtual Lab

    22/50

    22

  • 7/26/2019 OOPS & DS Virtual Lab

    23/50

    23

  • 7/26/2019 OOPS & DS Virtual Lab

    24/50

    24

  • 7/26/2019 OOPS & DS Virtual Lab

    25/50

    25

  • 7/26/2019 OOPS & DS Virtual Lab

    26/50

    26

  • 7/26/2019 OOPS & DS Virtual Lab

    27/50

    27

  • 7/26/2019 OOPS & DS Virtual Lab

    28/50

    28

  • 7/26/2019 OOPS & DS Virtual Lab

    29/50

    29

  • 7/26/2019 OOPS & DS Virtual Lab

    30/50

    30

  • 7/26/2019 OOPS & DS Virtual Lab

    31/50

    31

  • 7/26/2019 OOPS & DS Virtual Lab

    32/50

    32

  • 7/26/2019 OOPS & DS Virtual Lab

    33/50

    #ES$%&

    The Sorting using *rrays was executed successfully.

    Exp No.' Expression Evaluation using Stac"s

    Introduction

    33

  • 7/26/2019 OOPS & DS Virtual Lab

    34/50

    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. 8xamples 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. * 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. se 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

    ?oogle which can do this for most expressions.

    &he Stac" (ata Structure

    The stack data structure is characteriDed by its last

  • 7/26/2019 OOPS & DS Virtual Lab

    35/50

    Applications o) Stac"s

    Expression Evaluation

    @ne of the prominent applications of stacks is to expression evaluation. :onsider a table

    calculator which evaluates arithmetic expressions involving addition, multiplication, subtraction,

    and division. Gor example, $/ H 5 < 7 is a valid expression. The result of the above expression is

    & as multiplication has precedence over addition and subtraction. To disambiguate, one also

    uses parenthesis and write the same expression as $ '/H5+ < 7. Jowever, it would be quite

    cumbersome to use parentheses when especially, the precedence is known. Jence, one needs to

    first convert a given expression into a non

  • 7/26/2019 OOPS & DS Virtual Lab

    36/50

    such as a queue at a ticket reservation office, an operating system -ob queue, or a queue of aero

    planes ready to take off. In all such settings, the element or the ob-ect that is first inserted is the

    first one to come out of the queue also. Thus, the queue is a Girst

  • 7/26/2019 OOPS & DS Virtual Lab

    37/50

    37

  • 7/26/2019 OOPS & DS Virtual Lab

    38/50

    38

  • 7/26/2019 OOPS & DS Virtual Lab

    39/50

    39

  • 7/26/2019 OOPS & DS Virtual Lab

    40/50

    40

  • 7/26/2019 OOPS & DS Virtual Lab

    41/50

    41

  • 7/26/2019 OOPS & DS Virtual Lab

    42/50

    42

  • 7/26/2019 OOPS & DS Virtual Lab

    43/50

    43

  • 7/26/2019 OOPS & DS Virtual Lab

    44/50

  • 7/26/2019 OOPS & DS Virtual Lab

    45/50

    45

  • 7/26/2019 OOPS & DS Virtual Lab

    46/50

    46

  • 7/26/2019 OOPS & DS Virtual Lab

    47/50

    47

  • 7/26/2019 OOPS & DS Virtual Lab

    48/50

    48

  • 7/26/2019 OOPS & DS Virtual Lab

    49/50

    49

  • 7/26/2019 OOPS & DS Virtual Lab

    50/50

    #ES$%&

    The 8xpression 8valuation using Stacks was simulated successfully.