quick sort easy slides, Exams of Software Engineering

quick sort is algo for the students

Typology: Exams

2017/2018

Uploaded on 04/28/2018

mohsin-qurban
mohsin-qurban 🇵🇰

6 documents

1 / 67

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mergesort and Quicksort
Chapter 8
Kruse and Ryba
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43

Partial preview of the text

Download quick sort easy slides and more Exams Software Engineering in PDF only on Docsity!

Mergesort and Quicksort

Chapter 8

Kruse and Ryba

Sorting algorithms

• Insertion, selection and bubble sort have

quadratic worst-case performance

• The faster comparison based algorithm?

O(nlogn)

• Mergesort and Quicksort

Partitioning - Choice 1

  • (^) First n-1 elements into set A, last element set B
  • (^) Sort A using this partitioning scheme recursively
    • (^) B already sorted
  • (^) Combine A and B using method Insert() (= insertion

into sorted array)

  • (^) Leads to recursive version of InsertionSort()
    • (^) Number of comparisons: O(n^2 )
      • (^) Best case = n-
      • (^) Worst case = (^2)

2

^ 

n n

c i

n

i

Partitioning - Choice 2

  • (^) Put element with largest key in B, remaining elements

in A

  • (^) Sort A recursively
  • (^) To combine sorted A and B, append B to sorted A
    • (^) Use Max() to find largest element  recursive SelectionSort()
    • (^) Use bubbling process to find and move largest element to right-most position  recursive BubbleSort()
  • (^) All O(n^2 )

Example

• Partition into lists of size n/

[10, 4, 6, 3]

[10, 4, 6, 3, 8, 2, 5, 7]

[8, 2, 5, 7]

[10, 4] [6, 3]^ [8, 2]^ [5, 7]

[4] [10] [3][6] [2][8] [5][7]

Example Cont’d

• Merge

[3, 4, 6, 10]

[2, 3, 4, 5, 6, 7, 8, 10 ]

[2, 5, 7, 8]

[4, 10] [3, 6]^ [2, 8]^ [5, 7]

[4] [10] [3][6] [2][8] [5][7]

Merge Function

Evaluation

• Recurrence equation:

• Assume n is a power of 2

c 1 if n=

T(n) =

2T(n/2) + c 2 n if n>1, n=2k

Quicksort Algorithm

Given an array of n elements (e.g., integers):

  • (^) If array only contains one element, return
  • (^) Else
    • (^) pick one element to use as pivot.
    • (^) Partition elements into two sub-arrays:
      • (^) Elements less than or equal to pivot
      • (^) Elements greater than pivot
    • (^) Quicksort two sub-arrays
    • (^) Return results

Example

We are given array of n integers to sort:

Partitioning Array

Given a pivot, partition the elements of the array

such that the resulting array consists of:

  1. One sub-array that contains elements >= pivot
  2. Another sub-array that contains elements < pivot

The sub-arrays are stored in the original data array.

Partitioning loops through, swapping elements

below/above pivot.

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index (^) too_small_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index (^) too_small_index

  1. While data[too_big_index] <= data[pivot] ++too_big_index

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

too_big_index (^) too_small_index

  1. While data[too_big_index] <= data[pivot] ++too_big_index