25
Prepared by : shamiel. H first simester 2011/2012 UOH-CCSE-ICS353-prepared 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

Selection Sort Insertion Sort Bottomup Merge Chapter 1

Embed Size (px)

Citation preview

Page 1: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Prepared by : shamiel. H first simester 2011/2012

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

Page 2: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 3: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 4: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 5: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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 - 12. k i3. for j i + 1 to n

{Find the i th smallest element.}4. if A[j] < A[k] then k j5. end for6. if k i then swap A[i] and A[k]7. end for

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

5

Page 6: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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 50UOH-CCSE-ICS353-prepared

by:Shamiel .H -Natalia .A6

Page 7: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

nninnin

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

7

Page 8: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 9: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 10: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 11: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Algorithm: INSERTIONSORTInput: An array A[1..n] of n elements.Output: A[1..n] sorted in nondecreasing order.1. for i 2 to n2. x A[i]3. j i - 14. while (j >0) and (A[j] > x)5. A[j + 1] A[j]6. j j - 17. end while8. A[j + 1] x9. end for

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

11

Example sort : 34 8 64 51 32 21

Page 12: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 13: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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 n2 in the worst case .

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

13

Page 14: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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 n2 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

Page 15: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 16: Selection Sort Insertion Sort Bottomup Merge Chapter 1

• Continue this way in the jth iteration, we merge n/2j pairs of sorted sequences of size 2j-1 to yields n/2j sorted sequences of size 2j.

• If there are k remaining elements , where 1≤ k ≤ 2j-1 then they passed to the next iteration• If 2j-1 < k<2j then they are merged from a sorted

sequence of size k .

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

16

Algorithm description

Page 17: Selection Sort Insertion Sort Bottomup Merge Chapter 1

ExampleTo 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 25 2 3 6

1 2 3 6

Merge

1 2 2 3 4 5 6 6

Merge

Page 18: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Algorithm BOTTOMUPSORT• Input: An array A[1..n] of n elements• Output: A[1..n] sorted in non- decreasing order .• 1. t←1• 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

Page 19: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 20: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 21: Selection Sort Insertion Sort Bottomup Merge Chapter 1

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

Page 22: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Analysis of Bottom up merge sorting

• In any jth iteration of the while loop :-• n/2j merge operations on two sub-arrays of size

2j-1

• the number of comparisons needed to merge is between 2j-1 and 2j -1 .

• the number of comparisons needed in the jth

iteration is between (n/2j )2j-1 and (n/2j)(2j -1).

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

22

Page 23: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Analysis of Bottom up merge sorting

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

23

5 2

2 5

Size = k= 2j-1 = 20 = 1

Let k = jSize = n = 2k = 21 = 2Now n = 2k 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

Page 24: Selection Sort Insertion Sort Bottomup Merge Chapter 1

Analysis : number of comparisons

.1log

11

)2

11(

2

1

)2

()12(2

2

log

222

2

1

11

1

1

1

nnn

nnkn

nkn

nkn

nn

n

nnknnn

k

k

jj

k

jj

jk

jj

k

j

jk

jj

UOH-CCSE-ICS353-prepared by:Shamiel .H -Natalia .A

24

Page 25: Selection Sort Insertion Sort Bottomup Merge Chapter 1

• 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 nlogn–n+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