Professional Documents
Culture Documents
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.
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
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.
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.
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: -
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.