Vous êtes sur la page 1sur 53

Data Structures

and Algorithms
Arrays

Objectives

By the end of this lecture you will be


able to:
Define the types of Data Structures
Define Array
Define Array Operations and Algorithms

Data structures can be classified into two


categories:
Linear
Non-Linear

Types of
Data
Structures

Linear Data Structures:

are the structures which are arranged in a


sequence
The elements of such structures can be accessed
linearly

Non-Linear Data Structures:

Are the structures which do not have a linear


sequence

Type

Contd

Linear

Data Structure
Arrays
Stacks
Queues
Simple Queue
Double Ended Queue
Linked Lists
Singly Linked List
Doubly Linked List
Header Linked List
Circular Linked List
Stack and Queues as Linked Lists

Types

Data Structure

Nonlinear

Contd

Trees
Simple Trees
Binary Trees
Other Trees

Data Types
So far, we have seen only simple data types,
such as int, float, and char.

Array

Simple variables can hold only one value at any


time during program execution, although that value
may change.
A data structure is a data type that can hold
multiple values at the same time. (Synonyms:
complex data type, composite data type)
The array is one kind of data structure.

Linear Array:
An array is a group of related data items that all have the same
name and the same data type.
is a list of a finite number n of homogeneous data elements
Is one of the Linear Data Structure

Arrays

Arrays are static in that they remain the same size throughout
program execution.
An arrays data items are stored contiguously in memory.
Each of the data items is known as an element of the array. Each
element can be accessed individually.
The number of elements in an array is called the length or size of the
array
The lowest index is called the Lower Bound, LB
The largest index is called the Upper Bound, UB

The length can be found by the formula:


Length = UB LB + 1

Contd

Array elements can be denoted by subscript


notation as:
A1, A2, A3, A4, An
Or by using the C Language style
A[0], A[1], A[2], A[n]

int numbers[ 5 ] ;

Array
Declaration
and
Initialization

The name of this array is numbers.


This declaration sets aside a chunk of memory
that is big enough to hold 5 integers.
It does not initialize those memory locations to
0 or any other value. They contain garbage.
Initializing an array may be done with an array
initialization, as in :

int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;

myarray =

Accessing
Array
Elements

Each element in an array has a subscript


(index) associated with it.
Subscripts are integers and always begin at
zero.
Values of individual elements can be accessed
by indexing into the array. For example,
printf(The third element = %d.\n,
numbers[ 2 ] ) ;
would give the output
The third element = 6.

numbers

A subscript can also be an expression that


evaluates to an integer.

Accessing
Array
Elements
(cont)

numbers[ (a + b) * 2 ] ;
Caution! It is a logical error when a subscript
evaluates to a value that is out of range for the
particular array. Some systems will handle an
out-of-range error gracefully and some will
not (including ours). Normally, when you see a
file named core (or core*) it means you
exceeded the end of an array!

Individual elements of an array can also be


modified using subscripts.
numbers[ 4 ] = 20 ; /*changes the value of
the element
found at
subscript 4 to 20 */

Modifying
Elements

Initial values may be stored in an array using


indexing, rather than using an array
initialization.
numbers[ 0 ] = 5 ;
numbers[ 1 ] = 2 ;
numbers[ 2 ] = 6 ;
numbers[ 3 ] = 9 ;
numbers[ 4 ] = 3 ;

Since many arrays are quite large, using an array initialization


can be impractical.
Large arrays are often filled using a for loop.

Filling Large
Arrays

for ( i = 0; i < 100; i++ )

