Vous êtes sur la page 1sur 25

UOH-CCSE-ICS353-prepared by:Shamiel

.H -Natalia .A
1
ICS353
Lectures Notes - shamiel Hashim Ebrahim

Reference Book : Algorithms Design Techniques and Analysis
by : M.H.Alsuwaiyel
Ch1: Sec 1.5,1.6,1.7

1.5. Selection Sort Algorithm


Why do we care so much about sorting?

sorting is used by many applications

Sorting is initial step of many algorithms

many techniques can be illustrated by studying sorting
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
2
Selection sort
Let A[1..n] be any array of n elements simply selection
sort works :-
first find the smallest in the array and exchange it
with the element in the first position, then find the
second smallest element and exchange it with the
element in the second position, and continue in this
way until the entire array is sorted.


UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
3
Selection sort
This method described in selection sort algorithm .
Selection sort is:
The simplest sorting techniques.
a good algorithm to sort a small number of elements
an incremental algorithm induction method


UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
4
Selection Sort Algorithm
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in non-decreasing order.
1. for i 1 to n - 1
2. k i
3. for j i + 1 to n
{Find the i th smallest element.}
4. if A[j] < A[k] then k j
5. end for
6. if k = i then swap A[i] and A[k]
7. end for
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
5
Example
Sort array A[1..7]=
Using selection sort


5 1 12 16 2 14 50
5 1 12 16 2 14 50
1 5 12 16 2 14 50
1 2 12 16 5 14 50
1 2 5 16 12 14 50
1 2 5 12 16 14 50
1 2 5 12 14 16 50
1 2 5 12 14 16 50
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
6
Analysis of Algorithms
It easy to see that the number of element comparisons
performed by algorithm is





Observation 1: the number of element comparison by
algorithm selection sort is n(n-1)/2 the number of element
assignments is between 0 and 3(n-1).

