SlideShare uma empresa Scribd logo
1 de 107
Data Structure and Algorithm
Prepared by Basharat Jehan
Email: Basharat_jehan@yahoo.com
Data
• Facts or information used usually to calculate,
analyze, or plan something.
OR
• Data are simply collection of facts and figures.
Data are values or set of values.
OR
The quantities, characters, or symbols on which
operations are performed by a computer, being
stored and transmitted in the form of electrical
signals and recorded on magnetic, optical, or
mechanical recording media.
• In computing, data is information that has
been translated into a form that is more
convenient to move or process.
• Data is collected and analyzed to create
information suitable for making decisions
• Data items that are divided into sub items are
group items;
• those that are not are called elementary
items. For example, a student’s name may be
divided into three sub items – [first name,
middle name and last name]
• but the ID of a student would normally be
treated as a single item.
Information
• Knowledge that you get about someone or
something.
• knowledge obtained from investigation, study, or
instruction (2) : intelligence, news (3) : facts,
data .
• Data are converted to information after
Processing.
• knowledge gained through study,
communication, research, instruction, etc.;
factual data:
• Data that is (1) accurate and timely, (2)
specific and organized for a purpose, (3)
presented within a context that gives it
meaning and relevance, and (4) can lead to an
increase in understanding and decrease in
uncertainty.
Field, Record and File
Data Structure
• In computer science, a data structure is a
particular way of organizing data in a
computer so that it can be used efficiently.
Algorithm Notations
Physical and Logical Data Structures
• A physical data structure refers to the actual
organization of data on a storage device. The
logical data structure refers to how the
information appears to a program or user. For
example, a data file is a collection of
information stored together. This is its logical
structure. Physically, however, a file could be
stored on a disk in several scattered pieces.
ALGORITHM
INTRODUCTION
• An algorithm is an effective method expressed
as a finite list of well-defined instructions for
calculating a function
COMPUTER ALGORITHMS
• In computer systems, an algorithm is basically
an instance of logic written in software by
software developers to be effective for the
intended "target" computer(s) for the target
machines to produce output from
given input (perhaps null)
DEFINITION
• An algorithm is a finite sequence of step by
step, discrete, unambiguous instructions for
solving a particular problem
– has input data, and is expected to produce output
data
– each instruction can be carried out in a finite
amount of time in a deterministic way
Control structures
• Programs written in procedural languages, the
most common kind, are like recipes, having lists
of ingredients and step-by-step instructions for
using them. The three basic control structures in
virtually every procedural language are:
1. Sequence—combine the liquid ingredients, and
next add the dry ones.
2. Conditional—if the tomatoes are fresh then
simmer them, but if canned, skip this step.
3. Iterative—beat the egg whites until they form
soft peaks.
SEQUENCE
• it is the default control structure; instructions
are executed one after another. They might,
for example, carry out a series of arithmetic
operations, assigning results to variables, to
find the roots of a quadratic
equationax2 + bx + c = 0. The conditional IF-
THEN or IF-THEN-ELSE control structure allows
a program to follow alternative paths of
execution.
ITERATIVE/REPETITIVE FLOW
• Iteration (also called repetition) means a frame
that regulates the repeat of an action depending
on a condition. The action is called the body of
the iteration loop.
• This means that all low or high level algorithms
can be formulated as a series of structural
elements consisting only of the three types
above.
• Hence, for any algorithm description method it is
enough to be able to interpret the three types
above.
The flow diagram of the minimum
finding problem
ITERATION, OR LOOPING AND
CONDITIONAL FLOW
• It gives computers much of their power. They can
repeat a sequence of steps as often as necessary, and
appropriate repetitions of quite simple steps can solve
complex problems.
• These control structures can be combined. A sequence
may contain several loops; a loop may contain a loop
nested within it, or the two branches of a conditional
may each contain sequences with loops
and more conditionals. The following programming
fragment employs the IF-THEN structure for finding
one root of the quadratic equation, using the quadratic
formula:
Array data structure
• In computer science, an array data structure
or simply an array is a data structure
consisting of a collection of elements (values
or variables), each identified by at least one
array index or key.
FINDING LENGTH OF ARRY
• MAXIMUM LENGTH=
UPPER BOUND(UB)-LOWER BOUND(LB)+1
One dimensional (1-D) arrays or
Linear arrays:
• In it each element is represented by a single
subscript. The elements are stored in
consecutive memory locations. E.g. A [1], A
[2], ….., A [N].
• Or
• A(1), A(2)…
• A1,A2,…………
Multi dimensional arrays: Two
dimensional (2-D) arrays or Matrix
arrays
• In it each element is represented by two
subscripts. Thus a two dimensional m x n array
A has m rows and n columns and contains
m*n elements. It is also called matrix array
because in it the elements form a matrix. E.g.
A [2] [3] has 2 rows and 3 columns and 2*3 = 6
elements.
Traversing
• Traversing basically means the accessing the
each and every element of the array at least
once. Traversing is usually done to be aware of
the data elements which are present in the
array.
ALGORITHM FOR TRAVERSING
• SET K=LB
• REPEAT STEP 3 AND 4 WHILE K<=UB
• INPUT ARRAY[K]
• SET K=K+1
• END LOOP
• OUTPUT ARRAY[K]
• EXIT
A program to traverse or read a linear
or 1-D array
• #include<stdio.h>
#include<conio.h>
void main()
{
int a[7], i=0;
clrscr();
printf("Enter the elements of the array :");
for (i=0;i<=6 ;i++ )
{
scanf("%dn", &a[i]); /* taking input array from the user */
}
printf("nThe elements of the array you have enetered are as follows :")
for(i=0; i<=6; i++)
{
printf("%d", a[i]); /* printing the array elements or traversing the array */
}
getch();
}
Algorithm to find the sum of elements
of array
• Step#1 SUM=0
• Step#2 repeat for i=0 to UB
• [print the calculated sum]
• SUM=SUM+A[i]
• End of Loop
• Step#3 PRINT SUM
• Step#4 EXIT
Program
• #include<iostream.h>
• #include<conio.h>
• using namespace std;
• main()
• {
• int A[3], sum,i;
• sum=0;
• for(i=0; i<=2;i++)
• {
• cin>>A[i];
• sum=sum+A[i];
•
• }
• cout<<sum;
• getch();
• }
Accessing One Dimensional array by
Dope Vector method
• L(X[k])=Lo+C*(k-1)
• Where Lo is base address, C is the length of
each memory location, and k is index number.
• L(X[3]) = 200+2*(3-1)=204
Data address
45 200
45 202
56 204
67 206
88 208
representing 2D array in memory
Inserting
• Inserting: Adding a new record to the end of
array.
• Insertion may be at end of array or at specific
location.
• Adding a new record to the end of array.
• Algorithm
• REPEAT step 2 to 3 FOR I=4 to 9
• INPUT value in N
• Temp[I]= N
[End of step- 2 Loop]
Exit
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class insrt
• {
•
• private:
• int i, A[10];
• public:
• void input()
• {
• A[1]=10;
• A[2]=20;
• }
• void abc()
• {
• for(i=3; i<=10; i++)
• {
• cin>>A[i];
• }
• for(i=1; i<=10; i++)
• {
• cout<<A[i];
• }
• }};
• main()
• {
• insrt a;
• a.input();
• a.abc();
Element insertion at specific location
• Algorithm to insert element in array requires
the specific location at which the element is
to be added.
• This algorithm performs Insert Operation on
Array; it adds an ELEMENT at the given
location LOC in the array – DATA. In the first
step we initialized variable I with the
maximum number of element present in the
array (I:=N). After that with loop we shift the
element forward till we reach the location
LOC. In the next step, we insert the ELEMENT
at its location.
• #include<iostream>
• #include<conio.h>
• using namespace std;
• class bcs
• {
• private:
• int abc[5],i;
• public:
• void assign(int j)
• {
• for(int i=0; i<=j; i++)
• cin>>abc[i];
•
• void specific(int loc, int val)
• {
• for(int i=4; i>=loc; i--)
• abc[i+1]=abc[i];
• abc[loc]=val;
• }
• void print(int n)
• {
• for(int i=0; i<=n; i++)
• cout<<abc[i];
• }
• };
• main()
• {
• bcs obj;
• int n,pos;
• obj.assign(3);
• obj.print(3);
• cout<<"enter value to insert";
• cin>>n>>endl;
• cin>>pos;
• if(pos>=4)
• {
• cout<<"invalid location";
• return 0;
• }
• obj.specific(pos,n);
• obj.print(4);
• }
Useful link
• http://www.enjoylearningcs.com/array-
operation-insert-delete-element/
Delete Operation on Array
• To delete element from array we should know the
location from where we want to delete.
Algorithm to Delete Element from Array can be
executed at any location ie. at it beginning, in
middle or at the end of the array.
• Apply a loop from the location of item till the end
of the array. Shift the element forward. In this
algorithm, DATA is an array. ELEMENT is the value
which we want to delete from the location LOC. N
is the maximum number of element present in
the array DATA.
Algorithm to Delete Element from
Array is:
INTRODUCTION TO THE BASIC
OPERATIONS OF DATA STRUCTURE
• The data appearing in data structures are
processed by means of operations. The
following are operations are major operations
• Traversing: Accessing each record exactly once
so that certain items in the record may be
processed.
• Searching: Finding the location of the record
with a given key value, or finding the locations
of all records which satisfy one or more
conditions.
• Inserting: Adding a new record to the
structure.
• Deleting: Removing a record from the
structure. It is used to delete an existing data
item from the given collection of data items
•
Following two are special operations:
Following two are special operations:
• Sorting: Arranging the records in some logical
order. It is used to arrange the data items in
some order i.e. in ascending
or descending order in case of numerical
data and in dictionary order in case of
alphanumeric data
• Merging: Combining the records in two
different sorted files into a single sorted file.
Recursion Concept :
• Recursion is basic concept in Programming.
• When Function is defined in terms of itself
then it is called as “Recursion Function“.
• Recursive function is a function which
contains a call to itself.
59
Fibonacci Sequence
• The Fibonacci Sequence is the series of
numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The
next number is found by adding up the two
numbers before it. Similarly, the 3 is found by
adding the two numbers before it (1+2),
Fibonacci series program in c using
recursion
• #include<stdio.h>
•
• int Fibonacci(int);
•
• main()
• {
• int n, i = 0, c;
•
• cin>>n;
•
• cout<<"Fibonacci series;
•
• for ( c = 1 ; c <= n ; c++ )
• {
• cout<<Fibonacci(i);
• i++;
• }
•
• return 0;
• }
•
• int Fibonacci(int n)
• {
• if ( n == 0 )
• return 0;
• else if ( n == 1 )
• return 1;
• else
• return ( Fibonacci(n-1) + Fibonacci(n-2) );
• }
Searching Algorithms
• Necessary components to search a list of fdata
– Array containing the list
– Length of the list
– Item for which you are searching
• After search completed
– If item found, report “success,” return location in array
– If item not found, report “not found” or “failure”
Sequential Search
• In computer science, linear search or
sequential search is a method for finding a
particular value in a list that checks each
element in sequence until the desired element
is found or the list is exhausted. The list need
not be ordered.
• Suppose that you want to determine whether 27 is in the list
• First compare 27 with list[0]; that is, compare 27 with
35
• Because list[0] ≠ 27, you then compare 27 with
list[1]
• Because list[1] ≠ 27, you compare 27 with the next
element in the list
• Because list[2] = 27, the search stops
• This search is successful!
Searching Algorithms (Cont’d)
Figure 1: Array list with seven (07) elements
• Let’s now search for 10
• The search starts at the first element in the list; that
is, at list[0]
• Proceeding as before, we see that this time the
search item, which is 10, is compared with every
item in the list
• Eventually, no more data is left in the list to compare
with the search item; this is an unsuccessful search
Searching Algorithms (Cont’d)
Algorithm of Sequential Searching
• The complete algorithm for sequential search is
//list the elements to be searched
//target the value being searched for
//N the number of elements in the list
SequentialSearch( LIST, ITEM, N, LOC)
Set LOC=1
• for i = 1 to N do
• INPUT LIST[i]
• Repeat While(LOC<=N)
• if (ITEM = LIST[LOC])
• return LOC [Successful Search]
• LOC=LOC+1
• end if
• end While
• return 0 [Unsuccessful Search]
C++ Code for Sequential Search
• #include<iostream>
• #include<conio.h>
• using namespace std;
• main()
• {
• int A[10],i, LOC, ITEM,n;
• LOC=-1;
• cout<<"Enter size of array but less than 10";
• cin>>n;
• for (i=1; i<=n; i++)
• {
• cin>>A[i];
• }
• cout<<"item to be searched";
• cin>>ITEM;
• i=0;
• while(i<=n)
• {
• if (ITEM==A[i]){
• LOC=i;
• cout<<"location"<<LOC;
• }
• i++;
• }
• if(LOC==-1)
• {
•
• cout<<"data not found";
• }
• }
• Can only be performed on a sorted list !!!
• Uses divide and conquer technique to search list
Binary Search Algorithm
• Search item is compared with middle element of
list
• If search item < middle element of list, search is
restricted to first half of the list
• If search item > middle element of list, search
second half of the list
• If search item = middle element, search is
complete
Binary Search Algorithm (Cont’d)
• Determine whether 75 is in the list
Binary Search Algorithm (Cont’d)
Figure 2: Array list with twelve (12) elements
Figure 3: Search list, list[0] … list[11]
Binary Search Algorithm (Cont’d)
Figure 4: Search list, list[6] … list[11]
C++ program for binary search
• #include<iostream>
• #include<conio.h>
• using namespace std;
• main()
• {
• int a[10], loc,item,n,i;
• Loc=-1;
• cout<<"enter size of array"<<endl;
• cin>>n;
• for(i=1; i<=n; i++)
• {
• cin>>a[i];
•
• }
• for(i=1; i<=n; i++)
• {
• cout<<"array element"<<a[i]<<endl;
• }
•
• cout<<"enter item to be searched "<<endl;
• cin>>item;
• int middle=(1+n)/2;
• cout<<middle;
• if(item==a[middle])
• {
• cout<<middle;
• }
• if(item>a[middle])
• {
• i=middle+1;
• while(i<=n)
• {
• if(item==a[i])
• {
• Loc=i;
• cout<<loc;
• }
• i++;
• }}
•
• if(item<a[middle])
• {
• i=middle-1;
• while(i>0)
• {
• if(item==a[i])
• {
• i=loc;
• cout<<loc;
• }
• i--;
• }}
• if(loc==-1)
• {
• cout<<"item not found";
• }
• }
Sorting
• Definition is discussed already in previous
slides.. See data structure operations types
Bubble Sort
• The technique we use is called “Bubble Sort”
because the bigger value gradually bubbles
their way up to the top of array like air bubble
rising in water, while the small values sink to
the bottom of array.
• Algorithm: (Bubble Sort) BUBBLE (DATA, n)
• Here DATA is an Array with N elements. This algorithm sorts
the
• elements in DATA.
• 1. for(pass=n; pass>=1; pass--)
• 2. for(i=1; i<=pass; i++)
• 3. If DATA[i]>DATA[i+1], then:
• Interchange DATA[i] and DATA[i+1].
• [End of If Structure.]
• [End of inner loop.]
• [End of Step 1 outer loop.]
• 4. Exit.
C++ PROGRAMM FOR BUBBLE SORT
• #include<iostream>
• #include<conio.h>
• using namespace std;
• main()
• {
• int pass,n,i,temp;
• int a[10];
• cout<<"PROGRAM FOR BUBBLE SORT";
• cout<<"Enter size of array which is less than 10";
• cin>>n;
• for(i=1; i<=n; i++)
• {
• cin>>a[i];
• }
•
• for(pass=n; pass>=1; pass--)
•
• for(i=1; i<=pass; i++)
•
• if(a[i]>a[i+1])
• {
• temp=a[i+1];
• a[i+1]=a[i];
• a[i]=temp;
• }
•
• for(i=1; i<=n; i++)
• {
• cout<<a[i];
• }
•
• }
Insertion Sort
• Sorting method in which algorithm scan a
whole list one by one and in each iteration the
algorithm place each number to its correct
position.
example
algorithm
See example animated example at
• http://courses.cs.vt.edu/csonline/Algorithms/
Lessons/InsertionCardSort/insertioncardsort.s
wf
STACKS
• It is an ordered group of homogeneous items
of elements. Elements are added to and
removed from the top of the stack (the most
recently added items are at the top of the
stack). The last element to be added is the
first to be removed (LIFO: Last In, First Out).
• A stack is a list of elements in which an
element may be inserted or deleted only at
• one end, called TOP of the stack. The
elements are removed in reverse order of
that in which they were inserted into the
stack.
• Basic operations:
• These are two basic operations associated
with stack:
• Push() is the term used to insert/add an
element into a stack.
• Pop() is the term used to delete/remove an
element from a stack.
Stack push and pop program
• #include<iostream>
• #include<conio.h>
• using namespace std;
• main()
• {
• int s[10], i,j,n;
• int top=0;
• do
• {
• cin>>j;
• s[top+1]=j;
• for(j=top+1; j>=1;j--)
• cout<<s[j]<<endl;
• top++;
• cout<<"do you want to process more
y/n"<<endl;
• }
• while(top!=11);
• cout<<"stack full";
• // pop operation
• s[top]=NULL;
• for(j=top; j>=1;j--)
• cout<<s[j]<<endl;
• }
Queue
• A queue is a linear list of elements in which deletion
can take place only at one end, called the front, and
insertions can take place only at the other end, called
• the rear. The term “front” and “rear” are used in
describing a linear list only when it
• is implemented as a queue.
• Queue is also called first-in-first-out (FIFO) lists. Since
the first element in a
• queue will be the first element out of the queue. In
other words, the order in which
• elements enters a queue is the order in which they
leave.
• Primary queue operations:
• Enqueue: insert an element at the rear of the
queue.
• Dequeue: remove an element from the front
of the queue.
Insertion Algorithm in Queue
107
Deques
• A deque is a double-ended queue
• Insertions and deletions can occur at either
end
• Implementation is similar to that for queues
• Deques are not heavily used
• You should know what a deque is, but we
won’t explore them much further

Mais conteúdo relacionado

Mais procurados (20)

Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Java Stack Data Structure.pptx
Java Stack Data Structure.pptxJava Stack Data Structure.pptx
Java Stack Data Structure.pptx
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Queues
QueuesQueues
Queues
 
stack & queue
stack & queuestack & queue
stack & queue
 
Recursion
RecursionRecursion
Recursion
 
Hashing
HashingHashing
Hashing
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Linked List
Linked ListLinked List
Linked List
 
Array ppt
Array pptArray ppt
Array ppt
 
Hashing
HashingHashing
Hashing
 

Destaque

Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithmPratik Mota
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introductionSugandh Wafai
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
Chapter 2.2 data structures
Chapter 2.2 data structuresChapter 2.2 data structures
Chapter 2.2 data structuressshhzap
 
GOOGLE ALGORITHM - 2014 A LOOK SEO
GOOGLE ALGORITHM - 2014 A LOOK SEOGOOGLE ALGORITHM - 2014 A LOOK SEO
GOOGLE ALGORITHM - 2014 A LOOK SEOVENKATESH S
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithmTrupti Agrawal
 
AP Computer Science Test Prep - Part 3 - Data Structure & Algorithm
AP Computer Science Test Prep - Part 3 - Data Structure & AlgorithmAP Computer Science Test Prep - Part 3 - Data Structure & Algorithm
AP Computer Science Test Prep - Part 3 - Data Structure & AlgorithmNR Computer Learning Center
 
Security Compensation - How to Invest in Start-Up Security
Security Compensation - How to Invest in Start-Up SecuritySecurity Compensation - How to Invest in Start-Up Security
Security Compensation - How to Invest in Start-Up SecurityChristopher Grayson
 
Meaningful Elearning with Digital Badges & Missions
Meaningful Elearning with Digital Badges & MissionsMeaningful Elearning with Digital Badges & Missions
Meaningful Elearning with Digital Badges & MissionsShelly Sanchez Terrell
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting AlgorithmAl Amin
 
Editable honduras power point map with capital and flag templates slides outl...
Editable honduras power point map with capital and flag templates slides outl...Editable honduras power point map with capital and flag templates slides outl...
Editable honduras power point map with capital and flag templates slides outl...SlideTeam.net
 
QUE SON LAS RUBRICASx
QUE SON LAS RUBRICASxQUE SON LAS RUBRICASx
QUE SON LAS RUBRICASxSergd
 
Apt 502 a1 m1 powerpoint,,,
Apt 502 a1 m1 powerpoint,,,Apt 502 a1 m1 powerpoint,,,
Apt 502 a1 m1 powerpoint,,,cloder6416
 

Destaque (18)

Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithm
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data Structure
Data StructureData Structure
Data Structure
 
Chapter 2.2 data structures
Chapter 2.2 data structuresChapter 2.2 data structures
Chapter 2.2 data structures
 
GOOGLE ALGORITHM - 2014 A LOOK SEO
GOOGLE ALGORITHM - 2014 A LOOK SEOGOOGLE ALGORITHM - 2014 A LOOK SEO
GOOGLE ALGORITHM - 2014 A LOOK SEO
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
AP Computer Science Test Prep - Part 3 - Data Structure & Algorithm
AP Computer Science Test Prep - Part 3 - Data Structure & AlgorithmAP Computer Science Test Prep - Part 3 - Data Structure & Algorithm
AP Computer Science Test Prep - Part 3 - Data Structure & Algorithm
 
Security Compensation - How to Invest in Start-Up Security
Security Compensation - How to Invest in Start-Up SecuritySecurity Compensation - How to Invest in Start-Up Security
Security Compensation - How to Invest in Start-Up Security
 
Meaningful Elearning with Digital Badges & Missions
Meaningful Elearning with Digital Badges & MissionsMeaningful Elearning with Digital Badges & Missions
Meaningful Elearning with Digital Badges & Missions
 
Ds 1
Ds 1Ds 1
Ds 1
 
Data structures
Data structuresData structures
Data structures
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Editable honduras power point map with capital and flag templates slides outl...
Editable honduras power point map with capital and flag templates slides outl...Editable honduras power point map with capital and flag templates slides outl...
Editable honduras power point map with capital and flag templates slides outl...
 
QUE SON LAS RUBRICASx
QUE SON LAS RUBRICASxQUE SON LAS RUBRICASx
QUE SON LAS RUBRICASx
 
MCB c60
MCB c60MCB c60
MCB c60
 
Apt 502 a1 m1 powerpoint,,,
Apt 502 a1 m1 powerpoint,,,Apt 502 a1 m1 powerpoint,,,
Apt 502 a1 m1 powerpoint,,,
 
Funny Money
Funny MoneyFunny Money
Funny Money
 

Semelhante a Data structure and algorithm All in One

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxsabithabanu83
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxcoc7987515756
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and typesankita946617
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxPJS KUMAR
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
COA Chapter 1.pdf
COA Chapter 1.pdfCOA Chapter 1.pdf
COA Chapter 1.pdfAbelAteme
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsRajendran
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptAlliVinay1
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.pptAshok280385
 

Semelhante a Data structure and algorithm All in One (20)

Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
II B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptxII B.Sc IT DATA STRUCTURES.pptx
II B.Sc IT DATA STRUCTURES.pptx
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
DSA
DSADSA
DSA
 
COA Chapter 1.pdf
COA Chapter 1.pdfCOA Chapter 1.pdf
COA Chapter 1.pdf
 
Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
ds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.pptds 1 Introduction to Data Structures.ppt
ds 1 Introduction to Data Structures.ppt
 
Data structure
Data structureData structure
Data structure
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Queues
QueuesQueues
Queues
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 

Mais de jehan1987

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Artifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in oneArtifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in onejehan1987
 
Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
Complete java swing
Complete java swingComplete java swing
Complete java swingjehan1987
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
Assessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industryAssessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industryjehan1987
 

Mais de jehan1987 (7)

Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Artifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in oneArtifitial intelligence (ai) all in one
Artifitial intelligence (ai) all in one
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Assessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industryAssessment of project management practices in pakistani software industry
Assessment of project management practices in pakistani software industry
 

Último

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Último (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

Data structure and algorithm All in One

  • 1. Data Structure and Algorithm Prepared by Basharat Jehan Email: Basharat_jehan@yahoo.com
  • 2. Data • Facts or information used usually to calculate, analyze, or plan something. OR • Data are simply collection of facts and figures. Data are values or set of values. OR The quantities, characters, or symbols on which operations are performed by a computer, being stored and transmitted in the form of electrical signals and recorded on magnetic, optical, or mechanical recording media.
  • 3. • In computing, data is information that has been translated into a form that is more convenient to move or process. • Data is collected and analyzed to create information suitable for making decisions
  • 4. • Data items that are divided into sub items are group items; • those that are not are called elementary items. For example, a student’s name may be divided into three sub items – [first name, middle name and last name] • but the ID of a student would normally be treated as a single item.
  • 5.
  • 6. Information • Knowledge that you get about someone or something. • knowledge obtained from investigation, study, or instruction (2) : intelligence, news (3) : facts, data . • Data are converted to information after Processing. • knowledge gained through study, communication, research, instruction, etc.; factual data:
  • 7. • Data that is (1) accurate and timely, (2) specific and organized for a purpose, (3) presented within a context that gives it meaning and relevance, and (4) can lead to an increase in understanding and decrease in uncertainty.
  • 9. Data Structure • In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently.
  • 10.
  • 11.
  • 13.
  • 14.
  • 15. Physical and Logical Data Structures • A physical data structure refers to the actual organization of data on a storage device. The logical data structure refers to how the information appears to a program or user. For example, a data file is a collection of information stored together. This is its logical structure. Physically, however, a file could be stored on a disk in several scattered pieces.
  • 17. INTRODUCTION • An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function
  • 18. COMPUTER ALGORITHMS • In computer systems, an algorithm is basically an instance of logic written in software by software developers to be effective for the intended "target" computer(s) for the target machines to produce output from given input (perhaps null)
  • 19. DEFINITION • An algorithm is a finite sequence of step by step, discrete, unambiguous instructions for solving a particular problem – has input data, and is expected to produce output data – each instruction can be carried out in a finite amount of time in a deterministic way
  • 20. Control structures • Programs written in procedural languages, the most common kind, are like recipes, having lists of ingredients and step-by-step instructions for using them. The three basic control structures in virtually every procedural language are: 1. Sequence—combine the liquid ingredients, and next add the dry ones. 2. Conditional—if the tomatoes are fresh then simmer them, but if canned, skip this step. 3. Iterative—beat the egg whites until they form soft peaks.
  • 21. SEQUENCE • it is the default control structure; instructions are executed one after another. They might, for example, carry out a series of arithmetic operations, assigning results to variables, to find the roots of a quadratic equationax2 + bx + c = 0. The conditional IF- THEN or IF-THEN-ELSE control structure allows a program to follow alternative paths of execution.
  • 22. ITERATIVE/REPETITIVE FLOW • Iteration (also called repetition) means a frame that regulates the repeat of an action depending on a condition. The action is called the body of the iteration loop. • This means that all low or high level algorithms can be formulated as a series of structural elements consisting only of the three types above. • Hence, for any algorithm description method it is enough to be able to interpret the three types above.
  • 23. The flow diagram of the minimum finding problem
  • 24. ITERATION, OR LOOPING AND CONDITIONAL FLOW • It gives computers much of their power. They can repeat a sequence of steps as often as necessary, and appropriate repetitions of quite simple steps can solve complex problems. • These control structures can be combined. A sequence may contain several loops; a loop may contain a loop nested within it, or the two branches of a conditional may each contain sequences with loops and more conditionals. The following programming fragment employs the IF-THEN structure for finding one root of the quadratic equation, using the quadratic formula:
  • 25. Array data structure • In computer science, an array data structure or simply an array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
  • 26. FINDING LENGTH OF ARRY • MAXIMUM LENGTH= UPPER BOUND(UB)-LOWER BOUND(LB)+1
  • 27. One dimensional (1-D) arrays or Linear arrays: • In it each element is represented by a single subscript. The elements are stored in consecutive memory locations. E.g. A [1], A [2], ….., A [N]. • Or • A(1), A(2)… • A1,A2,…………
  • 28. Multi dimensional arrays: Two dimensional (2-D) arrays or Matrix arrays • In it each element is represented by two subscripts. Thus a two dimensional m x n array A has m rows and n columns and contains m*n elements. It is also called matrix array because in it the elements form a matrix. E.g. A [2] [3] has 2 rows and 3 columns and 2*3 = 6 elements.
  • 29. Traversing • Traversing basically means the accessing the each and every element of the array at least once. Traversing is usually done to be aware of the data elements which are present in the array.
  • 30. ALGORITHM FOR TRAVERSING • SET K=LB • REPEAT STEP 3 AND 4 WHILE K<=UB • INPUT ARRAY[K] • SET K=K+1 • END LOOP • OUTPUT ARRAY[K] • EXIT
  • 31. A program to traverse or read a linear or 1-D array • #include<stdio.h> #include<conio.h> void main() { int a[7], i=0; clrscr(); printf("Enter the elements of the array :"); for (i=0;i<=6 ;i++ ) { scanf("%dn", &a[i]); /* taking input array from the user */ } printf("nThe elements of the array you have enetered are as follows :") for(i=0; i<=6; i++) { printf("%d", a[i]); /* printing the array elements or traversing the array */ } getch(); }
  • 32. Algorithm to find the sum of elements of array • Step#1 SUM=0 • Step#2 repeat for i=0 to UB • [print the calculated sum] • SUM=SUM+A[i] • End of Loop • Step#3 PRINT SUM • Step#4 EXIT
  • 33. Program • #include<iostream.h> • #include<conio.h> • using namespace std; • main() • { • int A[3], sum,i; • sum=0; • for(i=0; i<=2;i++) • { • cin>>A[i]; • sum=sum+A[i]; • • } • cout<<sum; • getch(); • }
  • 34. Accessing One Dimensional array by Dope Vector method • L(X[k])=Lo+C*(k-1) • Where Lo is base address, C is the length of each memory location, and k is index number. • L(X[3]) = 200+2*(3-1)=204 Data address 45 200 45 202 56 204 67 206 88 208
  • 35.
  • 37.
  • 38. Inserting • Inserting: Adding a new record to the end of array. • Insertion may be at end of array or at specific location.
  • 39. • Adding a new record to the end of array. • Algorithm • REPEAT step 2 to 3 FOR I=4 to 9 • INPUT value in N • Temp[I]= N [End of step- 2 Loop] Exit
  • 40. • #include<iostream> • #include<conio.h> • using namespace std; • class insrt • { • • private: • int i, A[10]; • public: • void input() • { • A[1]=10; • A[2]=20; • } • void abc() • { • for(i=3; i<=10; i++) • { • cin>>A[i]; • } • for(i=1; i<=10; i++) • { • cout<<A[i]; • } • }}; • main() • { • insrt a; • a.input(); • a.abc();
  • 41. Element insertion at specific location • Algorithm to insert element in array requires the specific location at which the element is to be added.
  • 42.
  • 43. • This algorithm performs Insert Operation on Array; it adds an ELEMENT at the given location LOC in the array – DATA. In the first step we initialized variable I with the maximum number of element present in the array (I:=N). After that with loop we shift the element forward till we reach the location LOC. In the next step, we insert the ELEMENT at its location.
  • 44. • #include<iostream> • #include<conio.h> • using namespace std; • class bcs • { • private: • int abc[5],i; • public: • void assign(int j) • { • for(int i=0; i<=j; i++) • cin>>abc[i]; • • void specific(int loc, int val) • { • for(int i=4; i>=loc; i--) • abc[i+1]=abc[i]; • abc[loc]=val; • }
  • 45. • void print(int n) • { • for(int i=0; i<=n; i++) • cout<<abc[i]; • } • }; • main() • { • bcs obj; • int n,pos; • obj.assign(3); • obj.print(3); • cout<<"enter value to insert"; • cin>>n>>endl; • cin>>pos; • if(pos>=4) • { • cout<<"invalid location"; • return 0; • } • obj.specific(pos,n); • obj.print(4); • }
  • 47.
  • 48.
  • 49. Delete Operation on Array • To delete element from array we should know the location from where we want to delete. Algorithm to Delete Element from Array can be executed at any location ie. at it beginning, in middle or at the end of the array. • Apply a loop from the location of item till the end of the array. Shift the element forward. In this algorithm, DATA is an array. ELEMENT is the value which we want to delete from the location LOC. N is the maximum number of element present in the array DATA.
  • 50. Algorithm to Delete Element from Array is:
  • 51.
  • 52.
  • 53. INTRODUCTION TO THE BASIC OPERATIONS OF DATA STRUCTURE • The data appearing in data structures are processed by means of operations. The following are operations are major operations
  • 54. • Traversing: Accessing each record exactly once so that certain items in the record may be processed. • Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions. • Inserting: Adding a new record to the structure. • Deleting: Removing a record from the structure. It is used to delete an existing data item from the given collection of data items •
  • 55. Following two are special operations: Following two are special operations: • Sorting: Arranging the records in some logical order. It is used to arrange the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data • Merging: Combining the records in two different sorted files into a single sorted file.
  • 56. Recursion Concept : • Recursion is basic concept in Programming. • When Function is defined in terms of itself then it is called as “Recursion Function“. • Recursive function is a function which contains a call to itself.
  • 57.
  • 58.
  • 59. 59
  • 60. Fibonacci Sequence • The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. Similarly, the 3 is found by adding the two numbers before it (1+2),
  • 61. Fibonacci series program in c using recursion • #include<stdio.h> • • int Fibonacci(int); • • main() • { • int n, i = 0, c; • • cin>>n; • • cout<<"Fibonacci series; • • for ( c = 1 ; c <= n ; c++ ) • { • cout<<Fibonacci(i); • i++; • } • • return 0; • } •
  • 62. • int Fibonacci(int n) • { • if ( n == 0 ) • return 0; • else if ( n == 1 ) • return 1; • else • return ( Fibonacci(n-1) + Fibonacci(n-2) ); • }
  • 63. Searching Algorithms • Necessary components to search a list of fdata – Array containing the list – Length of the list – Item for which you are searching • After search completed – If item found, report “success,” return location in array – If item not found, report “not found” or “failure”
  • 64. Sequential Search • In computer science, linear search or sequential search is a method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted. The list need not be ordered.
  • 65. • Suppose that you want to determine whether 27 is in the list • First compare 27 with list[0]; that is, compare 27 with 35 • Because list[0] ≠ 27, you then compare 27 with list[1] • Because list[1] ≠ 27, you compare 27 with the next element in the list • Because list[2] = 27, the search stops • This search is successful! Searching Algorithms (Cont’d) Figure 1: Array list with seven (07) elements
  • 66. • Let’s now search for 10 • The search starts at the first element in the list; that is, at list[0] • Proceeding as before, we see that this time the search item, which is 10, is compared with every item in the list • Eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search Searching Algorithms (Cont’d)
  • 67. Algorithm of Sequential Searching • The complete algorithm for sequential search is //list the elements to be searched //target the value being searched for //N the number of elements in the list SequentialSearch( LIST, ITEM, N, LOC) Set LOC=1 • for i = 1 to N do • INPUT LIST[i] • Repeat While(LOC<=N) • if (ITEM = LIST[LOC]) • return LOC [Successful Search] • LOC=LOC+1 • end if • end While • return 0 [Unsuccessful Search]
  • 68. C++ Code for Sequential Search • #include<iostream> • #include<conio.h> • using namespace std; • main() • { • int A[10],i, LOC, ITEM,n; • LOC=-1; • cout<<"Enter size of array but less than 10"; • cin>>n;
  • 69. • for (i=1; i<=n; i++) • { • cin>>A[i]; • } • cout<<"item to be searched"; • cin>>ITEM;
  • 70. • i=0; • while(i<=n) • { • if (ITEM==A[i]){ • LOC=i; • cout<<"location"<<LOC; • } • i++; • }
  • 71. • if(LOC==-1) • { • • cout<<"data not found"; • } • }
  • 72. • Can only be performed on a sorted list !!! • Uses divide and conquer technique to search list Binary Search Algorithm
  • 73. • Search item is compared with middle element of list • If search item < middle element of list, search is restricted to first half of the list • If search item > middle element of list, search second half of the list • If search item = middle element, search is complete Binary Search Algorithm (Cont’d)
  • 74. • Determine whether 75 is in the list Binary Search Algorithm (Cont’d) Figure 2: Array list with twelve (12) elements Figure 3: Search list, list[0] … list[11]
  • 75. Binary Search Algorithm (Cont’d) Figure 4: Search list, list[6] … list[11]
  • 76.
  • 77. C++ program for binary search • #include<iostream> • #include<conio.h> • using namespace std; • main() • { • int a[10], loc,item,n,i; • Loc=-1; • cout<<"enter size of array"<<endl; • cin>>n; • for(i=1; i<=n; i++) • { • cin>>a[i]; • • } • for(i=1; i<=n; i++) • { • cout<<"array element"<<a[i]<<endl; • } •
  • 78. • cout<<"enter item to be searched "<<endl; • cin>>item; • int middle=(1+n)/2; • cout<<middle; • if(item==a[middle]) • { • cout<<middle; • }
  • 79. • if(item>a[middle]) • { • i=middle+1; • while(i<=n) • { • if(item==a[i]) • { • Loc=i; • cout<<loc; • } • i++; • }} •
  • 80. • if(item<a[middle]) • { • i=middle-1; • while(i>0) • { • if(item==a[i]) • { • i=loc; • cout<<loc; • } • i--; • }} • if(loc==-1) • { • cout<<"item not found"; • } • }
  • 81. Sorting • Definition is discussed already in previous slides.. See data structure operations types
  • 82. Bubble Sort • The technique we use is called “Bubble Sort” because the bigger value gradually bubbles their way up to the top of array like air bubble rising in water, while the small values sink to the bottom of array.
  • 83.
  • 84. • Algorithm: (Bubble Sort) BUBBLE (DATA, n) • Here DATA is an Array with N elements. This algorithm sorts the • elements in DATA. • 1. for(pass=n; pass>=1; pass--) • 2. for(i=1; i<=pass; i++) • 3. If DATA[i]>DATA[i+1], then: • Interchange DATA[i] and DATA[i+1]. • [End of If Structure.] • [End of inner loop.] • [End of Step 1 outer loop.] • 4. Exit.
  • 85. C++ PROGRAMM FOR BUBBLE SORT • #include<iostream> • #include<conio.h> • using namespace std; • main() • { • int pass,n,i,temp; • int a[10]; • cout<<"PROGRAM FOR BUBBLE SORT"; • cout<<"Enter size of array which is less than 10"; • cin>>n; • for(i=1; i<=n; i++) • { • cin>>a[i]; • } •
  • 86. • for(pass=n; pass>=1; pass--) • • for(i=1; i<=pass; i++) • • if(a[i]>a[i+1]) • { • temp=a[i+1]; • a[i+1]=a[i]; • a[i]=temp; • } • • for(i=1; i<=n; i++) • { • cout<<a[i]; • } • • }
  • 87. Insertion Sort • Sorting method in which algorithm scan a whole list one by one and in each iteration the algorithm place each number to its correct position.
  • 89.
  • 91. See example animated example at • http://courses.cs.vt.edu/csonline/Algorithms/ Lessons/InsertionCardSort/insertioncardsort.s wf
  • 92. STACKS • It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). The last element to be added is the first to be removed (LIFO: Last In, First Out).
  • 93.
  • 94. • A stack is a list of elements in which an element may be inserted or deleted only at • one end, called TOP of the stack. The elements are removed in reverse order of that in which they were inserted into the stack.
  • 95. • Basic operations: • These are two basic operations associated with stack: • Push() is the term used to insert/add an element into a stack. • Pop() is the term used to delete/remove an element from a stack.
  • 96.
  • 97. Stack push and pop program • #include<iostream> • #include<conio.h> • using namespace std; • main() • { • int s[10], i,j,n; • int top=0;
  • 98. • do • { • cin>>j; • s[top+1]=j; • for(j=top+1; j>=1;j--) • cout<<s[j]<<endl; • top++; • cout<<"do you want to process more y/n"<<endl;
  • 99. • } • while(top!=11); • cout<<"stack full"; • // pop operation • s[top]=NULL; • for(j=top; j>=1;j--) • cout<<s[j]<<endl; • }
  • 100. Queue • A queue is a linear list of elements in which deletion can take place only at one end, called the front, and insertions can take place only at the other end, called • the rear. The term “front” and “rear” are used in describing a linear list only when it • is implemented as a queue. • Queue is also called first-in-first-out (FIFO) lists. Since the first element in a • queue will be the first element out of the queue. In other words, the order in which • elements enters a queue is the order in which they leave.
  • 101. • Primary queue operations: • Enqueue: insert an element at the rear of the queue. • Dequeue: remove an element from the front of the queue.
  • 102.
  • 103.
  • 105.
  • 106.
  • 107. 107 Deques • A deque is a double-ended queue • Insertions and deletions can occur at either end • Implementation is similar to that for queues • Deques are not heavily used • You should know what a deque is, but we won’t explore them much further