searching, sorting, complexity part 02

149

Upload: others

Post on 23-Apr-2022

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Searching, Sorting, Complexity Part 02
Page 2: Searching, Sorting, Complexity Part 02

Searching, Sorting, Complexity Part 02

Page 3: Searching, Sorting, Complexity Part 02

• Efficiency– Producing desired results with little to no waste– Well organized and prevents wasteful use of a resource

• Resources– Time– Space

• How do we measure efficiency?– Algorithms do not require computers

Page 4: Searching, Sorting, Complexity Part 02

• Complexity– Classifies Computational Problems based on inherent difficulty– Relates problems to each other– Time and Space

• Asymptotic Analysis– A way to describe a limiting behavior / function– Limits in math are a value that a function approaches as the input approaches some value– Time and Space Complexity

Page 5: Searching, Sorting, Complexity Part 02

• Theoretical upper bound of an algorithm• The “Worst Case” scenario• Let f and g be functions defined on some subset of real numbers

𝒇𝒇 𝒏𝒏 = 𝑶𝑶 𝒈𝒈 𝒏𝒏 𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘𝒘 𝒏𝒏 ∈ ℝ as 𝒏𝒏 → ∞

• Let M be a constant that’s sufficiently large then we can say

𝒇𝒇 𝒏𝒏 ≤ 𝑴𝑴 𝒈𝒈 𝒏𝒏 𝒇𝒇𝒇𝒇𝒘𝒘 𝒂𝒂𝒂𝒂𝒂𝒂 𝒏𝒏 ≥ 𝒏𝒏𝟎𝟎

Page 6: Searching, Sorting, Complexity Part 02

• Common Big O Complexities– O(1) – Constant– O(log(n)) – Logarithmic– O(n) – Linear– O(nlogn) – Linearithmic– O(n2) – Quadratic– O(2n) – Exponential “Bad”– O(n!) – Factorial “Really Bad”

Big O

Page 7: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 8: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 9: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 10: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 11: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 12: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 13: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 14: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 15: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 16: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 17: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 18: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 19: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Smallest

Start

Page 20: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 8 7 6 12 10 11 9

Smallest

Start

Page 21: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 8 7 6 12 10 11 9

Smallest

Start

Page 22: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 8 7 6 12 10 11 9

Smallest

Start

Page 23: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 8 7 6 12 10 11 9

Smallest

Start

Page 24: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 8 7 6 12 10 11 9

Smallest

Start

Page 25: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 12 10 11 9

Smallest

Start

Page 26: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 12 10 11 9

Smallest

Start

Page 27: Searching, Sorting, Complexity Part 02

A Few Swaps Later

Page 28: Searching, Sorting, Complexity Part 02

• Problem:– Given any array of integers, develop an algorithm

that sorts the values from smallest to largest.• Selection Sort

1. Start from index 02. Assume the starting index has the smallest

value and record that index3. Sequentially check every other value4. If a value is found that is smaller at another

index, then record that current index5. Once all values have been checked if the

recorded index does not match the current index then swap those values

6. Increase the starting index by 17. Repeat 2 through 6 until the staring index >=

length

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Page 29: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Search for smallest value = n– Search for the next smallest value = n – 1– Search for the next smallest value = n – 2– …– Search for the largest element = 1

Complexity

Page 30: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Search for smallest value = n– Search for the next smallest value = n – 1– Search for the next smallest value = n – 2– …– Search for the largest element = 1

Complexity

O(n2)

Page 31: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 32: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 33: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 34: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

Page 35: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

Page 36: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

Page 37: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 10 6 12 5 11 9

Page 38: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 10 6 12 5 11 9

Page 39: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

Page 40: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

Page 41: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

Page 42: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 12 11 9

Page 43: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 12 11 9

Page 44: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 11 12 9

Page 45: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 11 12 9

Page 46: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 11 9 12

Page 47: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 5 11 9 12

Page 48: Searching, Sorting, Complexity Part 02

A Few Swaps Later

Page 49: Searching, Sorting, Complexity Part 02

• Bubble Sort1. Start from index 02. Check each index with its neighbor

(index+1)3. If that neighbor’s value is smaller then swap

with the current index’s value4. Repeat step 1 until no swaps have been

made

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Page 50: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Bubble Up Largest Value = n– Bubble Up Next Largest Value = n – 1– Bubble Up Next Largest Value = n – 2– …– Smallest Value = 1

Complexity

Page 51: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Bubble Up Largest Value = n– Bubble Up Next Largest Value = n – 1– Bubble Up Next Largest Value = n – 2– …– Smallest Value = 1

Complexity

O(n2)

Page 52: Searching, Sorting, Complexity Part 02

Can we do better?

Page 53: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 54: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3 4 5 6 7

10 8 7 6 12 5 11 9

Page 55: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3 4 5 6 7

10 8 7 6 12 5 11 9

0 1 2 3

10 8 7 6

0 1 2 3

12 5 11 9

Page 56: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

10 8 7 6

0 1 2 3

12 5 11 9

0 1

10 8

