Académique Documents
Professionnel Documents
Culture Documents
and Algorithms
Arrays
Objectives
Types of
Data
Structures
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
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
Contd
int numbers[ 5 ] ;
Array
Declaration
and
Initialization
int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;
myarray =
Accessing
Array
Elements
numbers
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!
Modifying
Elements
Filling Large
Arrays
{
values [ i ] = 0 ;
More
Declaration
s
Using
#define for
Array Sizes
#define SIZE 39
//pre-processor
#define GRADES 5
int main ( void )
{
int score [ SIZE ] ;
int gradeCount [ GRADES ] ;
Traversing
Arrays
Traversal
Algorithms
3.
[Visit Element] Apply PROCESS to
LA[K]
4.
[Increase counter] Set K := K + 1
[End of loop]
5.Exit
Traversal
Algorithms
1.Repeat for K = LB to UB
2.
3.Exit
Array
Operations
Insertion
Deletion
Sorting
Searching
Merging
Splitting
Insertion of
an element
in an array
2.Exit
Insertion of
elements in
an array
5.
6.Exit
Searching
an element
in an array
(Linear
Search)
Return K
6.
7.
EXIT
[Increase counter] Set K := K + 1
[End of loop]
Delete
K=LINEAR SEARCH(LA,TARGET)
Repeat step 3-5 while K < UB
1. Set UB:=K
2.Exit
Insert Item
at specific
location
1.
Set I = N - 1
[Initialize counter]
2.
2a.
3.
4. Set I = I 1
Loop]
5.
5a.
else
6.
7.
[End If]
[Insert element]
Sorting
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.
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
Types of
Sorting
Algorithms
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Shell Sort
Radix Sort
Swap Sort
Heap Sort
Sorting
algorithms
Bubble Sort
32
51
27 85
66
23
13
57
Example
Pass 1:
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
32
27
51 66
85
23
13
57
32
Contd
27
51 66
23
85
13
57
32
27
51 66
23
13
85
57
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.
3.
[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
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
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
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