Lect11 Sorting

Post on 11-May-2015

1,421 views 1 download

Tags:

transcript

Lecture 11 Sorting

Aj. Khwunta KirimasthongSchool of Information

TechnologyMae Fah Luang University

1302251 Data Structures and Algorithms

2

Outline

Sorting Concept Sorting Algorithm

Selection Sort Insertion Sort Merge Sort Quick Sort

3

Sorting Concept

Sorting is the operation of arranging data in some given order, such as Ascending

the dictionary, the telephone book Descending

grade-point average for honor students

The sorted data can be numerical data or character data.

Sorting is one of the most common data-processing applications.

4

Sorting Concept (Cont.)

Sorting problemLet A be a sequence of n elements. Sorting A refers to the operation of rearranging the content of A so that they are increasing in order.

Input: A: the sequence of n numbers

Output: A: the reordering of the input sequence such that

1 2( , ,..., )na a a

1 2( , ,..., )na a a

1 2 na a a

5

Selection Sort

One of basic sorting algorithms Selection Sort works as follows:

First, find the smallest elements in the data sequence 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 sequence is sorted.

6

Selection Sort Example5 2 4 6 1 3A

1 2 3 4 5 6

1 2 4 6 5 3A

1 2 3 4 5 6

(1)

1 2 4 6 5 3A(2) 1 2 3 4 5 6

1 2 3 6 5 4A(3)

1 2 3 4 5 6

1 2 3 4 5 6A(4)

1 2 3 4 5 6

1 2 3 4 5 6A(5) 1 2 3 4 5 6

1 2 3 4 5 6A(6)

1 2 3 4 5 6

7

Selection Sort Algorithm

Algorithm SelectionSort(A,n)Input: A: a sequence of n elements

n: the size of A

Output: A: a sorted sequence in ascending order

for i = 1 to n

min = ifor j = i+1 to n

if A[j] < A[min] thenmin = j

Swap(A,min,i)

8

Selection Sort Algorithm (Cont.)Algorithm: Swap(A,min,i)Input: A : a sequence of n elements

min: the position of the minimum value of A while considers at index i of A

i: the considered index of A

Output: exchange A[i] and A[min] temp = a[min]a[min] = a[i]a[i] = temp

9

Selection Sort Analysis

For i = 1, there are _(n-1)_ comparisons to find the first smallest element.

For i = 2, there are _(n-2)_ comparisons to find the second smallest element, and so on.

Accordingly,

(n-1) + (n-2) + (n-3) + … + 2 + 1 = = n(n-1)/2

The best case running time is ___(n2)__ The worst case running time is __ O(n2)__

1

1

n

i

i

10

Insertion Sort

To solve the sorting problem Insertion sort algorithm is almost as simple as

selection sort but perhaps more flexible. Insertion sort is the method people often

use for sorting a hand of cards.

11

Insertion Sort (Cont.)

Insertion sort uses an incremental approach: having the sorted subarray A[1…j-1], we insert the single element A[ j ] into its proper place and then we will get the sorted subarray A[ 1…j ]

pick up

Sorted order Unsorted order

12

5 2 4 6 1 3

key = A[j]

2 5 4 6 1 3

(1)

2 4 5 6 1 3

Insertion Sort Example

(3)

(2)

A

A

A

2key

4key

6key

in-place sorted

5 2 4 6 1 3

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

13

(4)

(6)

(5)

2 4 5 6 1 3

1 2 4 5 6 3

1 2 3 4 5 6

A

A

A

1key

3key

in-place sorted

Insertion Sort Example (Cont.)

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

14

Insertion Sort Algorithm

Algorithm: INSERTION-SORT(A)Input: A : A sequence of n numbers Output: A : A sorted sequence in increasing order of array A

for j = 2 to length(A) key = A[j]//Insert A[j] into the sorted sequence A[1…j-

1] i = j-1while i > 0 and A[i] > key

A[i+1] = A[i]i = i-1

A[i+1] = key

15

Insertion Sort Analysis

The running – time of INSERTION-SORT depends on the size of input.

16

Insertion Sort Analysis (cont.)INSERTION-SORT(A) cost times

1. for j = 2 to length(A) c1

2. key = A[j] c2

3. //Insert A[j] into the sorted sequence A[1…j-1] 0

4. i = j-1 c4

5. while i > 0 and A[i] > key c5

6. A[i+1] = A[i] c6

7. i = i-1 c7

8. A[i+1] = key c8

n

n-1

n-1

n-1

n-1

2

n

jj

t

2

( 1)n

jj

t

2

( 1)n

jj

t

17

Analysis of Insertion Sort (Cont.)

To Compute the total running time of INSERTION-

SORT, ( )T n

1 2 4 52

( ) ( 1) 0( 1) ( 1)n

jj

T n c n c n n c n c t

6 7 82 2

( 1) ( 1) ( 1)n n

j jj j

c t c t c n

18

Analysis of Insertion Sort (Cont.)

Best – case If input array is already sorted. Thus tj = 1

( )n

an + b for constant a and b

Base-case running time of INSERTION-SORT is

T(n) = c1n+c2(n-1)+(0)(n-1)+c4(n-1)+c5(n-1)+c6(0)+c7(0)+c8(n-1) = (c1+c2+c3+c4+c5+c8)n - (c2+c3+c4+c5+c8)

