You are on page 1of 94

A brief introduction on

Trees
Graphs
Review and Examine key properties of
Trees and variants
Graph and variants
Discuss usage for solving important problems
(search, sort, selection).
Searching And Sorting Algorithms

linear data structures
oA linear data structure traverses the data elements
sequentially, in which only one data element can directly
be reached. Ex: Arrays and Linked Lists,

Non-linear data structures
owhereas in non-Linear data structure every data item is
attached to several other data items in a way that is
specific for reflecting relationships. The data items are not
arranged in a sequential structure. Ex: Trees, Graphs
A tree is a collection of nodes and directed
edges, satisfying the following properties:
There is one specially designated node called the
root, which has no edges pointing to it.
Every node except the root has exactly one edge
pointing to it.
There is a unique path (of nodes and edges) from
the root to each node.

Node user-defined data structure that that contains
pointers to data and pointers to other nodes:
Root Node from which all other nodes descend
Parent has child nodes arranged in sub-trees.
Child nodes in a tree have 0 or more children.
Leaf node without descendants
Degree # of direct children a tree/sub-tree has.
In-degree: # of branches directed toward the node
Out-degree: # of branches directed away
Degree of node:. Sum (in-degree and out-degree)
Degree of tree: maximum degree of any node
Forest: Collection of trees
Path: sequence of adjacent nodes

Height # of edges on the
longest path from the root to a leaf.

Level Root is at level 0, its
direct children are at level 1, etc.
Recursive definition for height:
1+ max(height(T
L
), height(T
R
))

A
B C
D E
G F
H
If an edge goes from node a to node b, then a is called
the parent of b, and b is called a child of a.
Children of the same parent are called siblings.
If there is a path from a to b, then a is called an
ancestor of b, and b is called a descendent of a.
A node with all of its descendants is called a subtree.
If a node has no children, then it is called a leaf of the
tree.
If a node has no parent (there will be exactly one of
these), then it is the root of the tree.
A
B C
D E
F
G H
I
J K
A is the root
D, E, G, H, J & K are
leaves
B is the parent of D, E &
F
D, E & F are siblings
and children of B
I, J & K are descendants
of B
A & B are ancestors of I
An acyclic connected graph where each node has zero
or more children nodes and there is at most one parent
node, and at most one node has no parents, which is
root node of the tree.

Figure : General Tree
A balanced binary tree is commonly defined as a
binary tree in which the height of the two sub trees of
every node never differs by more than 1.
Figure : A Balanced Tree
Figure : An Unbalanced Tree
Heap is an ordered balanced binary tree in which the
value of the node at root of any subtree can be greater
than, less than or equal to the value of either of its
children.
Figure (a): Max-Heap Figure (b): Min-Heap
A binary tree in which each node is having either
only left sub-tree, either only right sub-tree or no sub-
tree is called as left skewed or right skewed binary
tree respectively.
(a) Left Skewed binary tree
(b) Right Skewed Binary tree

Figure (a) (b)

A binary tree is a tree in which each node can have
maximum of two children.
Thus each node can have no, one or two child.
pointers are used to identify whether it is a left or a right child
Intuitively, a binary tree is a tree in which each node
has no more than two children.

Figure : Binary Tree
(These two binary
trees are distinct.)
A binary search tree is a binary tree in which each node, n,
has a Comparable key (and an associated value) satisfying the
following restriction on the key in any node :
ns value is > all values in its left subtree, T
L
,
ns value is < all values in its right sub-tree, T
R
, and
T
L
and T
R
are both binary search trees.
John
Peter
Brenda
Amy Mary Tom
21
2
3
55
34
13 5
8
In other words, can we
put non-hierarchical
data into a tree. We
will study Binary
Search Trees later.
A binary tree is full if it has no missing nodes.
It is either empty.
Otherwise, the roots sub-trees are full binary trees
of height h 1.
If not empty, each node has 2 children, except the
nodes at level h which have no children.
Contains a total of 2
h+1
-1 nodes (how many leaves?)
This term is
ambiguous, some
indicate that each
node is either full or
empty.
A binary tree of height h is complete if it is full down to
level h 1, and level h is filled from left to right.
All nodes at level h 2 and above have 2 children each,
If a node at level h 1 has children, all nodes to its left
at the same level have 2 children each, and
If a node at level h 1 has 1 child, it is a left child.

