# Lecture 28: Sorting

## This Week

• Homework 03 Due Today
• Final Project group & topic selection, also due Today
• Option 1: computing prime numbers
• Option 2: sorting

# Sorting

Input. A (large) array float[] values

Task. Modify values so that the values are increasing.

Scale. values.length = 1_048_576 ($= 2^{20}$)

Baseline. Arrays.sort(values)

• include java.util.Arrays

## Sorting by Divide-and-Conquer

General Strategy:

• break problem up into smaller sub-tasks
• combine sub-solutions to get total solution

Question 1. How to divide sorting problem?

Question 2. How might parallelism help?

## Strategy 1: Divide by Index

Idea. (merge sort)

1. Split array into left and right sub-arrays
2. Sort left and right
3. Merge sorted left and right

Example. Sort [4, 2, 3, 1]

## Parallelism?

Question. What can be performed in parallel?

1. Split array into left and right sub-arrays
2. Sort left and right
3. Merge sorted left and right

## Strategy 2: Divide by Value

Idea. (quick sort)

1. Select a value pivot from array
2. Partition array into left and right sub-arrays
• values in left are <= pivot, values in right are >
3. Sort left and right

Example. Sort [4, 2, 3, 1]

## Parallelism?

Question. What can be performed in parallel?

1. Select a value pivot from array
2. Partition array into left and right sub-arrays
• values in left are <= pivot, values in right are >
3. Sort left and right

Merge sort: split, sub-sort, merge

Quick sort: partition, sort

Merge Sort?

Quick Sort?

## Merge Operation

Question. How to merge two sorted arrays?

## Speeding Up Merge?

Question. Can we improve performance of merge operation with SIMD instructions?

## Partition Operation?

Question. How to partition an array given a pivot?

## Speeding Up Partition?

Question. Can we improve performance of partition with SIMD instructions?

## Next Week

1. Fork-join pools: thread pools tailored to recursive methods!
2. Sorting networks: the art of sorting small arrays