Philadelphia University
Faculty of Information Technology
Department of Applied Computer Science
A Course for all Scientific Colleges
Module:
Computer Skills 2 for Scientific Colleges (710104)
Section: 2
Lecturer: Dr. Nadia Y. Yousif
Examples on Arrays
Sorting an Array
There are several sorting algorithms that are used to sort arrays of elements in ascending or
descending order.
The algorithm presented here is the Insertion sort.
Definition:
Let
a
0
, .
..,
a
n
1
be the sequence to be sorted. At the beginning and after each iteration of the
algorithm the sequence consists of two parts: the first part
a
0
, .
..,
a
i
1
is already sorted, the second
part
a
i
, .
..,
a
n
1
is still unsorted (
i
0, .
..,
n
).
In order to insert element
a
i
into the sorted part, it is compared with
a
i
1
,
a
i
2
etc. When an element
a
j
with
a
j
a
i
is found,
a
i
is inserted behind it. If no such element is found, then
a
i
is inserted at
the beginning of the sequence.
After inserting element
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '11
 hibasaadeh
 pH, Insertion Sort, Philadelphia University Faculty of Information Technology Department of Applied Computer Science, Nadia Y. Yousif

Click to edit the document details