# Lecture 28: Sorting

### COSC 273: Parallel and Distributed Computing

### Spring 2023

## This Week

- Homework 03 Due Today
- Final Project group & topic selection, also due Today
- Option 1: computing prime numbers
- Option 2: sorting
- Option 3: choose your own adventure

## The Task

**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)`

## Sorting by Divide-and-Conquer

General Strategy:

- break problem up into smaller sub-tasks
- solve 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)

- Split array into
`left`

and `right`

sub-arrays
- Sort
`left`

and `right`

- Merge sorted
`left`

and right

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

## Parallelism?

**Question.** What can be performed in parallel?

- Split array into
`left`

and `right`

sub-arrays
- Sort
`left`

and `right`

- Merge sorted
`left`

and right

## Strategy 2: Divide by *Value*

**Idea.** (quick sort)

- Select a value
`pivot`

from array
- Partition array into
`left`

and `right`

sub-arrays
- values in
`left`

are `<= pivot`

, values in `right`

are `>`

- Sort
`left`

and `right`

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

## Parallelism?

**Question.** What can be performed in parallel?

- Select a value
`pivot`

from array
- Partition array into
`left`

and `right`

sub-arrays
- values in
`left`

are `<= pivot`

, values in `right`

are `>`

- Sort
`left`

and `right`

## (Dis)advantages?

**Merge sort:** split, sub-sort, merge

**Quick sort:** partition, sort

## When to Stop Recursing?

**Merge 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

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