Introduction
Introduction
to
Data Structures
Definition
Data: Collection of raw facts.
Data structure is representation of the
logical relationship existing between
individual elements of data.
Data structure is a specialized format for
organizing and storing data in memory that
considers not only the elements stored but
also their relationship to each other.
• For Example: We have the data student’s Name
“Akash” and age “16”. Here “Akash” is a string type
data type and 16 are of integer. We can now organize
this data as a record like a Student Record. We can
collect and store other Student’s Record in a file or a
database as a data structure.
Introduction
}
Arrays
types
Single Dimension Array
◦ Array with one subscript
T wo Dimension Array
◦ Array with two subscripts
(Rows and Column)
If ELE not found then we get a condition BEG > END. This results
in unsuccessful search.
• ALGORITHM: Binary_Search (BEG, END, MID ELE) A is an array with N elements. Let BEG, END, MID
denote Beginning, end and middle location of the array
• Step 1: Set BEG=LB, END=UB LOC = -1
• Step 2: While(BEG <= END)
• MID = (BEG+END)/2
• if ( ELE = A [ MID ] ) then
• LOC = MID
• Goto Step 3
• else
• if( ELE < A[MID])
• END=MID-1;
• else
• BEG=MID+1;
• [End if]
• [End if]
• [End of While loop]
• Step 3: if ( LOC >= 0 ) then
• Print Element, “Found in Location”, LOC
• else
• Print Element, “Search is Unsuccessful”
• Example: Consider the following elements stored in an array and
we are searching for the element 47. The trace of the algorithm is
given below.
• Example: Consider the following elements stored in an array and we are
searching for the element 67. The trace of the algorithm is given below.
Difference between Linear Search and
Binary Search
Linear Search Binary Search
This can be used in sorted and This can be used only in sorted
unsorted array array
· peek( ): It returns the top item from the stack but does not remove it.