0 1

7 6

0 1

12 5

0 1

11 9

Page 57: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

10 8

0 1

7 6

0 1

12 5

0 1

11 9

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

Page 58: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

Page 59: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

i j

k

Page 60: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

8

i

k

Page 61: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

8 10

Page 62: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

8 10

0 1

i j

k

Page 63: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

8 10

0 1

6 7

Page 64: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0

10

0

8

0

7

0

6

0

12

0

5

0

11

0

9

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

Page 65: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

Page 66: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

i j

0 1 2 3

k

Page 67: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

i j

0 1 2 3

6

k

Page 68: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

i

0 1 2 3

6 7

k

Page 69: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

Page 70: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

0 1 2 3

i j

k

Page 71: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

0 1 2 3

5

i j

k

Page 72: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

0 1 2 3

5 9

i j

k

Page 73: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11

i

k

Page 74: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1

8 10

0 1

6 7

0 1

5 12

0 1

9 11

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

Page 75: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

Page 76: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

i j

k

Page 77: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5

i j

k

Page 78: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6

i j

k

Page 79: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6 7

i j

k

Page 80: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6 7 8

i j

k

Page 81: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6 7 8 9

i j

k

Page 82: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6 7 8 9 10

j

k

Page 83: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3

6 7 8 10

0 1 2 3

5 9 11 12

0 1 2 3 4 5 6 7

5 6 7 8 9 10 11 12

Page 84: Searching, Sorting, Complexity Part 02

• Merge Sort1. Recursively split the array in half until single

elements remain2. Merge two smaller arrays and return the

sorted result1. Create an array of combined size2. Add elements from the two smaller arrays into

the combined array in sorted order

3. Repeat Step 2 until the final array is reached

Example

0 1 2 3 4 5 6 7

5 6 7 8 9 10 11 12

Page 85: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Split– Merge

Complexity Visual

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

1 1

Page 86: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Split– Merge

Complexity Visual

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

1 1

lg(n)

Page 87: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Split– Merge

Complexity Visual

n

n/2 n/2

n/4 n/4 n/4 n/4

n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8

1 1

lg(n)

n

Page 88: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Descending Order

• Operations– Split– Merge

Complexity

O(nlg(n))

Page 89: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

Page 90: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

i

j

pivot

Page 91: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

i

j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 92: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 10 8 7 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 93: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 94: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

i

j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 95: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 96: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 10 7 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 97: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 10 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 98: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 10 6 12 5 11 9

i

j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 99: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 10 6 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 100: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 101: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

i

j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 102: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 103: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 104: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 10 12 5 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 105: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 106: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 107: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 108: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i j

pivot

If value at j is smaller than the pivot then swap values at i and j and increase i by 1

Page 109: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i

pivot

Swap values at i and the pivot

Page 110: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 12 10 11 9

i

pivot

Swap values at i and the pivot

Page 111: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Swap values at i and the pivot

Page 112: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

Page 113: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

j

pivotpivot

i

j

Page 114: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

j

pivotpivot

i

j

Page 115: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

j

pivotpivot

i

j

Page 116: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 8 7 6 5 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

j

pivotpivot

i

j

Page 117: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 118: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

jj

pivot

Page 119: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

jj

pivot

Page 120: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

jj

pivot

Page 121: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 122: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

jj

pivot

Page 123: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 7 6 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

j

pivot

Page 124: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 125: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

jj

pivot

Page 126: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 127: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 128: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

pivot

i

j

Page 129: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

i

Page 130: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

i

j

pivot

Page 131: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

i

j

pivot

Page 132: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

i

Page 133: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

i

j

pivot

Page 134: Searching, Sorting, Complexity Part 02

• Quick Sort1. Pick an arbitrary value called a “pivot” from

the array2. Using the pivot value “partition” the array

1. Reorder the array where smaller values are to the left of the pivot, and large / equal values are to the right

2. Once it has been partitioned the pivot value is where it should be

3. Recursively continue now with the array to the left, and the array to the right of the pivot

Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Recursively do the same for the values to the left of the partition and to the right of the

partition

Page 135: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

Page 136: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 137: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 138: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 139: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 140: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 141: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 142: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 143: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 144: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 145: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 146: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

pivot

Page 147: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity Example

Index 0 1 2 3 4 5 6 7

Value 5 6 7 8 9 10 11 12

i

j

Page 148: Searching, Sorting, Complexity Part 02

• Worst Case– Sorted in Ascending Order– Assuming pivot is always picked from the last

index

• Operations– The first index moves n spaces– The first index moves n-1 spaces– The first index moves n-2 spaces– …– The first index moves 1 space

Complexity

O(n2)

Page 149: Searching, Sorting, Complexity Part 02

Merge Sort• Worst Time Complexity = O(nlg(n))• Average Time Complexity = Ⲑ(nlg(n))• Worst Space Complexity = O(n) additional

Quick Sort• Worst Time Complexity = O(n2)• Average Time Complexity = Ⲑ(nlg(n))• Worst Space Complexity = O(lg(n)) additional