{
values [ i ] = 0 ;

would set every element of the 100 element array values to


0.

int score [ 39 ] , gradeCount [ 5 ];

More
Declaration
s

Declares two arrays of type int.


Neither array has been initialized.
score contains 39 elements (one for each student in
a class).
gradeCount contains 5 elements (one for each
possible grade, A - F).

Using
#define for
Array Sizes

#define SIZE 39
//pre-processor
#define GRADES 5
int main ( void )
{
int score [ SIZE ] ;
int gradeCount [ GRADES ] ;

Traversing
Arrays

Let LA be a collection of data


elements stored in the memory of
the computer
Suppose we want to print the
elements or count the number of
elements
These operations can be done with
the help of Traversing
Traversing is the method of
accessing and processing (called
visiting) each element exactly once

Algo. (Traversing a Linear Array): Here


LA is a linear array with lower bound LB
and upper bound UB. This algo traverses
LA by applying the PROCESS operation

Traversal
Algorithms

1.[Initialize counter] Set K := LB


2.Repeat step 3 and 4 while K <= UB

3.
[Visit Element] Apply PROCESS to
LA[K]
4.
[Increase counter] Set K := K + 1
[End of loop]

5.Exit

Algo. (Traversing a Linear Array):


Here LA is a linear array with lower
bound LB and upper bound UB. This
algo traverses LA with LB and UB

Traversal
Algorithms

1.Repeat for K = LB to UB

2.

Apply PROCESS to LA[K]


[End of loop]

3.Exit

Array
Operations

Insertion
Deletion
Sorting
Searching
Merging
Splitting

Insertion of
an element
in an array

Algo: (Insertion into a Linear


Array):We will be inserting an ITEM
into an Array LA at location P(this
algorithm simply replace an item)
1.[Insert] LA [P] := ITEM

2.Exit

Insertion of
elements in
an array

Algo: (Insertion into a Linear Array): Here


LA is a linear array with lower bound LB and
upper bound UB .We will be inserting
elements into an Array LA
1.[Initialize counter] Set K := LB
2.Repeat step 3-5 while K <= UB
3. Input ITEM
4.

[Insert Element] ] LA [K] := ITEM

5.

[Increase counter] Set K := K + 1


[End of loop]

6.Exit

Searching
an element
in an array
(Linear
Search)

Algo: (Searching an element in Linear Array): Here LA is a


linear array with lower bound LB and upper bound UB .We
will search an elements TARGET in an Array LA. If it is found
we will return location ,else we will display message ITEM
NOT FOUND.
1.INPUT TARGET
2.[Initialize counter] Set K := LB
3.Repeat step 3-7 while K <= UB
4. IF LA[K]==TARGET
5.

Return K

6.
7.

EXIT
[Increase counter] Set K := K + 1
[End of loop]

8.PRINT target item not found in array LA


9.Exit

Delete

Algo: (Searching an element in Linear Array):


Here LA is a linear array with lower bound LB
and upper bound UB .We will delete an
elements TARGET in an Array LA.
INPUT TARGET
Call LINEAR SEARCH(LA,TARGET)
IF LOCATION returned

K=LINEAR SEARCH(LA,TARGET)
Repeat step 3-5 while K < UB

Set LA[K] := LA[K + 1]

[Increase counter] Set K := K + 1


[End of loop]

1. Set UB:=K
2.Exit

Description: Here A is a sorted linear array with N


elements. LOC is the location where ITEM is to be inserted.

Insert Item
at specific
location

1.

Set I = N - 1

[Initialize counter]

2.

if (Loc >= 0 and Loc < N) then

2a.

Repeat While (I >= LOC-1)

3.

Set A[I+1] = A[I]

[Move elements downward]

4. Set I = I 1
Loop]

