Docsity

Sorting Algpothims Logics, Lecture notes of Design and Analysis of Algorithms

Partial preview of the text

Download Sorting Algpothims Logics and more Lecture notes Design and Analysis of Algorithms in PDF only on Docsity!

CSC 301 – Design and Analysis of Algorithms Instructor: Dr. M. Hasan Jamal Lecture# 03: Sorting Algorithms

Types of Sorting Algorithms

  • (^) Non-recursive/Incremental comparison sorting
    • (^) Selection sort
    • (^) Bubble sort
    • (^) Insertion sort
  • (^) Recursive comparison sorting
    • (^) Merge sort
    • (^) Quick sort
    • (^) Heap sort
  • (^) Non-comparison linear sorting
    • (^) Count sort
    • (^) Radix sort
    • (^) Bucket sort 2

Alg.: SELECTION-SORT (A)

n ← length[A]

for j ← 0 to n - 2

smallest ← j

for i ← j + 1 to n-

if A[i] < A[smallest]

smallest ← i

exchange A[j] ↔ A[smallest]

Cost Steps

c 1 1

c

n-

c 3 n-

c

c

c 6

c

n-

Selection Sort Analysis

tj: # of times the inner for loop statement is executed at iteration j

  2 0 n j t j

T n c c n c n c t c t c t c n

n j j n j j n j j

  2 0 n j t j

  2 0 n j t j

Best/Worst/Average Case Analysis T(n) = (n^2 ) Disadvantage: (nn^2 ) running time in all cases

an bn c

2 ( 1 )

 ^ 

n n j n j ( 1 ) 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) ( ) 1 2 ( 1 ) 3 ( 1 ) 4 5 6  7             c n n n c n n c n n T n c c n c n c ( ) ( 1 ) ( 1 ) 7 ( 1 )

 1  2   3   4       

T n c c n c n c t c t c t c n n j j n j j n j j

BubbleSort(A)

**1. n = Length[A];

  1. for j = 0 to n-
  2. for i = 0 to n-j-
  3. if A[i] > A[i+1]
  4. temp = A[i]
  5. A[i] = A[i+1]
  6. A[i+1] = temp
  7. return A;**

cost times c 1 1 c 2 n- c 3 c 4 c 5 c 6 c 7 c 8 1

tj: # of times the inner for loop statement is executed at iteration j

Bubble Sort Analysis 

n j j t 8

T( n) c c (n 1 ) c t c t c t c t c t c n j j n j j n j j n j j n j j              

n j j t 

n j j t 

n j j t 

n j j t

Best/Worst/Average Case Analysis

  • (^) Since we don’t know at which iteration the list is completely sorted, the nested loop will execute completely. a quadratic function of n
  • (^) T(n) = (n 2 ) order of growth in n 2

1 2 3 4 5 6 7 8 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) ( ) ( 1 ) c n n c n n c n n c n n c n n T n c c n c               an bn c 2      2 0 2 n ( 1 ) j n n j 8

T( n) c c (n 1 ) c t c t c t c t c t c n j j n j j n j j n j j n j j              

Best Case Analysis (Modified)

  • (^) The array is already sorted
    • (^) Keep track of no. of swaps done in an iteration
    • (^) Can finish early if no swapping occurs
    • (^) tj = 1  n – 1 comparisons in a single iteration T(n) = c 1 + c 2 (n -1) + c 3 (n -1) + c 4 (n -1) + c 8 = (c 2 + c 3 + c 4 )n + (c 1 – c 2 – c 3 – c 4 + c 8 ) = an + b = (n)

T( n) c c (n 1 ) c t c t c t c t c t c n j j n j j n j j n j j n j j              

Worst Case Analysis (Modified)

  • (^) The array is in reverse sorted order
    • (^) Always A[i] > A[i+1] a quadratic function of n T(n) = (n

) order of growth in n

Advantages

Simple to code

Requires little memory (nin-place)

Disadvantages

(nn

2

) running time in worst & average case

Nobody “EVER” uses bubble sort

(nEXTREMELY INEFFICIENT)

an bn c

     2 0 2 n ( 1 ) j n n j 1 2 3 4 5 6 7 8 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) 2 ( 1 ) ( ) ( 1 ) c n n c n n c n n c n n c n n T n c c n c               8

T( n) c c (n 1 ) c t c t c t c t c t c n j j n j j n j j n j j n j j              

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 2.78^ 7.42^ 0.56^ 1.12^ 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 0: step 0.

Array index 0 1 2 3 4 5 6 7 8 9

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 2.78^ 7.42^ 0.56^ 1.12^ 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 1: step 0.

Array index 0 1 2 3 4 5 6 7 8 9

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 2.78^ 0.56^ 7.42^ 1.12^ 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 2: step 1.

0.56 2.

Array index 0 1 2 3 4 5 6 7 8 9

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 2.78^ 0.56^ 7.42^ 1.12^ 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 2: step 2.

0.56 2.

Array index 0 1 2 3 4 5 6 7 8 9

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 0.56^ 2.78 1.12^ 7.42 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 3: step 1.

1.12 2.

Array index 0 1 2 3 4 5 6 7 8 9

InsertionSort(A, n)

**1. for i = 1 to n-

  1. key = A[i]
  2. j = i – 1
  3. while (j >= 0) and (A[j] > key)
  4. A[j+1] = A[j]
  5. j = j – 1
  6. A[j+1] = key**

Insertion Sort

Value 0.56^ 2.78 1.12^ 7.42 1.17^ 0.32 6.21^ 4.42 3.14^ 7.

Iteration 3: step 2.

1.12 2.

Array index 0 1 2 3 4 5 6 7 8 9