Data Structure ArraySearchSort
Data Structure ArraySearchSort
Data Structure
Data and Data Item: Raw facts are called as data & those raw facts which are the
part of our information is called as meaningful data.
Data Items are classified as -
1. Elementary Items.
2. Group Items.
Those data items that are not divided into sub items are called Elementary items.
And those data items that are divided into sub items are called Group items.
Collections of data are frequently organized into a hierarchy of fields, records and
files. Therefore representation of single unit of value on certain type is termed as
data items.
Data Structure: - Techniques and methods are used to represent the data in
computer memory along with some operations to perform on it is termed as Data
Structure.
Other operations
1. Sorting: - Sorting means arranging of an data elements of a data structure in
a specified order {Ascending and Descending}.
2. Merging: - Merging means combining the two similar data structure to make
new data structure of a same time.
1
Data Structure Sweta Bohra
After these illustrations we can further divide non-primitive data type in 2 categories
1. Simple data structure: - Simple data structure is normally built from
primitive data type by int, char, float.
Following data structure can be formed as simple data structure.
1. Array.
2. Structure.
2. Compound data structure: - Simple data structure can be combined in
various ways to form a complex structure are called compound data structure.
2. Non-linear: - Those data structure which are multi level like tree, graph and
forest are termed as non-linear data structure.
Data Structure
Primitive Non-Primitive
(int, float, char)
Simple Compound
[Array, Structure]
Linear Non-Linear
[Stack, Queue, Linked List] [Tree, Graph,
Forest]
2
Data Structure Sweta Bohra
Algorithms, are used to manipulate the data contained in these data structures as
in searching and sorting. The formal representation of this model as a sequence of
instructions is called an algorithm, and coded algorithm, in a specific computer
language is called a program.
3
Data Structure Sweta Bohra
Characteristics of Algorithm
It should contain finite number of steps and for execution contain finite time.
It should contain unambiguous symbol.
4
Data Structure Sweta Bohra
Analy
sis
Priory Posteri
Calculate on the base of or on a particular
Dependent
Iteration Hardware
• Gives Approx. Value • Gives Exact Value
• (Independent) • (Dependency)
• Uniformity • Value is not uniform
Asymptotic Notation
It’s a mathematical way to represent time complexity.
1. Big - Oh (O)
c=
constant
value
n = input
value
t = time
5
Data Structure Sweta Bohra
6
Data Structure Sweta Bohra
2. Big - Omega
c=
constant
value
n = input
value
t = time
t Graph Convention
f(n)f(n) = g(n)
f(n) >= c.g(n)
where c > 0
c.g(n) n > k
k >=0
k
• Best Case
• Lower Bound (At
least)
3. Theta
7
Data Structure Sweta Bohra
c=
constant
value
n = input
value
t = time
t Graph Convention
f(n) C1.g(n) <=f(n) <= c2.g(n)
c.g(
n)
k
• Best
Case
• Lower
Bound
(At least)
8
Data Structure Sweta Bohra
Array: - We used variables to store only a single piece of data. But sometimes, you
want to refer to a whole bunch of data all at once. For that, you need a new type of
variable: the array. An array is a “collection variable” or data structure. It is a linear
structure where all the elements [Data Items] form a sequence.
When elements are Linear Structure & are represented by means of contiguous
[sequence] memory location then this Linear Structure are called Array.
Array is one of the simplest Data Structure and is very easy to sort, search & traverse.
Array stores a list of finite numbers of homogeneous statements, i.e., Data Elements
of same type.
Types of Array
1. One dimensional array [1-d]
2. Multidimensional array [n-d]
1 2 3 4 5 6 7 8
1 n
Here, n is length of array (total number of data can be stored in array) and it can be
found by index (Lower bound and Upper bound).
Lower bound (LB); smallest index i.e. 1
Upper bound (UB); largest index i.e. 8
9
Data Structure Sweta Bohra
2 4 64 4 33
Here ‘num’ is the name of the array, size is 5 and it is of int type. The array subscript
always starts from zero.
Subscript notation
num[6] or
Bracket notation
Subscript
or
Index
10
Data Structure Sweta Bohra
In computing, row-major order and column-major order describe methods for storing
multidimensional arrays in linear memory.
It can be store by programming language in two different ways
Column by Column (Column-major order)
Row by Row(Row major order)
(1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3) (1,4) (2,4) (3,4)
(1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Computer does not keep track of address of each element of array; rather it will store
address of first element i.e. base address,
Base(LA)
Linear Array
LOC (LA[K])
Location
Element of Array
11
Element of Array
Data Structure Sweta Bohra
Address 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Index 7 8 9 10
It means address (memory location) of 9th index element is 10. Using this formula
we can access any of the data element of array, no need to traverse each element
one by one
12
Data Structure Sweta Bohra
13
Data Structure Sweta Bohra
Let ‘LA’ be a collection of data elements stored in the memory of the computer.
Suppose we want to count the number of elements of LA with a specific
property{+1}, these can be accomplished by accessing and processing each element
of LA exactly one.
TRAVERSE(LA,LB,UB,N)
Step 1. Set K = LB
Step 2. Repeat Step 3 to Step 4 while K < LB+N
Step 3. PROCESS LA[K]
Step 4. K=K+1
End while
Step 5. End
14
Data Structure Sweta Bohra
Insertion an element at the end of a linear array can be easily done provided by
memory space located for the array is large enough to accommodate the additional
elements.
On the other hand, suppose we need to insert an element in the middle of the array
then an average half of the element must be moved downwards to new location to
accommodate the new element and keep the order of the element.
1. Beginning/First
15
Data Structure Sweta Bohra
Step 9. N=N+1
End if
Step 10. End
16
Data Structure Sweta Bohra
2. End/Last
INSERTLAST(LA,LB,UB,N,NUM,ITEM)
Step 1. Set K = UB
Step 3. IF(LB+N = UB)
Step 4. PRINT “ARRAY IS FULL, CAN’T INSERT VALUE”
Else
Step 5. LA[LB+N-1] = ITEM
Step 6. N=N+1
End if
Step 6. End
17
Data Structure Sweta Bohra
3. After
18
Data Structure Sweta Bohra
19
Data Structure Sweta Bohra
4. Before
INSERTBEFORE(LA,LB,UB,N,VAL,ITEM)
Step 1. Set K = LB
Set J = LB+N-1
Step 2. Repeat Step 3 to Step 4 while K < N+K
Step 4. if (LA[K] = VAL)
BREAK
End if
Step 5. K=K+1
Step 6. End while
Step 7. Repeat Step 3 and Step 4 while K >= J
Step 8. LA[J+1] = LA[J]
Step 9. J=J-1
End while
Step 10. LA[K] = ITEM
Step 11. End
20
Data Structure Sweta Bohra
DELETE(LA,LB,UB,N,NUM,ITEM)
Step 1. Set K = LB
Set J = N
Step 2. Repeat Step 3 to Step 4 while K < LB+N
Step 4. if (LA[K] = NUM)
BREAK
End if
Step 5. K=K+1
Step 6. End while
Step 7. Repeat Step 3 and Step 4 while K<=LB+N-1
Step 8. LA[K] = LA[K+1]
Step 9. K=K+1
End while
Step 10. N=N-1
Step 11. End
21
Data Structure Sweta Bohra
22
Data Structure Sweta Bohra
SEARCH(LA,LB,UB,N,NUM)
Step 1. Set K = LB
Step 2. Repeat Step 3 to Step 4 while K < LB+N
Step 3. if (LA[K] = NUM)
PRINT “Element found at”,K or
End K<=LB+N-1
End if
Step 4. K=K+1
Step 5. End while
Step 6. PRINT “Element not found!”
Step 7. End
23
Data Structure Sweta Bohra
1. Best Case: O(1) where the element is found at the first index.
Element to search: 2
2 3 1 9 4 7 8 2 10 21 5 56 78 15 56 28
2. Worst Case: O(n) where the element is found at the last index or element is
not present in the array.
Element to search: 28
2 3 1 9 4 7 8 2 10 21 5 56 78 15 56 28
Element to search: 50
2 3 1 9 4 7 8 2 10 21 5 56 78 15 56 28
Element to search: 10
2 3 1 9 4 7 8 2 10 21 5 56 78 15 56 28
24
Data Structure Sweta Bohra
Binary Search
If the keys match, then a matching element has been found and its index, or position,
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.
For example, if we search record of student in stack of forms of all students arranged
on roll number or any key or searching name in directory or any word in dictionary.
BINARYSRCH(LA,LB,UB,N,NUM,ITEM)
Step 1. Set BEG = LB
Step 2. Set END = LB+N-1
Step 3. MID = int(BEG+END)/2
Step 4. Repeat Step 4 to Step while BEG < END AND LA[MID]!= ITEM
Step 5. if (LA[MID] < ITEM)
BEG = MID+1
End if
if (LA[MID] > ITEM)
END = MID-1
End if
MID = int(BEG+END)/2
Step 6. End while
25
Data Structure Sweta Bohra
1. Best Case : O(1) where the element is found at the first iteration.
Element to search: 9
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
MID = int
((BEG+END)/2)
26
Data Structure Sweta Bohra
2. Worst Case : Log2N where the element is found at the last index or element
is not present in the array.
Sorting Categories
There are two different categories in sorting:
Internal sorting: If the input data is such that it can be adjusted in the main
memory at once, it is called internal sorting.
External sorting: When the data is in big volume the data is divided into block and
stored in secondary memory. From secondary memory data is transferred blocks by
blocks to main memory & sorting procedure is used to sort on each block. Then the
whole data is merged to get the completed sorted file. This type of sorting is called
external sorting.
27
Data Structure Sweta Bohra
If the input data is such that it cannot be adjusted in the memory entirely at
once, it needs to be stored in a hard disk, floppy disk, or any other storage
device. This is called external sorting.
Bubble Sorting
Bubble Sort: It’s one of the simplest algorithms which repeatedly iterates through
the list, compares the elements next to each other, and swaps according to the
condition passed to them.
Suppose, an array, arr [5] = {14,33,27,35,10} and we have to sort it in increasing
order then Steps followed would be as:
Here two loops would be required in sorting, the first loop for iterating and the second
loop for comparing.
As first we check if the previous element is greater than the next element then swap
it otherwise increase the counter.
28
Data Structure Sweta Bohra
29
Data Structure Sweta Bohra
BUBBLE(LA, N,TEMP)
Step 1. i=0
Step 2. Repeat Step 3 to Step 4 while i<N-1
Step 3. Repeat Step 4 for j=0 to [(N-1)-i]
Step 4. if(LA[j]>LA[j+1]
TEMP=LA[j]
LA[j]=LA[j+1]
LA[j+1]=TEMP
End if
Step 4. End for
Step 5. i=i+1
Step 6. End while
Step 7. End
1. Best Case : O(n) It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of bubble sort
is O(n).
2. Worst Case : O(n2) - It occurs when the array elements are required
to be sorted in reverse order. That means suppose you have to sort
the array elements in ascending order, but its elements are in
descending order. The worst-case time complexity of bubble sort is
O(n2).
3. Average Case : O(n2) It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The
average case time complexity of bubble sort is O(n2).
30
Data Structure Sweta Bohra
Insertion Sorting
Insertion Sort: Insertion sort is the sorting mechanism where the sorted array is built
having one item at a time. The array elements are compared with each other
sequentially and then arranged simultaneously in some particular order. The analogy
can be understood from the style we arrange a deck of cards. This sort works on the
principle of inserting an element at a particular position, hence the name Insertion
Sort.
INSERTION-SORT(LA , n)
Step 1: for i = 1 to n
Step 2: key =LA [i]
Step 3: j=i – 1
Step 4: while j > 0 and LA[j] > key
Step 5: LA[j+1] = LA[j]
Step 6: j=j–1
Step 7: End while
Step 8: LA[j+1] = key
Step 9: End for
31
Data Structure Sweta Bohra
32
Data Structure Sweta Bohra
1. Best Case : O(n) It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of Insertion sort
is O(n).
2. Worst Case : O(n2) It occurs when the array elements are required to
be sorted in reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending order.
The worst-case time complexity of Insertion sort is O(n2).
3. Average Case : O(n2) It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The
average case time complexity of Insertion sort is O(n2).
33
Data Structure Sweta Bohra
Selection Sorting
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted part
at the left end and the unsorted part at the right end. Initially, the sorted part is
empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the
leftmost element, and that element becomes a part of the sorted array. This process
continues moving unsorted array boundary by one element to the right.
SELECTION-SORT(LA , n)
Step 1: for i = 1 to n-1
Step 2: min=i *set current element as minimum
Step 3: for j=i+1 to n
Step 4: If (LA [j] < LA [min]) then *check the element minimum
Step 5: min=j
Step 6: End if
Step 7: End for
Step 8: If indexmin != i *swap min element with current element
Step 9: Swap LA[min] and LA[i]
Step 10: End if
Step 11: End for
1. Best Case : O(n2) It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of Selection sort
is O(n2).
2. Worst Case : O(n2) It occurs when the array elements are required to
be sorted in reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending order.
The worst-case time complexity of Selection sort is O(n2).
3. Average Case : O(n2) It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The
average case time complexity of Selection sort is O(n2).
34
Data Structure Sweta Bohra
POINTER ARRAY
{5, 8, 6, 9, 1, 5, 3} Integer Array: each elements are integer.
{0.2, 2.3, 5.6, 9.2, 8.2} Real Array: each elements are real.
Pointer Array: In array if each elements are pointer then it’s called pointer array.
Pointer is the variable which contains the address of the elements.
35
Data Structure Sweta Bohra
1. Student(15)
It indicates 15 records in a file; is a collection of records.
Any particular field in file can be accessed by
Student.Age[5]
36
Data Structure Sweta Bohra
Arrays Records
37