Unit 2 - Data Structure - WWW - Rgpvnotes.in
Unit 2 - Data Structure - WWW - Rgpvnotes.in
Unit 2 - Data Structure - WWW - Rgpvnotes.in
Tech
Subject Name: Data Structure
Subject Code: IT-303
Semester: 3rd
Downloaded from be.rgpvnotes.in
Subject Notes
IT 302- Data Structure
Unit-2
2.1 Arrays & Structure Introduction
The simplest type of data structure is a linear (or one dimensional) array.
Array is set of homogenous data items which are stored in contiguous memory locations under a common name
and with sequence of indices starting from 0.An array size is fixed and therefore requires a fixed number of
memory locations. It is a list of finite number n of similar data referenced respectively by a set of n consecutive
numbers, usually 1, 2, 3 . . . . . . . n. if we choose the name A for the array, then the elements of A are denoted by
subscript notation
A 1, A 2, A 3 . . . . A n
or by the parenthesis notation
A (1), A (2), A (3) . . . . . . A (n)
or by the bracket notation
A [1], A [2], A [3] . . . . . . A [n]
Structures
Structure is a collection of variables of different data types under a single name. It is similar to a class in that, both
holds a collecion of data of different data types. For example: You want to store some information about a
person: his/her name, citizenship number and salary. You can easily create different variables name, citNo, salary
to store these information separately.
The struct keyword defines a structure type followed by an identifier (name of the structure). Then inside the
curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For
example:
struct Person
{
char name[50];
int age;
float salary;
};
Here a structure person is defined which has three members: name, age and salary.
Once you declare a structure person as above. You can define a structure variable as: Person bill; Here, a
structure variable bill is defined which is of type structure Person.The members of structure variable is accessed
using a dot (.) operator. Suppose, you want to access age of structure variable bill and assign it 50 to it. You can
perform this task by using following code below:
bill.age = 50;
Suppose that A is a variable that refers to an array. Then the element at index k in A is referred to as A[k]. The first
element is A[0], the second is A[1], and so forth. Each element is of same size. Elements are stored contiguously,
with the first element stored at the smallest memory address (called the base address).
2.3 One-dimensional Array or linear array requires only one index to access an element. Ex: int A[10]
Example:
One Dimension Array
Given the base address of an array B[1300.....1900] as 1020 and size of each element is 2 bytes in the memory.
Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]
In Column-major form, all the elements of the first column are stored, then the elements of the second column
and so on up to the last column.
Ex: 8 6 5 4
2 1 9 7
3 6 4 2
Examples:
Q 1. An array X [-15..........10, 15...............40] requires one byte of storage. If beginning location is 1500 determine
the location of X [15][20].
Solution:
As you see here the number of rows and columns are not given in the question. So they are calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 - (- 15)] +1 = 26
Number or rows say N = (Uc – Lc) + 1 = [40 - 15)] +1 = 26
a) Traversing: means to visit all the elements of the array in an operation is called traversing.
b) Insertion: means to put values into an array
c) Deletion: to delete a value from an array.
d) Sorting: Re-arrangement of values in an array in a specific order (Ascending or Descending) is called sorting.
e) Searching: The process of finding the location of a particular element in an array is called searching.
f) Merging : - Merging means joining two lists or two arrays in one single array.
It means processing or visiting each element in the array exactly once; Let A is a a a sto ed i the o pute ’s
memory. If we want to display the contents of A , it has to be traversed i.e. by accessing and processing each
element of A exactly once.
Sorting an array is the ordering the array elements in ascending (increasing from min to max) or descending
(decreasing from max to min) order.
Bubble Sort:
The te h i ue e use is alled Bu le “o t e ause the igge alue g aduall u les thei a up to the top
of array like air bubble rising in water, while the small values sink to the bottom of array. This technique is to
make several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in
increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their
values are swapped in the array.
You can perform a search for an array element based on its value or its index.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is
the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Insert operation is to insert one or more data elements into an array. Based on the requirement, a
new element can be added at the beginning, end, or any given index of array.
Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the Kth position of LA .
1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
Deletion refers to removing an existing element from the array and re-organizing all elements of an
array.
Algorithm
Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is
the algorithm to delete an element available at the Kth position of LA.
1. Start
2. Set J = K
10x2 + 26x, here 10 and 26 are coefficients and 2, 1 are its exponential value.
2.9.1 Representation of a Polynomial: A polynomial is an expression that contains more than two
terms. A term is made up of coefficient and exponent. An example of polynomial is P(x) =
4x3+6x2+7x+9
1. A polynomial thus may be represented using arrays or linked lists. Array representation assumes
that the exponents of the given expression are arranged from 0 to the highest value (degree), which
is represented by the subscript of the array beginning with 0. The coefficients of the respective
exponent are placed at an appropriate index in the array. The array representation for the above
polynomial expression is given below:
struct polynomial
{
int coefficient;
int exponent;
struct polynomial *next;
};
2. A polynomial may also be represented using a linked list. A structure may be defined such that it
contains two parts- one is the coefficient and second is the corresponding exponent. The structure
definition may be given as shown below:
struct polynode {
float coeff;
int exp;
polynode* link;
} * p;
For adding two polynomials using arrays is straightforward method, since both the arrays may be
added up element wise beginning from 0 to n-1, resulting in addition of two polynomials. Addition of
two polynomials using linked list requires comparing the exponents, and wherever the exponents are
found to be same, the coefficients are added up. For terms with different exponents, the complete
term is simply added to the result thereby making it a part of addition result. The complete program
to add two polynomials is given in subsequent section.
2.11 Searching
Linear Search
Linear search or sequential search is a method for finding a particular value in a list that consists of
checking every one of its elements, one at a time and in sequence, until the desired one is found.
Linear search is the simplest search algorithm.
How Linear Search works
Linear search in an array is usually programmed by stepping up an index variable until it reaches the
last index. This normally requires two comparisons for each list item: one to check whether the index
has reached the end of the array, and another one to check whether the item has the desired value.
1.Repeat For J = 1 to N
5. If (J > N) Then
P i t: ITEM does ’t exist
[End of If]
6.Exit
Binary Search A binary search or half-interval search algorithm finds the position of a specified input
value (the search "key") within an array sorted by key value. For binary search, the array should be
arranged in ascending or descending order. In each step, the algorithm compares the search key
value with the key value of the middle element of the array. If the keys match, then a matching
element has been found and its index is returned. Otherwise, if the search key is less than the middle
element's key, then the algorithm repeats its action on the sub-array to the left of the middle
element or, if the search key is greater, on the sub-array to the right. If the remaining array to be
searched is empty, then the key cannot be found in the array and a special "not found" indication is
returned.
10.Else
[End of If]
13. Exit
2.12 Sorting
Sorting is nothing but storage of data in sorted order, it can be in ascending or descending order.
There are so many things in our real life that we need to search, like a particular record in database,
roll numbers in merit list, a particular telephone number, any particular page in a book etc.
TYPES OF SORTING
1. An internal sort is any data sorting process that takes place entirely within the main memory
of a computer. This is possible whenever the data to be sorted is small enough to all be held
in the main memory.
2. External sorting is a term for a class of sorting algorithms that can handle massive amounts of
data. External sorting is required when the data being sorted do not fit into the main memory
of a computing device (usually RAM) and instead they must reside in the slower external
memory (usually a hard drive). External sorting typically uses a hybrid sort-merge strategy. In
the sorting phase, chunks of data small enough to fit in main memory are read, sorted, and
written out to a temporary file. In the merge phase, the sorted sub files are combined into a
single larger file.
It is a simple sorting algorithm that builds the final sorted array (or list) one item at a time.
Simple implementation
1. Efficient for small data sets
2. Stable; i.e., does not change the relative order of elements with equal keys
3. In-place; i.e., only requires a constant amount O(1) of additional memory space.
A) Set A[j+1]=A[j]
1. j=j-1
5.Set A[j+1]=key
6.Return
2.12.2.Selection Sort
Selection sorting is conceptually the simplest sorting algorithm. This algorithm first finds the smallest
element in the array and exchanges it with the element in the first position, then find the second
smallest element and exchange it with the element in the second position, and continues in this way
until the entire array is sorted
2. Set MIN = J
3. Repeat For K = J+1 to N
4. If (A[K] < A[MIN]) Then
5. Set MIN = K
[End of If]
[End of Step 3 For Loop]
(i) Interchange A[J] and A[MIN] [End of Step 1 For Loop]
6.Exit
The number of comparison in the selection sort algorithm is independent of the original order of the element. That is
comparison during PASS 1 to find the smallest element, there are n-2 comparisons during PASS 2 to find the second sma
and so on. Accordingly
2.12.3.Bubble Sort
Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for eg: an Array with N n
elements. Bubble Sort compares all the element one by one and sort them based on their values. It is called Bubble sor
with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to
surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and
each pair that is out of order.
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble so
step, elements written in bold are being compared. Three passes will be required.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4 (
1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
(14258) (14258)
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2 (
12458) (12458)
(12458) (12458)
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pa
any swap to know it is sorted.
Third Pass:
(12458) (12458)(12458) (12458)(12458) (1245
8)(12458) (12458)
2.Set ptr=1
1. If (A[ptr] > A[ptr+1]) Then Interchange A[ptr] and A[ptr+1] [End of If]
2. ptr=ptr+1
4. Exit
2.12.4.Quick Sort
Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but it is
very fast and requires very less additional space. It is based on the rule of Divide and Conquer (also
called partition-exchange sort). This algorithm divides the list into three main parts
6 8 17 14 25 63 37 52
Hence after the first pass, pivot will be set at its position, with all the elements smaller to it on its left
and all the elements larger than it on the right. Now 6 8 17 14 and 63 37 52 are considered as two
separate lists, and same logic is applied on them, and we keep doing this until the complete list is
sorted.
Partition ( ):
1. Set LEFT = BEG, RIGHT = END and LOC = BEG
2. Beginning with the element pointed by RIGHT, scan the array from right to left, comparing each
element with the element pointed by LOC until:
3.(a) Element smaller than the element pointed by LOC is found.
(b) Interchange elements pointed by LOC and RIGHT.
(c) If RIGHT becomes equal to LOC, terminate the subfunction Partition ( ).
Beginning with the element pointed by LEFT, scan the array from left to right, comparing each
element with the element pointed by LOC until:
4.(a) Element greater than the element pointed by LOC is found.
(b) Interchange elements pointed by LOC and LEFT.
(c) If LEFT becomes equal to LOC, terminate the subfunction Partition ( ).
5.Exit
5. Merge Sort
Merge Sort follows the rule of Divide and Conquer. But it doesn't divide the list into two halves. In
merge sort the unsorted list is divided into N sub lists, each having one element, because a list of one
element is considered sorted. Then, it repeatedly merge these sub lists, to produce new sorted sub
lists, and at lasts one sorted list is produced. Merge Sort is quite fast, and has a time complexity of
O(n log n). It is also a stable sort, which means the equal elements are ordered in the same order in
the sorted list.
PASS 1. Merge each pair of elements to obtain the list of sorted pairs.
PASS 2. Merge each pair of pairs to obtain the list of sorted quadruplets.
PASS 3. Merge each pair of sorted quadruplets to obtain the two sorted subarrays.
PASS 4. Merge the two sorted subarrays to obtain the single sorted array.
6.Heap Sort
Heap Sort is one of the best sorting methods being in -place and with no quadratic worst-case
scenarios. Heap sort algorithm is divided into two basic parts
Creating a Heap of the unsorted list.
Then a sorted array is created by repeatedly removing the largest/smallest element from the heap,
and inserting it into the array. The heap is reconstructed after each removal.
What is a Heap?
Heap is a special tree-based data structure that satisfies the following special heap properties Shape
Property: Heap data structure is always a Complete Binary Tree, which means all levels of the tree
are fully filled.
Heap Property: All nodes are either greater than or equal to or less than or equal to each of its
children. If the parent nodes are greater than their children, heap is called a Max-Heap, and if the
parent nodes are smaller than their child nodes, heap is called Min-Heap.
The heap sort algorithm is applied to an array A with n elements. The algorithm has two phases, and
we analyze the complexity of each phae separately.
Phase 1. Suppose H is a heap. The number of comparisons to find the appropriate place of a new
element item in H cannot exceed the depth of H. Since H is complete tree, its depth is bounded by
log2m where m is the number of elements in H. Accordingly, the total number g(n) of comparisons to
insert the n elements of A into H is bounded as
g(n) ≤ n log2n
Phase 2. If H is a complete tree with m elements, the left and right subtrees of H are heaps and L is
the root of H Reheaping uses 4 comparisons to move the node L one step down the tree H. Since the
depth cannot exceeds log2m , it uses 4log2m comparisons to find the appropriate place of L in the
tree H.
h(n)≤4nlog2n
Thus each phase requires time proportional to nlog 2n, the running time to sort n elements array A
would be nlog2n
7. Radix Sort
The idea is to consider the key one character at a time and to divide the entries, not into two sub
lists, but into as many sub lists as there are possibilities for the given character from the key. If our
keys, for example, are words or other alphabetic strings, then we divide the list into 26 sub lists at
each stage. That is, we set up a table of 26 lists and distribute the entries into the lists according to
one of the characters in the key.
A person sorting words by this method might first distribute the words into 26 lists according to the
initial letter (or distribute punched cards into 12 piles), then divide each of these sub lists into further
sub lists according to the second letter, and so on. The following idea eliminates this multiplicity of
sub lists: Partition the items into the table of sub lists first by the least significant position, not the
most significant. After this first partition, the sub lists from the table are put back together as a single
list, in the order given by the character in the least significant position. The list is then partitioned into
the table according to the second least significant position and recombined as one list. When, after
repetition of these steps, the list has been partitioned by the most significant place and recombined,
it will be completely sorted. This process is illustrated by sorting the list of nine three-letter words
below.
The list A of n elements A1, A2,……………An is given. Let d denote the radix(e.g d=10 for decimal digits,
d=26 for letters and d=2 for bits) and each item Ai is represented by means of s of the digits:
Ai = di1 di2………………. dis
The radix sort require s passes, the number of digits in each item . Pass K will compare each d ik with
each of the d digits. Hence
C(n)≤ d*s*n