A binary tree is balanced if the difference in height between
any nodes left and right sub-tree is s 1.






Note that:
A full binary tree is also complete.
A complete binary tree is not always full.
Full and complete binary trees are also balanced.
Balanced binary trees are not always full or complete.
Complete and Balanced
Not Balanced, Why?
The number of edges in a tree is n-1.
The number of nodes n in a full binary tree is: n = 2
h + 1
1
where h is the height of the tree.
The number of nodes n in a complete binary tree is:
minimum: n = 2
h

maximum: n = 2
h + 1
1 where h is the height of the tree.
The number of nodes n in a full or perfect binary tree is:
n = 2L 1 where L is the number of leaf nodes in the tree.
The number of leaf nodes n in a full or perfect binary tree is:
n = 2
h
where h is the height of the tree.
The number of leaf nodes in a Complete Binary Tree with n
nodes is UpperBound(n / 2).
For any non-empty binary tree with n
0
leaf nodes and n
2
nodes of
degree 2, n
0
= n
2
+ 1.

Basic Idea:
Instead of using pointers to the left and right child of a
node, use indices into an array of nodes representing
the binary tree.
Also, use variable free as an index to the first position
in the array that is available for a new entry. Use either
the left or right child indices to indicate additional,
available positions.
Together, the list of available positions in the array is
called the free list.
Index Item Left
Child
Right
Child
0 Jane 1 2
1 Bob 3 4
2 Tom 5 -1
3 Alan -1 -1
4 Ellen -1 -1
5 Nancy -1 -1
6 ? -1 7
7 ? -1 8
8 ? -1 9
9 . . . . . . . . .
root
0
free
6
Jane
Alan
Tom Bob
Nancy Ellen
Index Item Left
Child
Right
Child
0 Jane 1 2
1 Bob 3 4
2 Tom 5 -1
3 Alan -1 -1
4 Ellen -1 -1
5 Nancy 6 -1
6 Mary -1 -1
7 ? -1 8
8 ? -1 9
9 . . . . . . . . .


root
0
free
7
* Mary Added under Nancy.
Jane
Alan
Tom Bob
Nancy Ellen
Mary
Index Item Left
Child
Right
Child
0 Jane 1 2
1 Bob 3 -1
2 Tom 5 -1
3 Alan -1 -1
4 ? -1 7
5 Nancy 6 -1
6 Mary -1 -1
7 ? -1 8
8 ? -1 9
9 . . . . . . . . .
root
0
free
4
* Ellen deleted.
Jane
Alan
Tom Bob
Nancy
Mary
1
2 3
4 7 5 6
Let i, 1 < i < n, be the number assigned to an element of a complete binary tree.

1
2 3
4 6
6 1 2 3
5 7
Array-based representations allow for efficient traversal.
Consider the node at index i.
Left Child is at index 2i+1.
Right Child is at index 2i+2.
Parent is at floor( (i-1)/2 ).
1
3
7
1 3 7
Drawback of array-based trees: Example has only 3
nodes, but uses 2
h+1
-1 array cells to store it
However, if the array
is just numbers or
pointers, not a huge
concern.

We can encode an n-ary tree as a binary tree, by
having a linked-list of children. Hence still two
pointers, one to the first child and one to the next
sibling.
Kinda rotates the tree.
Insertion In BST
Deleting from a BST
Binary Search Tree Traversing
Depth-first Traversal
Preorder
In-order
Post-order
Breadth-First Traversal
Level order
Constructing Tree for Given in-Order & Pre/Post Order Traversal of Tree
Searching and Sorting using Binary Search Tree


Figure. Insert a new element whose value is 42
set x=42, p=36 and found =0;
While (p!=null and !found)
parent = p;
if (p->Info = x) then
Set found = 1;
Else if (x < p->Info) then
Set p = p->lchild;
Else
Set p = p->rchild;
[end of while loop at Step2]
1
st
iteration
Set p = 40 //p->rchild;
2
nd
iteration
Set p = 44 //p->rchild;
3
rd
iteration
if (42 < 44) then
Set p = 42 //p->lchild;








