Welcome To All: Data Structures and Algorithm

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Welcome to All

Data Structures and Algorithm


Subject Name : Data Structures and
Algorithm
Subject Code : 16SCCCA5/16SCCCS5/16SCCIT5

Class : III BCA/III B.Sc C.S/III B.Sc IT


Sub. Incharge : Dr.K.Bhuvaneswari
Assistant Professor
Dept. of BCA

Idhaya College for Women


Kumbakonam
Unit – III : Algorithms – Priority Queues - Heaps –
Heap Sort – Merge Sort – Quick Sort – Binary Search –
Finding the Maximum and Minimum.
Definition of Algorithm
• An algorithm is a finite set of instructions that if followed
accomplishes a particular task.

• All algorithms must satisfy the following criteria:


1.Input.Zero or more quantities are externally supplied.
2. Output. At least one quantity is produced.
3.Definiteness. Each instruction is clear and unambiguous.
4. Finiteness. It should have finite number of steps.
5.Effectiveness. Every instruction must be very basic. It
should be feasible.
Important and active areas of research

• How to device algorithm


• How to validate algorithm
• How to analyze algorithm
• How to test a program

Algorithm Specification
• Pseudocode Conventions
• Recursive Algorithms
Pseudocode Conventions
It contains some important functions.
i. Comments begin with // and continue until the end of line.
ii. Blocks are indicated with matching braces { and }.
Statements are delimited by ;
iii. An identifier begins with a letter. Variable may be global
(or) local compound data types can be formed with records.
iv. Assignment of values to variables are assign with =
<variable> := <expression>;
v. There are two boolean values true and false.
Logical operators and, or, not and operators <,<,=,=,>, and >
are provided
vi. Arrays are denoted as [ ]
vii. Looping statements for, while & repeat-until.
viii. Condition statements if & if-else
ix. Input and output are done using instructions read and write

Eg. Algorithm finds and returns the maximum of n given


numbers:
1 Algorithm Max(A, n)
2 // A is an array of size n.
3 {
4 Result := A[ l ];
5 for i :=2 to n do
6 if A[i] > Result then Result :=A[i];
7 return Result;
8 }
Recursive Algorithms:
• A recursive function is a function that is defined in terms of
itself.
• An algorithm is said to be recursive. If the same algorithm is
invoked in the body.
• An algorithm that calls itself is direct recursive.
• Algorithm A is said to be indirect recursive if it calls another
algorithm

Example:
• Towers of Hanoi
• Permutation Generator
Towers of Hanoi
• Towers of Hanoi puzzle, there was a diamond tower with 64
golden disk (A)
• Besides this tower there were two other diamond tower
(B & C)
• Brahman move the disks from A to tower B using tower C for
intermediate storage.
• The number of disks is n. To get the largest disk to the bottom
of tower B,
• we move the remaining n disks to tower C and then move the
largest to tower B.
• Now we are left with the task of moving the disks from tower
C to tower B. To do this, we have towers A and B available.
• Tower B has a disk on it can be ignored as the disk is larger
than the disks being moved from tower C and so any disk can
be placed on top of it.
Permutation Generator:
• Given a set of n > 1 elements.
• Problem: print all possible permutations of this set.
Eg. The set is { a, b, c }
Set of permutations is
{ (a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)
}

1. a followed by all the permutations of (b,c,d)


2. b followed by all the permutations of (a,c,d)
3. C followed by all the permutations of (a,b,d)
4. d followed by all the permutations of (a,b,c)
Priority queue
Any data structure that supports the operations of search
min (or max), insert, and delete min (or max, respectively) is
called a priority queue.

Sorting
Sorting arranges data in a sequence which makes searching
easier. Every record which is going to be sorted will contain one
key. Based on the key the record will be sorted.
Sorting Techniques:
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
Thank you

You might also like