Lecture 13 Ierg 4120

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

13 - Typical Algorithms in Functional Style

Kehuan Zhang ©

IERG4120 - Functional Programming

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 1 / 16


What we will cover?

How to implement five different sort algorithms in functional style


▶ insert sort
▶ bubble sort
▶ merge sort
▶ quick sort
▶ selection sort
Common algorithms over trees
▶ search, counting, pre-oder traversal
Goal: how to use functional programming language to implement
typical algorithms

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 2 / 16


Insert Sort
How it works → Animation of a simple example from Wikipedia
1 insert the first element into a gradually sorted list (initial empty)
2 repeat until all elements are inserted properly
Core idea to impelment insert sort in OCaml
▶ We use list instead of array, as list is more close to functional style
▶ using recursive call instead of for-/while-loops
let rec insert_sort lst =
(* insert n into ll which is sorted already *)
let rec insert n ll =
match ll with
| [] -> [n]
| hd::tl -> if n < hd then n::ll
else hd::(insert n tl) in
match lst with
| [] -> []
| hd::tl -> insert hd (insert_sort tl);;
Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 3 / 16
Bubble Sort

How it works → Animation from Wikipedia


(1) starting from the first element, compare two adjacent elemtns, and
shift the larger one to right
(2) repeat until the list will not change

let rec bubble_sort lst =


let sorted = match lst with
| [] -> []
| hd1 :: hd2 :: tl when hd1 > hd2 ->
hd2 :: bubble_sort (hd1 :: tl)
| hd::tl -> hd :: bubble_sort tl
in
if lst = sorted then lst
else bubble_sort sorted;;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 4 / 16


Merge Sort 1 - Split

How it works → Animation from Wikipedia


1 split the list into two sub-lists
2 sort each sub-lists (using merge sort algorithm recursively)
3 merge the two sorted sub-lists
Two operations: split and merge

let split lst =


let rec split_aux ll left right =
match ll,left,right with
| ([] | [_]),_,_ -> (List.rev left),right
| (_::_::t),_,h::right_t ->
split_aux t (h::left) right_t
| _ -> assert false in
split_aux lst [] lst;;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 5 / 16


Merget Sort 2 - Merge two sorted List
the merge function will combine two sorted lists into a single one
let rec merge l1 l2 =
match l1,l2 with
| [],l | l,[] -> l
| h1::t1,h2::t2 when h1 < h2 -> h1::(merge t1 l2)
| _, h2::t2 -> h2::(merge l1 t2);;
the final merge_sort function will integrate above two operations
together and call itself recursively
let rec merge_sort lst =
match lst with
| [] -> []
| hd::[] -> [hd]
| _ -> let l1, l2 = split lst in
merge (merge_sort l1) (merge_sort l2);;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 6 / 16


Quick Sort (1/2)
How it works → Animation from Wikipedia, which has three steps:
1 split → split a list into two sub-lists using a pivot value
2 sort → sort each sub-list (recursively with quick-sort algorithm)
3 merge → merge the two sorted sub-lists into a single one
Note:: for simplicity, we choose the first element as the pivot value,
and it is put into the right sub-list (lst vs. tl)
let split_pivot lst =
let rec aux_split_pivot pivot left right ll =
match ll with
| [] -> Some (left, right)
| hd::tl when hd < pivot ->
aux_split_pivot pivot (hd::left) right tl
| hd::tl ->
aux_split_pivot pivot left (hd::right) tl in
match lst with
| [] -> None
| hd::tl -> aux_split_pivot hd [] [] lst;;
Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 7 / 16
Quick Sort (2/2)

Below is the code for step 2 and 3


▶ note that the two sorted sub-lists are mergeed directly with **@**
function, because it is guaranteed that all elements in left sub-list are
smaller than that in right sub-list
▶ also note that the pivot is already in the right sub-list

let rec quick_sort lst =


match lst with
| [] | [_] -> lst
| _ -> match split_pivot lst with
| None -> []
| Some (left, right) ->
(quick_sort left) @ (quick_sort right);;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 8 / 16


Selection Sort (1/2)
How it works → Animation from Wikipedia
1 take out the minimum value of the given input list
2 insert that minimum value as the first element of current input list
3 repeat above process for the remaining list (i.e., exclude the already
taken and inserted values)
two basic operations
▶ split_min → split the given list into a tuple of the minimum value
and a list of remaining elements
▶ selection_sort → perform selection sort algorithm recursively

let rec split_min (min:'a) (acc: 'a list) (lst:'a list) =


match lst with
| [] -> (Some min, acc)
| hd::tl when hd < min -> split_min hd (min::acc) tl
| hd::tl -> split_min min (hd::acc) tl;;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 9 / 16


Selection Sort (2/2)

The selection_sort function which will perform the selection sort


algorithm recursively
let rec selection_sort lst =
let min, remaining = match lst with
| [] -> (None, [])
| hd::tl -> split_min hd [] tl in
match min with
| None -> []
| Some x -> x::(selection_sort remaining);;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 10 / 16


Algorithms about Trees

Definition of Trees

type 'a tree = Leaf of 'a | Node of 'a * 'a tree * 'a tree;;

let t = Node (4, Node (2, Node (1, Leaf 90, Leaf 100),
Node (3, Leaf 20 , Leaf 30)),
Node (5, Node (6, Leaf 40, Leaf 50),
Node (7, Leaf 70, Leaf 60)));;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 11 / 16


the example tree

2 5

1 3 6 7

90 100 20 30 40 50 70 60

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 12 / 16


Algorithms about Trees (cont.)

Counting tree nodes and leaves numbers


(* How many nodes in a tree *)
let rec num_of_node = function
| Leaf _ -> 0
| Node (_, left, right) ->
1 + (num_of_node left) + (num_of_node right);;

(* how many leaves in a tree *)


let rec num_of_leaves = function
| Leaf _ -> 1
| Node (_, left, right) ->
(num_of_leaves left) + (num_of_leaves right);;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 13 / 16


Algorithms about Trees (cont.)
Search and check if a give value is in a tree or not
Note: how the problem is solved gracefully via recursive calls to
value_in_tree function over the left and right sub-trees
Try to understand the difference between imperative programming
and declarative programming
▶ imperative programming uses statements to specify the detailed
operation for each step
▶ declarative programming use expressions to specify the methods to
calculate final values

let rec is_value_in_tree value = function


| Leaf v -> v = value
| Node (v, left, right) -> (v = value) ||
(is_value_in_tree value left) ||
(is_value_in_tree value right);;

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 14 / 16


Algorithms about Trees (cont.)
trave a tree with pre-order
▶ what is pre-order?
let pre_order t =
let rec aux_pre_order acc t =
match t with
| Leaf v -> v::acc
| Node (v, left, right) ->
aux_pre_order (aux_pre_order (v::acc) left) right in
List.rev (aux_pre_order [] t);;
A tail-recursive version of pre-order traversal
let pre_order_tail tree =
let rec aux_pre_order_tail acc t = match t with
| Leaf v -> v::acc
| Node (v, left, right) -> v::(aux_pre_order_tail
(aux_pre_order_tail acc right) left)
in aux_pre_order_tail [] t;;
Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 15 / 16
Summary

In this lecture we introduced how to implement some typical


algorithms in functional style, include:
▶ various sorting algorithms, like insert sort, selection sort, quick sort,
etc.
▶ some algorithms about trees
Next week:
▶ F# → Functional programming with .NET
▶ ReasonML → Functional programming for Web application

Kehuan Zhang © 13 - Typical Algorithms in Functional Style IERG4120 - Functional Programming 16 / 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy