csed101 introduction to computing sorting 1

Post on 11-Feb-2016

17 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

오진오 Jinoh Oh. CSED101 Introduction to Computing Sorting 1. Sorting. Ascending sort 1, 2, 3, 4, … Descending sort 10, 9, 8, … Greedy approach Selection sort Insertion sort Divide-and-conquer approach Merge sort Quick sort. Selection Sort. 6. 3. 5. 1. 2. 4. 8. 7. 9. - PowerPoint PPT Presentation

TRANSCRIPT

CSED101 INTRODUCTION TO COMPUTINGSORTING 1

오진오Jinoh Oh

Sorting Ascending sort

1, 2, 3, 4, … Descending sort

10, 9, 8, … Greedy approach

Selection sort Insertion sort

Divide-and-conquer approach Merge sort Quick sort

– 2

Selection Sort– 3

6 3 5 1 2 4 8 7 9

Selection Sort– 4

6 3 5 1 2 4 8 7 96 3 51 2 4 8 7 9

Selection Sort– 5

6 3 5 1 2 4 8 7 96 3 51 2 4 8 7 9

6 3 51 2 4 8 7 9

Selection Sort– 6

6 3 5 1 2 4 8 7 96 3 51 2 4 8 7 9

6 3 51 2 4 8 7 963 51 2 4 8 7 9

Selection Sort– 7

6 3 5 1 2 4 8 7 96 3 51 2 4 8 7 9

6 3 51 2 4 8 7 963 51 2 4 8 7 9

63 51 2 4 8 7 9

Sorting – greedy approach – selec-tion sort Input: a list of numbers (source list), x1,

x2, …, xn Output: a list of sorted numbers (target

list) Procedure

Pick the smallest number from the source list

Put it to the end of the target list.

Repeat this until the source list becomes empty.

– 8

Find_min Function that find smallest element from

given list. Returns min element and remain list.

– 9

Find_minlet rec find_ min l = match l with | h::[] -> (h,[]) | h::t -> let (m, l') = find_min t in if h < m then (h, t) else (m, h::l');;

– 10

Selection Sort Function process

– 11

Selection Sort let rec selection_sort l = match l with | [] -> [] | _ -> let (m, l') = find_min l in m :: (selection l');;

– 12

Insertion Sort– 1

3

6 3 5 1 2 4 8 7 9

Insertion Sort– 1

4

6 3 5 1 2 4 8 7 96 3 5 1 2 4 8 7 9

Insertion Sort– 1

5

6 3 5 1 2 4 8 7 96 3 5 1 2 4 8 7 9

63 5 1 2 4 8 7 9

Insertion Sort– 1

6

6 3 5 1 2 4 8 7 96 3 5 1 2 4 8 7 9

63 5 1 2 4 8 7 963 5 1 2 4 8 7 9

Insertion Sort– 1

7

6 3 5 1 2 4 8 7 96 3 5 1 2 4 8 7 9

63 5 1 2 4 8 7 963 5 1 2 4 8 7 9

63 51 2 4 8 7 9

Sorting – greedy approach – inser-tion sort Input: a list of numbers (source list), x1,

x2, …, xn Output: a list of sorted numbers (target

list) Procedure

Pick the first element from the source list Put it to the proper position of the target

list.

Repeat this until the source list becomes empty.

– 18

insert Function to put a element to the proper posi-

tion of the target list.

Ex) insert 5 [1; 3; 4; 6; 7; 9];; - : int list = [1; 3; 4; 5; 6; 7; 9]

– 19

insertlet rec insert x inlist = match inlist with | [] -> [x] | h::t -> if h < x then h :: insert x t

else x::h::t ;;

– 20

– 21

Insertion Sort Process insertion sort using the insert func-

tion.

Ex) insertion [3; 9; 4; 6; 7; 1];; - : int list = [1; 3; 4; 6; 7; 9]

– 22

Insertion Sort let rec insertion_sort l =

match l with [] -> [] | h::t -> insert h (insertion_sort t);;

top related