To delete an element x, we must first verify that its key is exist into
the binary search tree.
Case A:
1. When the node to be deleted is as leaf node of the binary search
tree
2. When the node to be deleted has a single child either left or right
child.
Case B
1. When the nodes to be deleted have both left and right child.
The algorithm BTREEDEL calls DELCASEA and DELCASEB.
The pointer variable dnode and pardnode denotes the location of the
deleted node and its parent respectively.
The pointer variable succ and parsucc denotes the inorder successor
of the node to be deleted and parent of the successor respectively.

Example: Delete node whose value is 40 in BST of the given figure
Step 1. Search the node to be deleted
return dnode and parentnode if exist
Step2.
If (dnode->lchild ==NULL) and (dnode->rchild==NULL) then
Set child = NULL;
else if (dnode->lchild)!=NULL then
Set child = dnode->lchild;
Else
Set child = dnode->rchild;
Step3: If (pardnode !=NULL) then
if (dnode=pardnode->lchild)
Set pardnode->lchild = child;
Else
Set pardnode->rchild = child;
Else
root = child;
Step4: end DELCASEA




Delete node whose value is 40 in BST of the given figure
.
dnode=40 and
parentnode=44.
Executed
child =
NULL
Executed
pardnode->lchild =NULL

Depth-first Traversal
Preorder
Inorder
Postorder
Breadth-First Traversal
Level order

Basic Idea:
1) Visit the root.
2) Recursively invoke preorder on the left sub-tree.
3) Recursively invoke preorder on the right sub-tree.
Preorder Result: 60, 20, 10, 40, 30, 50, 70
60
40 10
70 20
30 50
1
4
5
3
2
7
6
Basic Idea:
1) Recursively invoke inorder on the left sub-tree.
2) Visit the root.
3) Recursively invoke inorder on the right subtree.
Inorder Result: 10, 20, 30, 40, 50, 60, 70
60
40 10
70 20
30 50
6
4
3
1
2
7
5
Basic Idea:
1) Recursively invoke postorder on the left sub-tree.
2) Recursively invoke postorder on the right subtree.
3) Visit the root.
Postorder Result: 10, 30, 50, 40, 20, 70, 60
60
40 10
70 20
30 50
7
4
2
1
5
6
3
f
c
j
a d
h k
i
Visit the tree in left-to-right, by level, order:
Visit the root node and put its children in a queue (left to right).
Dequeue, visit, and put dequeued nodes children into the queue.
Repeat until the queue is empty.
Level order traversal of
the figure given below




fcjadhki
Basic Idea for a Nonrecursive, Inorder Traversal:
1) Push a pointer to the root of the binary tree onto a stack.
2) Follow leftChild pointers, pushing each one onto the stack,
until a NULL leftChild pointer is found.
3) Process (visit) the item in this node.
4) Get the nodes rightChild pointer:
If it is not NULL, then push it onto the stack, and return to
step 2 with the leftChild pointer of this rightChild.
If it is NULL, then pop a node pointer from the stack, and
return to step 3. If the stack is empty (so nothing could be
popped), then stop the traversal is done.
Example
A binary tree T has 9 nodes. The inorder and preorder traversals of T yield the
following sequence of nodes
Inorder E A C K F H D B G
Preorder F A E K C D H G B
Draw the tree.

The tree T is drawn from its root downward in different stages as follows.
The root of T is obtained by choosing the first node in its preorder. Thus F is
the root of T
The inorder of T to find the nodes in the left subtree T of F. Thus T1 consists of
nodes E, A, C, K then. Then the left child of F is obtained by choosing the first
node in the preorder of T1. Thus A is the left child of F.
Similarly, the right subtree T2 of F consists of the nodes H, D, B, and G. D is
the right child of F. Repeating the above process with each new node is given
below.

Figure Construction of binary tree from inorder and preorder sequence

