Anda di halaman 1dari 9

DATA STRUCTURES NOTES

Algorithm
An algorithm is a finite set of instructions that must be followed to solve a problem
OR
An algorithm is a logical representation of the instructions which should be executed to perform a
meaningful task.
Characteristics of an algorithm
1) input There are zero or more quantities supplied as input
2) output The result is available (atleast one quantity) is produced
3) Definiteness Each instruction should be unique, concise and clear.
4) Finiteness If the algorithm is traced out , then for all cases, the algorithm should terminate
after a finite number of steps
5) Effectiveness- Each instruction should be very basic and feasible.
Efficiency of an algorithm
After the designing of algorithm, the efficiency of algorithm must be analysed. Efficiency of algorithm
determines whether the algorithm is economical in use of computer resources i:e CPU time (running
time) and memory (memory space).
Complexity of Algorithm / Performance Analysis
The criteria on which we the complexity of algorithm is found out is
1) Does it do what it want to do it?
2) Does it work correctly accourding to the original specifications of the task?
3) Is there documentation that describes how to use it and how it works?
4) Are procedures created in such a way that they perform logical sub functions?
5) Is the code readable?
The other criteria on which algorithms are judged for the performance are computing time ( Time
complexity) and storage requirements (space complexity)
-

Time Complexity
The time complexity of an algorithm/program is the amount of computer time that it needs to
run to completion.
-

Space Complexity
The space complexity of an algorithm is the amount of memory need to run to completion.

Storage Structure for Arrays


The simplest data structure that makes use of computed address to locate its elements is the one
dimensional array. A number of contiguous memory locations are sequentially allocated to an array.
The size of the array is fixed and therefore requires a fixed number of memory locations.
Single dimensional Array
For any array with base address of L0 and c representing the number of words allocated to each
element, the address of Ai is given by the following equation
Loc(Ai) = L0 + c * ( i 1 )
For ex. A is an array of 10 elements with base address of first element with 2050. To find the address of
the 5 element. The size of each element in the array is 1 byte.
Loc(A5) = 2050 + 1 * ( 5-1 ) = 2044

5th element

2050
2051
2052
2053
2054
2055
2056
2057
2058
2059

Double Dimensional Arrays


A two dimensional array is a collections of elements placed in m rows and n columns. It is also called a
matrix. There are two subscripts in the syntax of two dimensional array in which one specifies the
number of rows and the other specifies the number of columns.

Row major and column Major order of Storing a 2 dimensional Array


All elements of a matrix gets stored in a linear fasion ( 1 dimensional ) in the memory.
The two ways in which the the elements can be stored in computer memory are
1) Row major order
2) Column major order

Row major order - In row major order the elements of the first row occupies the first set of address ,
second row occupies the next set and so on.
For ex

The formula to calculate the address of any element in a matrix of size m x n ( m rows and n columns )
with each element of c bytes stored in row major order is
Loc ( A(i,j)) =L0 + [ ( i -1 ) * n + (j -1 ) ] * c
Ex : Location of A ( 3,2 ) in a matrix of 5 X 4 with base address of 5020 is
Location= 5020 + [(3-1) * 4 + (2-1 ) ] * 2 = 5038
1 2
3
4
5 6
7
8
The above array in linear fasion in row major order will be
9 10
11 12
13 14
15 16
1 18
2 3 194 20
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
17
First element at 5020
A(3,2) at location 5038

column major order - In column major order the elements of the first column occupies the first set of
address , second column occupies the next set and so on.

The formula to calculate the address of any element in a matrix of size m x n ( m rows and n columns )
with each element of c bytes stored in column major order is
Loc ( A(i,j)) =L0 + [ ( j -1 ) * m + (i -1 ) ] * c
Ex : Location of A ( 3,2 ) in a matrix of 5 X 4 with base address of 5020 is
Location= 5020 + [(2-1) * 5 + (3-1 ) ] * 2 = 5034
1
5
9
13
17

2
6
10
14
18

3
7
11
15
19

4
8
12
16
20

The above array in linear fasion in column major order will be


1

13 17 2

First element at 5020

10 14 18 3

11 15 19 4

A(3,2) at location 5034

12 16 20

SORTING & SEARCHING


Sorting is the operation of arranging the records of a table into some sequential order according to an
ordering criterion. The sort is performed according to the key value of each record.

1)Bubble Sorting
It is a sorting technique in which two records are interchanged immediately when found that they
are not in proper order.

It requires at most n-1 (n-1 or less than n-1) passes to sort the elements.
During the first pass, a[1] and a[2] are compared, and if they are out of order, then a[1] and a[2]
are interchanged; this process is repeated for a[2] and a[3], a[3] and a[4] and so on.
This method will cause small values to move up or bubble up.
After the first pass, the record with the largest value will be in the nth position.
On each successive pass, the records with the next largest key will be placed in position n-1,n-2
and so on.. respectively and finally resulting in sorted array.
After each pass through the table, a check can be made to determine whether any swapping is
done during that pass or not. If no swapping occurred, then the table is sorted and no further
passes are required.

Complexity of Bubble sort


Best case - O(n)
Average case - O(n2 )
Worst case - O(n2 )
2) Selection Sort

It is one of the easiest ways to sort a table.


A search is performed to locate the element which has the smallest value beginning from the
first element.
When the smallest element is found, it is interchanged with the first element in the table.
A search for the second smallest element is then carried out. This is done by examining the
values of the elements starting from the second position.
When the second smallest element is found, it is interchanged with the second position value.
The process of searching for the record with the next smallest key and placing in its proper
postion continues until all the records have been sorted in ascending order.
Complexity of Selection sort
Best case - O(n)

Average case - O(n2 )


Worst case - O(n2 )
3) Quick Sort

It is the best and the fastest sorting technique sorting technique


At each step in the method, the goal is to place a particular record in its final position within the
table.
After placing a record in its final position, all the records which are above this record will have
smaller values whereas all the records that follow it will have larger values.
This technique partitions the table into two subtables
The same process is then applied for each of these subtables and repeated until all records are
placed in its final position.
Complexity of Selection sort
Best case - O(n log n)
Average case - O(n log n )
Worst case - O(n log n)
4) Insertion Sort

Insertion sort is the worst sort when the data is already in sorted order.
Complexity of Insertion sort
Complexity of insertion sort is O( N2 )
6) Shell Sort

Anda mungkin juga menyukai