You are on page 1of 6

Data Structure

Definition: -
Data may be organized in many ways the logical or mathematical
model of particular organization of data is called a data structure.
The choice of a particular data model depends on two
considerations.
It must be rich enough in structure to mirror the actual
relationships of the data is the real world.
The structure should be simple enough that one can effectively
process the data when necessary.

Primitive Data Structure: -


Data at their most primitive level within a computer that is the data
structure that typically are directly operated upon by machine level
instruction.
Integer
Real
Char
Pointer
Logical

Non Primitive Data Structure: -


A data structure which is used the primitive data structure to build
the further one and based on the logic it is called Non-Primitive data
structure. Means to say it does not interact directly with machine level
transaction.

Linear Data Structure: -


A data structures are classified as either linear or non-linear. A data
structure is said to be linear if its elements from a sequence, or in
other words, a linear list. There are two basic way to representing such
linear structure in memory. One way is to have the linear relationship
between the elements represented by means of sequential memory
location. These linear structures are called array and the other way is

Current Page 1 Total Pages 6


Data Structure
to have the linear relationship between the elements represented by
means of pointer or links. These linear structures are called linked list.

Linear Array: -
A linear array is a list of a finite no of homogenous data elements
such that
The elements of array are referenced respectively by & index set
consistency of a consecutive nos.
The elements of the array are stored respectively successive
memory location.
The number n of elements is called the length or size of array. In
general, the length or the number of data elements of the array can be
obtain from the index set by the formula LENGTH = UB
LB + 1
Where UB is the largest index, called upper bound, and LB is the
smallest index, called lower bound, of the array. Note that length = UB
when LB = 1. The elements of the array may be noted by the subscript
notation or by the parenthesis.
Logical representation of Array
1 20 5 65 8
0 5 0 1 2 3 4

0 10
1 20
2 5
3 65
4 85
Representation of linear array in memory
Let LA be a linear array in the memory of the computer. Recall that
the memory of the computer is simply a sequence of addressed
locations. As previous noted the elements of LA are stored in
successive memory cells. Accordingly, the computer does not need to
keep track of the address of every element of LA, but needs to keep
track only of the address of the first elements of LA, denoted by
Base(LA) and called the base address of LA. Using this address base
(LA), the computer calculates the address of any element of LA by the
following formula.
LOC (LA [K]) = BASE (LA) +W (K LOWER BOUND)
Where w is the number of words per memory cell for the array LA.
Observe that the time to calculate LOC (LA [K]) is essentially the same
for any value of K. Furthermore, given any subscript K, one can locate
and access the content of LA [K] without scanning any other element
of LA.
A collection A of data element is said to be indexed if any element
of A, which we shall call AK, can be located and processed in a time

Current Page 2 Total Pages 6


Data Structure
that is independent of K. The above discussion indicates that linear
arrays can be indexed. This is very important property of linear arrays.

Traversing Linear Arrays: -


Let A be a collection of data element stored in the memory of the
computer. Suppose we want to print the contents of the A or suppose
we want to count the number of elements of A with a given property.
This can be accomplished by traversing A, that is by accessing and
processing each element of A exactly once.

Inserting and Deleting: -


Let A be a collection of data elements in the memory of the
computer. Inserting refers to the operation of adding another
element to the collection A, and deleting refers to the operation of
removing one of the element from the A.
Inserting element at the end of the linear array is easy if it has an
enough memory to accommodate it but insert in to the middle is a
little difficult because you have to create the virtual space for it or in
the other word you have to move the elements down word one.
Similarly in the deleting element at the end of the linear array is
easy but to delete element from the middle is a little difficult because
you have to forgot the reference of the particular place and put or
upward the element one.

Sorting: -
Sorting is fundamental operation of computer science. Sorting refers
to the operation of arranging the data in some given order, such as
increasing or decreasing, with numerical data, or alphabetically, with
character data.
Sorting may seem to be a trivial task. Actually, sorting efficiently
may be quite complicated. There are so many kind of sorting
techniques are present.

Bubble Sort: -
In a bubble sort element compare with another element and change
immediately according to the requirement and thus the large element
is bubble up to the nth position or sinks to the nth position. At the first
time comparison made one time less then actual elements are and this
way every after one session this decrease by one time and goes the
one after another largest element sinks downwards or upward
according to our requirement.
Step 1: Compare A [1] and A [2] and arrange them in the desired
order, then compare A [2] and A [3] and arrange them, then
compare A [3] and A[4] and arrange them. It will continue
until we compare A [n-1] with A [n] and arrange them so on.

Current Page 3 Total Pages 6