= = + + + =
1
1
1
1
2
) 1 (
1 ... ) 2 ( 1 ( ) (
n
i
n
i
n n
i n n i n
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
7
1.6. INSERTION SORT
Insertion sort it is another sorting method in which the
number of comparisons depends on the order of the
input elements, is called INSERTIONSORT , this algorithm
work as follow :-
we begin with the sub-array of size 1 ,A[1] which is
already sorted .
Next ,A[2] is inserted before or after A[1] depending on
whether is smaller than A[1] or not.
continuing this way in the ith iteration A[i] is picked and
transferred to its correct position in the sorted sub-array
A[1..i-1] .

UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
8
INSERTION SORT
In each iteration of the scan of the sub-array ,A[1..i-1] an
element is shifted one position up to a higher index .
The process of scanning comparison and shifting
continues until:
Either an element =A[i] is found
Or when all the sorted sequence so far is scanned .
Then A[i] is inserted in its proper position.
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
9
Insertion Sort -Example
The Array to Sort:
The six-element array (8, 4, 7, 3, 9, 3)
Iteration 1:
Sort the first two elements as follows:
4 8 7 3 9 3
Iteration 2:
The 7 in element 3 is put in the proper position relative to elements 1 and 2
Specifically, the 4 in position 1 is left where it is, the 8 in position 2 is moved up one, and
the 7 is put in the "hole" (i.e., position 2)
4 7 8 3 9 3
Iteration 3:
The 3 in element 4 must be put in the proper position relative to elements 1 ,2 and 3
Specifically, the 4, 7, and 8 are all shifted up 1 position and the 3 is put in the "hole" (i.e.,
position 1)
3 4 7 8 9 3
The Remaining Iterations:
3 4 7 8 9 3
3 3 4 7 8 9

UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
10
Algorithm: INSERTIONSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing order.
1. for i 2 to n
2. x A[i]
3. j i - 1
4. while (j >0) and (A[j] > x)
5. A[j + 1] A[j]
6. j j - 1
7. end while
8. A[j + 1] x
9. end for
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
11
Example sort : 34 8 64 51 32 21
Observation 1
The number of element comparisons performed
by Algorithm insertion sort is between n-1 and
n(n-1)/2.
The number of element assignments is equal to
the number of element comparisons plus n-1
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
12
Remarks
Remarks on selection and Insertion sort algorithms:-
1- there is a correlation between element comparisons
and assignments in algorithm Insertion sort which is not
the case with SELECTIONSORT .
The number of element comparison in Insertion sort is
sensitive to the relative ordering of the array elements
which is not the case SELECTIONSORT.
Insertion sort and selection sort are both inefficient as the
number of operations required to sort n elements is
proportional to n
2
in the worst case .
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
13
1.6. Bottom Up Merge Sorting
The previous discussed sorting methods are inefficient in
the sense that they required a number of operation
proportional to n
2
to sort n element
The following sorting algorithm there is fewer element
comparisons by divide the input element and merge each
pair in to one 2 element sorted sequence and continue in
this way to yields a sorted sequence, in the following
steps :-


UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
14
Algorithm description
Let A be an array of n elements that to be sorted.
Fist merge n/2 consecutive pairs of elements to get n/2
sorted sequence of size 2 , if there is one remaining
element then it is passed to the next iteration .
Next we merge n/4 pairs of consecutive 2 element
sequence to yields n/4 sorted sequence of size 4, if there
are one or two remaining elements then they are passed
to the next iteration , if there are three elements left two
sorted elements are merged with one element to form a 3
element sorted sequence .

UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
15
Continue this way in the jth iteration, we merge
n/2
j
pairs of sorted sequences of size 2
j-1
to yields
n/2
j
sorted sequences of size 2j.
If there are k remaining elements , where
1 k 2
j-1
then they passed to the next iteration
If 2
j-1
< k<2
j
then they are merged from a sorted
sequence of size k .
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
16
Algorithm description
Example
To sort the array 5,2,4,6,1,3,2,6

UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
17
Merge
1 3
Merge
4 6
2 4 5 6
Merge
2 5
Merge
2 6
Merge
initial sequence
4 6 1 2 5 2 3 6
1 2 3 6
Merge
1 2 2 3 4 5 6 6
Merge
Algorithm BOTTOMUPSORT
Input: An array A[1..n] of n elements
Output: A[1..n] sorted in non- decreasing order .
1. t1
2. while t<n
3. s t; t 2s ,i 0
4. while i+t n
5. merge(A,i+1,i+s,i+t)
6. i i+t
7.End while
8.if i+s <n then merge (A,i+1,i+s,n)
9.End while

UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
18
Analysis of Bottom up merge sorting
In the first iteration :-
Initially all elements are individual sorted arrays.
n sequences of one element each are merged in
pairs .
the number of comparisons needed to merge
each pair is 1,hence the number of comparison is
n/2 .
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
19
Analysis of Bottom up merge sorting
In the second iteration :-
- n/2 sorted sequence of two elements each merge
in pairs.
- The number of comparisons need to merge each
pair is either 2 or 3 hence the number of
comparison in this iteration between (n/4)*2 and
(n/4)*3.


UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
20
Analysis of Bottom up merge sorting
In the third Iteration :-
n/4 sorted sequence of four elements each are
merged in pair .
the number of comparisons needed to merge each
pair is between 4 and 7.
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
21
Analysis of Bottom up merge sorting
In any j
th
iteration of the while loop :-
n/2
j
merge operations on two sub-arrays of size

2
j-1

the number of comparisons needed to merge is
between 2
j-1
and 2
j
-1 .
the number of comparisons needed in the j
th
iteration is between (n/2
j
)2
j-1
and (n/2
j
)(2
j
-1).
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
22
Analysis of Bottom up merge sorting
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
23
5 2
2 5
Size = k= 2
j-1
= 2
0
= 1
Let k = j
Size = n = 2
k
= 2
1
= 2
Now
n = 2
k
k= log n
So for outer while loop k= log n
and at every iteration there are n
numbers on which log n operations
are performed so it is equal to
nlogn
Analysis : number of comparisons
. 1 log
1
1
)
2
1
1 (
2
1
)
2
( ) 1 2 (
2
2
log
2 2
2
2
1
1 1
1
1
1
+ =
|
.
|

\
|
=
=
=
=
= = =
|
.
|

\
|



=
= =
=

=
n n n
n
n kn
n kn
n kn
n
n
n
n n kn n n
k
k
j
j
k
j
j
j
k
j
j
k
j
j
k
j
j
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
24
The total number of element assignments is
exactly 2 * n * log n .
Observation :-
The total number of element comparison
performed by algorithm to sort an array of n
element where n is a power of 2 is between
(nlogn)/2 and nlognn+1 and the total number of
element assignments done by the algorithm is
exactly 2n log n .
UOH-CCSE-ICS353-prepared by:Shamiel
.H -Natalia .A
25

Vous aimerez peut-être aussi