The postorder sequence of tree is visit left subtree, visit right subtree then visit rooted node.
E C K A H B G D F


Tree finite, non-empty set of elements with a root with all
remaining elements in sub-trees
Real World applications Binary space partitioning trees (BSP)
efficient method for determining visibility relationships between
a static group of 3d polygons from an arbitrary viewpoint. A
viewpoint can be composed of a clusters of polygons. A plane
can be found that separates on set of clusters from another thus
some are visible and some arent. The tree is rooted at the first
partitioning plane chose, and the internal nodes describe the
partitioning planes and the leaves describe the regions of space.
VIDEO GAMES DOOM XXX, Quake X, etc
Compiler construction dealing with syntax analysis, symbol
table construction, arithmetic expression manipulation.
You are a child of two parents, who in turn came from two
parents.Hierarchical!!!
Discuss reasons for
using trees vs. using lists
Efficient retrieval
traversal of list vs. traversal of a tree.
Sorting
tree vs. list
Definition
A graph G consists of two sets V and E.
o The set V is a finite, non-empty set of vertices.
o The set E is a set of pairs of vertices, these pairs are called edges.
Graph G can be represent a set of G =(V,E)
A graph is nodes joined by edge i.e. A set of nodes V and
a set of edges E
A node is defined by its name or label.
An edge is defined by the two nodes which it connects,
plus optionally:
An order of the nodes (direction)
A weight or cost (usually a number)