Data Structure
Step 2: Repeat the step 1 with one less comparison; that is, now we
stop after we compare and possibly rearrange A [n-2] and A
[n-1].
Step n-1: Compare A [1] with A [2] and arrange them.
The process of sequential traversing through all or part of a list is
frequently called Pass each of the above steps is called pass.
Accordingly, the bubble sort algorithm requires n-1 passes where n is
the number of inputs item.

Complexity of Algorithm: -
Traditionally, the time for a sorting algorithm in measured in terms
of the number of comparisons. The number f(n) of comparisons in
bubble sort is easily computed. Specifically there are n-1 comparisons
during the first pass which places the largest elements in the last
position; there are n-2 comparisons in the second step which places
second largest element in the next large position and so on.
f (n) = n (n-1) = n2 +o(n) = O(n2)
2 2
In other words the time require the time required to execute the
bubble sort algorithm is proportional to n 2, where n is the number of
input items.
We can reduce the time of sorting by setting up the flag variable.

Insertion Sort: -
Suppose an array A with n elements is in memory. The insertion sort
algorithm scans A from A[1] to A[n], inserting each element A[K] into
its proper position in the previous sorted sub-array.
In the insertion sort first element is considered as a sorted then
after take the second element and put it before or after the first then
take the third element and scan the element with first and second and
put it before the first and second or put it between the first and second
or put it after the second and thus so on.
Step 1: A[1] by itself sorted
Step 2: A[2] is inserted either before or after A[1] so that A[1], A[2] is
sorted.
Step 3: A[3] is inserted into its proper place in A[1], A[2], that is,
before A[1], between A[1] and A[2], or after A[2] so that A[1],
A[2] and A[3] are sorted.
Step N: A[N] is inserted into its proper place in A[1], A[2],.,A[N-1]
so that A[1], A[2],.,A[N] is sorted.
This sort algorithm is frequently used when n is small. There
remains only the problem of deciding how to insert A[K] in its proper
place in the sorted sub-array. This can be accomplished by comparing
A[K] with A[K-1], comparing A[K] with A[K-2], comparing A[K] with A[K-
3] and so on, until first meeting an element A[J], then each elements
A[K-1], A[K-2]A[J+1] is moved forward one location, and A[K] is
then inserted in the J+1 position in array.

Current Page 4 Total Pages 6


Data Structure
The algorithm must specify if there always is an element A[J]
otherwise we must constantly check to see if we are comparing A[K]
with A[1]. This condition can be accomplished by introducing a sentinel
element A[0] = - (or very small number).

Complexity of the algorithm: -


The number f(n) of comparisons in the insertion sort algorithm can
easily compute. First of all worst case occurs when the array A is in
reverse order and the inner loop must use the maximum number K-1 of
comparisons.
f(n) = n(n-1) = O(n2)
2
Insertion sort is usually used only when n in small and in such a
case.

Selection Sort: -
Suppose an array A with n elements is in memory. The selection
sort algorithm for sorting A works as follows, first find the smallest
elements in the list and put it in the first position. Then find the second
position then find the second smallest element in the list and put it in
the second position and so on.
Step 1: Find the loc of the smallest in the list of N elements A[1], A[2]
A[N], and then interchange A[LOC] and A[1]. Then A[1] is
sorted.
Step 2: Find the location LOC of the smallest in the sub-list of N-1
elements A[2], A[3],A[N] and then interchange A[LOC] and
A[2] then A[1] and A[2] sorted.
Step N-1: Find the location LOC of the smaller of the elements
A[N-1], A[N] and then interchange A[LOC] and A[N-1] then
A[1], A[2],A[N] is sorted
Thus A is sorted after N-1 passes.

Complexity of Algorithm: -
First note that the number f(n) of comparison in the selection sort
algorithm is dependent of the original order of the elements observe
that MIN(A, K, N, LOC) requires N-K comparisons during pass 1 to find
the smallest element, there are N-2 comparisons requires to find the
second smallest element and so on.
f(n) = n(n-1)
2
The number of interchanges and assignments does depend on the
original order of the elements in the array A, but the sum of these
operations does not exceed a factor of n2.

Searching: -

Current Page 5 Total Pages 6


Data Structure
Let Data be a collection of data elements in memory, and suppose
specific item information is given searching refers to the operation of
finding the location of item in data or printing some message.
Suppose data is a linear array with n elements. Given no other
information about data the most intuitive way to search for given item
in data is to compare item with each element of data one by one. That
is first we test weather data[1] = item, and then we test weather
data[2] = item and, so on. This method which traverses data
sequentially to locate item, is called linear search or sequential search.

Complexity of Algorithm: -
The complexity of searching algorithms is measured in terms of the
number f(n) of comparisons required to find item in data where data
contains n elements.
f(n) = n+1
There is in the worst case.

Current Page 6 Total Pages 6

You might also like