19

Worse – case If input array is in reverse sorted order. Thus tj = j

Worse-case running time of INSERTION-SORT is

for constant a, b and c

2an bn c

Analysis of Insertion Sort (Cont.)

2( )n

6 7 8( 1) ( 1)

( 1)2 2

n n n nc c c n

1

2

)1()1()1()( 5421

nncncncncnT

2 4 5 8( )c c c c

ncccc

cccnccc

nT

8

765421

2665

222222)(

20

Worse – case (Cont.) If input array is in reverse sorted order. Thus tj = j

Analysis of Insertion Sort (Cont.)

1

( 1)

2

n

j

n nj

1

2

)1(

22

nnjt

n

j

n

jj

2

)1()11(

2

)1)1)((1()1()1(

22

nnnnjt

n

j

n

jj

Divide and Conquer Approach

Merge Sort

Quick Sort

22

Divide-and-Conquer Approach The divide-and-conquer approach involves

three steps at each level of the recursion: Divide the problem into a number of sub-problems

that are similar to the original problem but smaller in size.

Conquer the sub-problems by solving them recursively.

Combine the solution of subproblems into the solution for the original problem.

23

Merge Sort

The Merge Sort algorithm closely follows the divide-and-conquer approach. It operates as follows, Divide: Divide the n-element sequence to be

sorted into two subsequences of n/2 elements each.

Conquer: Sort the two subsequences recursively using merge sort

Combine: Merge the two sorted subsequences to produce the sorted solution.

24

Merge Sort (Cont.)

The recursion “bottom out” when the sequence to be sorted has length 1, in which case there is no work to be done, since every sequence of length 1 is already in sorted order.

25

Merge Sort Example

5 2 4 6 1 3A

0 1 2 3 4 5

5 2 4

0 1 2

6 1 3

3 4 5

5 2 4

0 1 2

6 1 3

3 4 5

5 2

0 1

6 1

3 4

DIVIDE&CONQUER

26

Merge Sort Example (cont.)

1 2 3 4 5 6A

0 1 2 3 4 5

2 4 5

0 1 2

1 3 6

3 4 5

2 5 4

0 1 2

1 6 3

3 4 5

5 2

0 1

6 1

3 4

COMBINE

27

Merge Sort Algorithm

Algorithm MergeSort( A, p, q )Input: A: a sequence of n elements

p: the beginning index of A q: the last index of A

Output: A: a sorted sequence of n elementsif(p < q) then

r = floor((p+q)/2) MergeSort(A,p,r)MergeSort(A,r+1,q)Merge(A,p,r,q)

28

Algo Merge(A,p,r,q)Input: A, p, r, q : a sorted subsequences A[p…r] and A[r+1…q] Output: A: a sorted sequence A[p…q]

let i=p, k=p, j=r+1while (i ≤ r) and (j ≤ q) if (A[i] ≤ A[j]) then B[k] = A[i]

k++ i++ else

B[k] = A[j] k=k+1j=j+1

if (i>r) then /* If the maximum value of left subsequence is less than the right subsequence */

while(j ≤ q)B[k] = A[j]k++j++

else if(j > q) then /* If the maximum value of left subsequence is gather than the right subsequence */

while(i ≤ r)B[k] = A[i]k++i++

for k = p to qA[k] = b[k]

29

Merge Sort Analysis

The best case running time is _(n log n)_ The worst case running time is _ O(n log n) _

30

Quick Sort

Quick Sort, likes merge sort, is based on the divide-and-conquer approach.

To sort an array A[p…r] Divide: Partition (rearrange) the array A[p…r] into

two subarray A[p…q -1] and subarray A[q+1…r] such that: Each element of A[p…q -1] is less than or equal to A[q] Each element of A[q+1…r] is greater than A[q]

31

Quick Sort (Cont.)

To sort sorting an array A[p…r] (cont.) Conquer: Sort the two subarray A[p…q -1] and

A[q+1…r] by recursive calls to QuickSort. Combine: Since the subarrays are sorted in place, no

work is needed to combine them: the entire array A[p…r] is now sorted.

32

Quick Sort Example5 2 4 6 1 3A

0 1 2 3 4 5

33

Quick Sort AlgorithmAlgo QuickSort(A, p, r)

Input: A: A sequence of n numbers p: The first index of A

r: The last index of A

Output: A: A sorted sequence in increasing order of array A

if (p < r) then

q = Partition(A,p,r)

QuickSort(A,p,q-1)

QuickSort(A,q+1,r)

• Rearrange the subarray A[p…r] in place.

• The elements that is less than A[q] are placed at the left side of A[q] and

• The elements that is greater than A[q] are placed at the right side of A[q].

34

Quick Sort Algorithm (Cont.)Algo Partition(A, p, r)

Input: A: A sequence of n numbers p: The first index of A

r: The last index of AOutput: A: The rearranged subarray A[p…r]

key = A[r]

i = p – 1

for j = p to r -1

if A[ j ] ≤ key then

i = i+1

exchange A[i] and A[j]

exchange A[i+1] and key

return i+1

35

Quick Sort Analysis

The best case running time is _ (n log n)_ The worst case running time is _ O(n2)_

Q & A