Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except...

21
Insertion Sort Speaker: David Walker COS 326 Princeton University slides copyright 2020 David Walker and Andrew Appel permission granted to reuse these slides for non-commercial educational purposes

Transcript of Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except...

Page 1: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

Speaker: David WalkerCOS 326

Princeton University

slides copyright 2020 David Walker and Andrew Appelpermission granted to reuse these slides for non-commercial educational purposes

Page 2: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Recall Insertion Sort

At any point during the insertion sort:– some initial segment of the array will be sorted– the rest of the array will be in the same (unsorted) order as it

was originally

-5 -2 3 -4 10 6 7

sorted unsorted

2

Page 3: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Recall Insertion Sort

At any point during the insertion sort:– some initial segment of the array will be sorted– the rest of the array will be in the same (unsorted) order as it

was originally

At each step, take the next item in the array and insert it in order into the sorted portion of the list

-5 -2 3 -4 10 6 7

sorted unsorted

-5 -4 -2 3 10 6 7

sorted unsorted

3

Page 4: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort With ListsThe algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

We'll factor the algorithm:– a function to insert into a sorted list– a sorting function that repeatedly inserts

-5 -2 3 -4 10 6 7

sorted unsorted

list 1: list 2:

4

Page 5: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insert

(* insert x in to sorted list xs *)

let rec insert (x : int) (xs : int list) : int list =

5

Page 6: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insert

(* insert x in to sorted list xs *)

let rec insert (x : int) (xs : int list) : int list =match xs with| [] -> | hd :: tl ->

a familiar pattern: analyze the list by cases

6

Page 7: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insert

(* insert x in to sorted list xs *)

let rec insert (x : int) (xs : int list) : int list =match xs with| [] -> [x]| hd :: tl -> insert x into the

empty list

7

Page 8: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insert

(* insert x in to sorted list xs *)

let rec insert (x : int) (xs : int list) : int list =match xs with| [] -> [x]| hd :: tl ->

if hd < x thenhd :: insert x tl

build a new list with:• hd at the beginning• the result of inserting x in to

the tail of the list afterwards

8

Page 9: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insert

(* insert x in to sorted list xs *)

let rec insert (x : int) (xs : int list) : int list =match xs with| [] -> [x]| hd :: tl ->

if hd < x thenhd :: insert x tl

else x :: xs

put x on the front of the list, the rest of the list follows

9

Page 10: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

A Common ParadigmSome functions over inductive data do their work like this:• step 1: set up initial conditions• step 2: iterate/recurse over the data

10

Page 11: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

A Common ParadigmSome functions over inductive data do their work like this:• step 1: set up initial conditions• step 2: iterate/recurse over the data

How that looks:

11

let f x y =let rec loop z =

… loop z …inlet z = setup x y inloop z

recursive loop

set up

Page 12: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

12

Page 13: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

let rec loop (sorted : il) (unsorted : il) : il =

in

13

Page 14: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

let rec loop (sorted : il) (unsorted : il) : il =

inloop [] xs

14

Page 15: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> | hd :: tl ->

inloop [] xs

15

Page 16: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> sorted| hd :: tl ->

inloop [] xs

16

Page 17: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Insertion Sort

type il = int list

insert : int -> il -> il

(* insertion sort *)

let rec insert_sort(xs : il) : il =

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> sorted| hd :: tl -> loop (insert hd sorted) tl

inloop [] xs

17

Page 18: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Does Insertion Sort Terminate?Recall that we said: inductive functions should call themselves recursively on smaller data items.

What about that loop in insertion sort?

18

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> sorted| hd :: tl -> loop (insert hd sorted) tl

Page 19: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Does Insertion Sort Terminate?Recall that we said: inductive functions should call themselves recursively on smaller data items.

What about that loop in insertion sort?

19

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> sorted| hd :: tl -> loop (insert hd sorted) tl

growing! shrinking!

Page 20: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Does Insertion Sort Terminate?Recall that we said: inductive functions should call themselves recursively on smaller data items.

What about that loop in insertion sort?

Refined idea: Pick an argument up front. That argument mustcontain smaller data on every recursive call.

20

let rec loop (sorted : il) (unsorted : il) : il =match unsorted with| [] -> sorted| hd :: tl -> loop (insert hd sorted) tl

growing! shrinking!

Page 21: Insertion Sort - cs.princeton.edu · Insertion Sort With Lists The algorithm is similar, except instead of one array, we will maintain two lists, a sorted list and an unsorted list

Exercises• Write a function to sum the elements of a list

– sum [1; 2; 3] ==> 6• Write a function to append two lists

– append [1;2;3] [4;5;6] ==> [1;2;3;4;5;6]• Write a function to reverse a list

– rev [1;2;3] ==> [3;2;1]• Write a function to turn a list of pairs into a pair of lists

– split [(1,2); (3,4); (5,6)] ==> ([1;3;5], [2;4;6])• Write a function that returns all prefixes of a list

– prefixes [1;2;3] ==> [[]; [1]; [1;2]; [1;2;3]]• suffixes…

21