27
Thinking in Data Structures Tushar B Kute http://www.tusharkute.com

Thinking in data structures

Embed Size (px)

DESCRIPTION

Thinking in Data Structures

Citation preview

Page 1: Thinking in data structures

Thinking in Data Structures

Tushar B Kutehttp://www.tusharkute.com

Page 2: Thinking in data structures

www.tusharkute.com 2

Data Structure

● What is the "Data Structure" ?

– Ways to represent data.

– In a general sense, any data representation is a data structure. Example: An integer more typically, a data structure is meant to be an organization for a collection of data items.

● Why data structure ?

– To design and implement large-scale computer system

– Have proven correct algorithms

– The art of programming● How to master in data structure ?

– practice, discuss, and think

Page 3: Thinking in data structures

www.tusharkute.com 3

Need of data structures

● Data structures organize data

– More efficient programs.● More powerful computers

– More complex applications.● More complex applications demand more calculations.

● Complex computing tasks are unlike our everyday experience.

Page 4: Thinking in data structures

www.tusharkute.com 4

List of data structures

● Static

– Array

– Stack

– Queue● Dynamic

– Linked list

– Tree

– Graph

Page 5: Thinking in data structures

www.tusharkute.com 5

Choosing a data structure

int p[10], i=0;

while(1)

{

scanf(“%d”, &p[i]);

i++;

}

int *p, i=0;

while(1)

{

scanf(“%d”, &p[i]);

i++;

}

Page 6: Thinking in data structures

www.tusharkute.com 6

System life cycle

● Summary

– R A D R C V● Requirements

– What inputs, functions, and outputs.● Analysis

– Break the problem down into manageable pieces.

– Top-down approach.

– Bottom-up approach.

Page 7: Thinking in data structures

www.tusharkute.com 7

System life cycle

● Design

– Create abstract data types and the algorithm specifications, language independent.

● Refinement and Coding

– Determining data structures and algorithms.● Verification

– Developing correctness proofs, testing the program, and removing errors.

Page 8: Thinking in data structures

www.tusharkute.com 8

Efficiency

● A solution is said to be efficient if it solves the problem within its resource constraints.

– Space

– Time● The cost of a solution is the amount of resources that the

solution consumes.

Page 9: Thinking in data structures

www.tusharkute.com 9

Data Structure philosophy

● Each data structure has costs and benefits.

● Rarely is one data structure better than another in all situations.

● A data structure requires:

– space for each data item it stores,

– time to perform each basic operation,● Programming effort.

Page 10: Thinking in data structures

www.tusharkute.com 10

Data structure philosophy

● Each problem has constraints on available space and time.

● Only after a careful analysis of problem characteristics can we know the best data structure for the task.

● Bank example:

– Start account: a few minutes

– Transactions: a few seconds

– Close account: overnight

Page 11: Thinking in data structures

www.tusharkute.com 11

Example.

for(a=0; a>10; a++) //loop-1

{

printf(“Hello World...”);

}

for(a=0; a<10; a++) //loop-2

{

printf(“Hello World...”);

}

Page 12: Thinking in data structures

www.tusharkute.com 12

Example.

for(a=0; a<=10; a++) //loop-3

{

printf(“Hello World...”);

}

for(a=0; a!=10; a++) //loop-4

{

printf(“Hello World...”);

}

Page 13: Thinking in data structures

www.tusharkute.com 13

Example: check for prime number

flag=0;

for(a=2;a<num;a++)

{

if(num%a==0)

flag=1;

}

if(flag==1)

printf(“Number is not prime.”);

else

printf(“Number is prime.”);

Page 14: Thinking in data structures

www.tusharkute.com 14

Refinement-1

flag=0;

for(a=2;a<num;a++)

{

if(num%a==0) {

flag=1;break;

}

}

if(flag==1)

printf(“Number is not prime.”);

else

printf(“Number is prime.”);

Page 15: Thinking in data structures

www.tusharkute.com 15

Refinement-2

flag=0;

for(a=2;a<num/2;a++)

{

if(num%a==0) {

flag=1;break;

}

}

if(flag==1)

printf(“Number is not prime.”);

else

printf(“Number is prime.”);

Page 16: Thinking in data structures

www.tusharkute.com 16

Refinement-3

for(a=2;a<num/2;a++)

{

if(num%a==0)

break;}

if(a==(num/2))

printf(“Number is prime.”);

else

printf(“Number is not prime.”);

Page 17: Thinking in data structures

www.tusharkute.com 17

Example: swapping of two numbers, Way-1

a=13, b=29;

temp = a;

a = b;

b = temp;

Page 18: Thinking in data structures

www.tusharkute.com 18

Way-2

a=13, b=29;

a = a + b;

a = a – b;

b = a – b;

Page 19: Thinking in data structures

www.tusharkute.com 19

Way-3

a=13, b=29;

a = a ^ b;

b = a ^ b;

a = a ^ b;

or

a^=b^=a^=b;

Page 20: Thinking in data structures

www.tusharkute.com 20

Worst / Average / Best case

● Worst-case running time of an algorithm

– The longest running time for any input of size n

– An upper bound on the running time for any input● Guarantee that the algorithm will never take longer

– Example: Sort a set of numbers in increasing order; and the data is in decreasing order

– The worst case can occur fairly often

– E.g. in searching a database for a particular piece of information● Best-case running time

– Sort a set of numbers in increasing order; and the data is already in increasing order

● Average-case running time

– May be difficult to define what “average” means

Page 21: Thinking in data structures

www.tusharkute.com 21

Example: searching in database

● Best case: O(1)

● Worst case: O(n)

● Average case: O(n/2)

Page 22: Thinking in data structures

www.tusharkute.com 22

Running time of algorithms

● Bounds are for the algorithms, rather than programs

– Programs are just implementations of an algorithm, and almost always the details of the program do not affect the bounds

● Bounds are for algorithms, rather than problems

– A problem can be solved with several algorithms, some are more efficient than others

Page 23: Thinking in data structures

www.tusharkute.com 23

Describing algorithms

● Natural language

– English, Chinese

– Instructions must be definite and effectiveness.● Graphic representation

– Flowchart● Work well only if the algorithm is small and simple.

● Pseudo language

– Readable● Instructions must be definite and effectiveness.

● Combining English and C

– Simple and Tough task to do.

Page 24: Thinking in data structures

www.tusharkute.com 24

Algorithm and programs

● Algorithm: a method or a process followed to solve a problem.

– A recipe: The algorithm gives us a “recipe” for solving the problem by performing a series of steps, where each step is completely understood.

● An algorithm takes the input to a problem (function) and transforms it to the output.

– A mapping of input to output.● A problem can be solved by many algorithms.

Page 25: Thinking in data structures

www.tusharkute.com 25

A problem can have many solutions

● For example, the problem of sorting can be solved by the following algorithms:

– Insertion sort

– Bubble sort

– Selection sort

– Shell sort

– Merge sort

– Radix sort

– Merge sort

– Quick sort

Page 26: Thinking in data structures

www.tusharkute.com 26

Algorithm properties

● An algorithm possesses the following properties:

– It must be correct.

– It must be composed of a series of concrete steps.

– There can be no ambiguity as to which step will be performed next.

– It must be composed of a finite number of steps.

– It must terminate.● A computer program is an instance, or concrete

representation, for an algorithm in some programming language.

Page 27: Thinking in data structures

www.tusharkute.com 27

Thank you

This presentation is created using LibreOffice Impress 3.6.2.2