[Decrease counter by 1] [End of While

5.

Set A[LOC] = ITEM

5a.

else

6.

Write Cannot insert value

7.

[End If]

[Insert element]

Sorting and Searching are one of the fundamental


operations in Computer Science
Sorting is the operation of arranging data in some
order

Sorting

In case of numerical data, the arrangement could


be:
Increasing Oder or Ascending Order
Decreasing Order or Descending Order

In case of character data, the order could be:


Alphabetical

Internal Sort
The data to be sorted is all stored in the computers
main memory.

Some
Definitions

External Sort
Some of the data to be sorted might be stored in
some external, slower, device.
In Place Sort
The amount of extra space required to sort the data
is constant with the input size.

There are many sorting algorithms


Selecting a particular algorithm depends on the
type of data and the amount of data
Suppose we have a list of numbers, A with n
elements, then the sorting can be defined as:

Contd

A[0] < A[1] < A[2] < A[3] < < A[n]
(increasing order)
For example:
10
1

5
3

20

18

16
10

25
16

27
18

20

25

27

Sorte
d

There are many, many different types of sorting


algorithms, but the primary ones are:

Types of
Sorting
Algorithms

Bubble Sort

Selection Sort

Insertion Sort

Merge Sort

Quick Sort

Shell Sort

Radix Sort

Swap Sort

Heap Sort

bubble sort: swap adjacent pairs that are out of order


selection sort: look for the smallest element, move to front
insertion sort: build an increasingly large sorted front
portion
merge sort: recursively divide the array in half and sort it

Sorting
algorithms

heap sort: place the values into a sorted tree structure


quick sort: recursively partition array based on a middle
value

other specialized sorting algorithms:


bucket sort: cluster elements into smaller groups, sort
them
radix sort: sort integers by last digit, then 2nd to last,
then ...

One of the very basic sorting algorithms


It takes (n 1) steps or passes to sort a list of n numbers
Therefore the complexity of this algorithm is O(n 2) i.e., it
takes n2 time to sort a list of n numbers

Bubble Sort

Suppose we have a list A[1], A[2], , A[n]


The Bubble sort works as:
1. Compare A[1] and A[2] and arrange them i.e., A[1] < A[2]
2. Then compare A[2] and A[3] and arrange them i.e., A[2] <
A[3]
3. Keep doing this until we have A[n-1] < A[n]

At the end of pass one, the largest element is bubbled


up to nth position in the list
Each pass requires one less comparison then the last
pass

32

51

27 85

66

23

13

57

Suppose we have the following list:

Example
Pass 1:

Compare A[0] and A[1] i.e., 32 and 51, As 32 < 51, no


element is exchanged

32

Contd

27

51 85

66

23

13

57

Compare A[2] and A[3], as A[2] < A[3], items are not
exchanged

32

27

51

85

66 23

13

57

Compare A[3] and A[4], as A[3] > A[4], interchange them

32

27

51 66

85

23

13

57

Compare A[4] and A[5], as A[4] > A[5], exchange

32

Contd

27

51 66

23

85

13

57

Compare A[5] and A[6], as A[5] > A[6] therefore we


exchange them

32

27

51 66

23

13

85

57

Compare A[6] and A[7], as A[6] > A[7], exchange them

32

27

51 66

23

13

57

85

Contd

Pass 2

32

27

51 66

23

85

13

57

27

32

51 66

23

13

85

57

27

32

51 66

23

13

57

85

27

32

51 66

23

13

57

85

27

32

51 23

66

13

57

85

27

32

51 23

13

66

57

85

27

32

51 23

13

57

66

85

Contd

Pass 3:

27

32

51 23

13

57

66

85

27

32

23 51

13

57

66

85

27

32

23 13

51

57

66

85

Contd

Pass 4:

27

32

23 13

51

57

66

85

27

23

32 13

51

57

66

85

27

23

13 32

51

57

66

85

Contd

Pass 5:

27

23

13 32

51

57

66

85

23

27

13 32

51

57

66

85

23

13

27 32

51

57

66

85

Contd

Pass 6:

23

13

27 32

51

57

66

85

13

23

27 32

51

57

66

85

Algo. BubbleSort(Data, N)
1.Repeat steps 2 and 3 for K = 0 to N -1

Bubble Sort
Algorithm

2.

Set PTR := 0 [initialize pointer]

3.

Repeat while PTR <= N K


If Data[PTR] > Data[PTR + 1], then
Interchange DATA[PTR] and Data[PTR + 1]
[End of If]
II.
Set PTR := PTR + 1
[End of while]
I.

[End of for]
4.Exit

Running time:
Worst case: O(N2)
Best case: O(N)

Bubble Sort:
Analysis

Variant:
bi-directional bubble sort
original bubble sort: only works to
one direction
bi-directional bubble sort: works
back and forth.

Selection
Sort

Idea:
Find the smallest element in the array
Exchange it with the element in the first
position
Find the second smallest element and
exchange it with the element in the second
position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the
amount of order in the file

Selection
Sort: Contd

1. Select the best (eg. smallest) item


from the unsorted group, then put the
best item at the end of the sorted
group.
2. Repeat the process until the unsorted
group becomes empty.

Example

Alg.: SELECTION-SORT(A)
8

n length[A]

for j 1 to n - 1

Selection
Sort

do smallest j
for i j + 1 to n
do if A[i] < A[smallest]
then smallest i
exchange A[j] A[smallest]

Selection
Sort:
Analysis

Running time:
Worst case: O(N2)
Best case: O(N2)

Insertion
Sort

Idea: like sorting a hand of playing cards


Start with an empty left hand and the
cards facing down on the table.
Remove one card at a time from the
table, and insert it into the correct
position in the left hand
compare it with each of the cards
already in the hand, from right to left
The cards held in the left hand are sorted
these cards were originally the top
cards of the pile on the table

Insertion Sort
To insert 12, we need to
make room for it by moving
first 36 and then 24.

Contd

2
4 3
0
1
6
6

12

Insertion Sort

6 10 24

36

Contd
12

Insertion Sort
6 10

Contd

24 3
6

12

input array

Insertion
Sort

at each iteration, the array is divided in two sub-arrays:


left sub-array
right sub-array

sorted

unsorted

Insertion
Sort

Alg.: INSERTION-SORT(A)

a1 a2 a3 a4 a 5 a6 a7 a8

for j 2 to n
do key A[ j ]

INSERTIONSORT

key

Insert A[ j ] into the sorted sequence A[1 . . j -1]


ij-1
while i > 0 and A[i] > key
do A[i + 1] A[i]
ii1
A[i + 1] key
Insertion sort sorts the elements in place

Vous aimerez peut-être aussi