Graph G = (V, E)
V = set of vertices
E = set of edges _ (VV)
Types of graphs
Undirected: edge (u, v) = (v, u); for all v, (v, v) e E (No
self loops.)
Directed: (u, v) is edge from u to v, denoted as u v.
Self loops are allowed.
Weighted: each edge has an associated weight, given
by a weight function w : E R.
Dense: |E| ~ |V|
2
.( # of edges/#of nodes is large)
Sparse: |E| << |V|
2
.( #of edges/#of nodes is small )
|E| = O(|V|
2
)
Path:- (u1,u2,u3v) eV and
(u1,u2),(u2,u3),uk,v) e E are in G(E).
Degree
i. I n undirected graph
A nodes degree is the number of its edges
ii. I n directed graph
I ndegree:-is the # of edges for which v is head.
Outdegree:- is the # of edges for which v is tail.
length: the number of edges in the path
cost: the sum of the weights on each edge in the path
cycle: a path that starts and finishes at the same node
An acyclic graph contains no cycles

Two nodes are adjacent if they are connected by an
edge.
If (u, v) e E, then vertex v is adjacent to vertex u.
Adjacency relationship is:
Symmetric if G is undirected.
Not necessarily so, if G is directed.
If G is strongly connected:
There is a path between every pair of vertices.
|E| > |V| 1.
Furthermore, if |E| = |V| 1, then G is a tree.

For example :-(undirected graph) in the first Figure
V(G)={1,2,3,4}
E(G)={(1,2),(2,4),(4,3),(3,1)}
For example :-( directed graph) See the second Figure.
V(G)={1,2,3,4}
E(G)={(1,2),(2,1)(2,4)(4,2),(4,3)(3,4),(3,1)(1,3)}






Figure Undirected graph (unweighted)
Figure Directed graph (unweighted)
Two standard ways.
Adjacency Lists.




Adjacency Matrix.
a
d c
b
a
d c
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0





a
b
c
d
b
a
d
d c
c
a b
a c
Consists of an array Adj of |V| lists.
One list per vertex.
For u e V, Adj[u] consists of all vertices adjacent to u.

a
d c
b





a
b
c
d
b
c
d
d c
a
d c
b
If weighted, store weights
also in adjacency lists.





a
b
c
d
b
a
d
d c
c
a b
a c
|V| |V| matrix A.
Number vertices from 1 to |V| in some arbitrary manner.
A is then given by:

e
= =
otherwise 0
) , ( if 1
] , [
E j i
a j i A
ij
a
d c
b
1
2
3
4
1 2 3 4
1 0 1 1 1
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0
a
d c
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
A = A
T
for undirected graphs.
In the given graph G=(V,E), we want to visit all vertices in G
that are reachable from vertex v, where v eV.
Problems with the Graph Traversal:
I. No First Node. So there must be a way to find out the starting node
of the graph. User can enter the starting point or there can be several
other methods to find out the starting point.
II. When traversing a graph, we must be careful to avoid going round in
circles. We do this by marking the vertices which have already been
visited. List of visited nodes can also be maintained.
III. No natural order among the successor of a particular node.
IV. Nodes which are not connected or to which there is no path.

We have two common search methods:
1. Breadth first search
2. Depth first search

Breadth First Search
It is based on FIFO system using a Queue .
Depth First Search
It is based on LIFO system using a Stack .



Figure Undirected graph, for finding the breadth first search and depth first search

The steps of over search follow as start node 1
1. initially, add 1 to queue as follows
Front=1 Rear =1 Queue:1
2. Delete front 1 and add adjacent nodes of 1 which
is not visited.
Front= 2 Rear=5 Queue:2, 3, 4, 5
3. Delete front 2 and add adjacent nodes of 2 which
is not visited.
Front=3 Rear=6 Queue:3, 4, 5, 6
4. Delete front 3 and add adjacent nodes of 3 which
is not visited.
Front=4 Rear=7 Queue:4, 5, 6, 7

5. Delete front 4 and add adjacent nodes of 4 which
is not visited.
Front=5 Rear=8 Queue:5, 6, 7, 8
6. Delete front 5 from queue and add to queue
adjacent nodes of 5 which is not visited.
Front=6 Rear=8 Queue:6, 7, 8
7. Delete front 6 from queue and add to queue
adjacent nodes of 6 which is not visited.
Front=7 Rear=9 Queue:7, 8, 9
8. Delete front 7 from queue and add to queue
adjacent nodes of 7 which is not visited.
Front=8 Rear=9 Queue:8, 9
9. Delete front 8 from queue and add to queue
adjacent nodes of 8 which is not visited.
Front=9 Queue:9
Rear=9
10. Delete front 9 from queue and add to queue
adjacent nodes of 9 which is not visited.
Front=0 Queue:0
Rear=0
Breadth first search is 1, 2, 3, 4, 5, 6, 7, 8, 9
The steps of over search follow as start node 1
1 Initially, push 1 onto stack as follows
Stack: 1
2 Pop top 1 and push all adjacent nodes of 1 which is not
visited .
Stack: 2, 3, 4, 5
3 Pop top 5 and push all adjacent nodes of 5 which is not
visited.
Stack : 2, 3, 4
4 pop top 4 and push all adjacent nodes of 4 which is not
visited.
Stack : 2, 3, 7, 8
5 pop top 8 and push all adjacent nodes of 8 which is not
visited.
Stack : 2,3,7
6 pop top 7 and push all adjacent nodes of 7 which is not
visited.
Stack : 2,3,6
7 pop top 6 and push all adjacent nodes of 6 which is not
visited.
Stack : 2,3,9
8 pop top 9 and push all adjacent nodes of 9 which is not
visited.
Stack : 2,3
9 pop top 3 and push all adjacent nodes of 3 which is not
visited.
Stack : 2
10 pop top 2 and push all adjacent nodes of 2 which is not
visited.
Stack : empty
Depth first search sequence is: 1, 5, 4, 8, 7, 6, 9, 3, 2
Electrical network problems Electrical network
analysis and synthesis are mainly the study of
network topology
Topological sort A topological sort of a graph can
be viewed as an orderings of its vertices along a
horizontal line so that all directed edges go from left
to right.
Directed acyclic graphs are used in many
applications to indicate precedence among events
such as code optimization techniques of complier.

Shortest path A path from source vertex v
to w is shortest path if there is shortest path
from v to u and u to w with lower costs. The
shortest paths are not necessarily unique.
The most common shortest path problem:-
Traveling salesman.
Airline, The shortest path find which route
provide minimum air fair total time of flights.

Minimum spanning tree (MST) A spanning tree
for a graph, G = (V, E), is a sub-graph of G that is a
tree and contains all the vertices of G.
In a weighted graph, the weight of a graph is the sum
of the weights of the edges of the graph. A MST for a
weighted/cost graph is a spanning tree with minimum
cost.
There are many applications where minimum
spanning tree is needed

To find cheapest way to connect a set of terminals,
cities, electrical, electronic components of a circuit,
computers, or premises by using roads, wires or
wireless, or telephone lines.
The solution is a MST, which has an edge for each
possible connection weighted by the cost of that
connection.
The routing problems to find path among the system
over Internet, also need MST.
Prims and Kruskals algorithm is the most common
solution for MST.

Searching Algorithms
Sequential Search
Binary Search

Sorting Algorithms
Bubble Sort
Insertion Sort
Quick Sort

Example
Suppose we have an array of integers.

Lets search for the number 3. We start at from
beginning and check the first element in the array. Is it
3?

Example
Suppose we have an array of integers.

Lets search for the number 3. We start at from
beginning and check the first element in the array. Is it
3?

Example
Suppose we have an array of integers.

Lets search for the number 3. We start at from
beginning and check the first element in the array. Is it
3?

Example
Suppose we have an array of integers.

Lets search for the number 3. We start at from
beginning and check the first element in the array. Is it
3?

Example
Suppose we have an array of integers.

Lets search for the number 3. We start at from
beginning and check the first element in the array. Is it
3?

Example
Suppose the following
array of integers is given.
2 6 7 34 76 123 234 567 677 986
We want to seek the
value 123 from this array.

Suppose the array to be sorted is : 6 1 4 3 9

Suppose the array to be sorted is : 6 1 4 3 9
First Pass
Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Second Pass

Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Second Pass
1 4 3 6 9 -> 1 4 3 6 9 *No swap takes place

Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Second Pass
1 4 3 6 9 -> 1 4 3 6 9 *No swap takes place
1 4 3 6 9 -> 1 3 4 6 9

Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Second Pass
1 4 3 6 9 -> 1 4 3 6 9 *No swap takes place
1 4 3 6 9 -> 1 3 4 6 9
1 3 4 6 9 -> 1 3 4 6 9

Suppose the array to be sorted is : 6 1 4 3 9
First Pass
6 1 4 3 9 -> 1 6 4 3 9
1 6 4 3 9 -> 1 4 6 3 9
1 4 6 3 9 -> 1 4 3 6 9
1 4 3 6 9 -> 1 4 3 6 9
Second Pass
1 4 3 6 9 -> 1 4 3 6 9 *No swap takes place
1 4 3 6 9 -> 1 3 4 6 9
1 3 4 6 9 -> 1 3 4 6 9
1 3 4 6 9 -> 1 3 4 6 9

Third Pass
1 3 4 6 9 -> 1 3 4 6 9 *No swap takes place
1 3 4 6 9 -> 1 3 4 6 9
1 3 4 6 9 -> 1 3 4 6 9
1 3 4 6 9 -> 1 3 4 6 9

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)
1 6 8 4 5 3 7 2 (Consider indices 0 - 2)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)
1 6 8 4 5 3 7 2 (Consider indices 0 - 2)
1 4 6 8 5 3 7 2 (Consider indices 0 -3)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)
1 6 8 4 5 3 7 2 (Consider indices 0 - 2)
1 4 6 8 5 3 7 2 (Consider indices 0 -3)
1 4 5 6 8 3 7 2 (Consider indices 0 -4)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)
1 6 8 4 5 3 7 2 (Consider indices 0 - 2)
1 4 6 8 5 3 7 2 (Consider indices 0 -3)
1 4 5 6 8 3 7 2 (Consider indices 0 -4)
1 3 4 5 6 7 8 2 (Consider indices 0 -5)

Given the array: 6 8 1 4 5 3 7 2 - and the goal is to
put it into ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0 - 1)
1 6 8 4 5 3 7 2 (Consider indices 0 - 2)
1 4 6 8 5 3 7 2 (Consider indices 0 -3)
1 4 5 6 8 3 7 2 (Consider indices 0 -4)
1 3 4 5 6 7 8 2 (Consider indices 0 -5)
1 2 3 4 5 6 7 8 ( the array is sorted!)

You might also like