csed101 introduction to computing sorting 1

22
CSED101 INTRODUCTION TO COMPUTING SORTING 1 오오오 Jinoh Oh

Upload: sasha

Post on 11-Feb-2016

17 views

Category:

Documents


0 download

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

Page 1: CSED101 Introduction to Computing Sorting 1

CSED101 INTRODUCTION TO COMPUTINGSORTING 1

오진오Jinoh Oh

Page 2: 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

– 2

Page 3: CSED101 Introduction to Computing Sorting 1

Selection Sort– 3

6 3 5 1 2 4 8 7 9

Page 4: CSED101 Introduction to Computing Sorting 1

Selection Sort– 4

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

Page 5: CSED101 Introduction to Computing Sorting 1

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

Page 6: CSED101 Introduction to Computing Sorting 1

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

Page 7: CSED101 Introduction to Computing Sorting 1

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

Page 8: CSED101 Introduction to Computing Sorting 1

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

Page 9: CSED101 Introduction to Computing Sorting 1

Find_min Function that find smallest element from

given list. Returns min element and remain list.

– 9

Page 10: CSED101 Introduction to Computing Sorting 1

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

Page 11: CSED101 Introduction to Computing Sorting 1

Selection Sort Function process

– 11

Page 12: CSED101 Introduction to Computing Sorting 1

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

– 12

Page 13: CSED101 Introduction to Computing Sorting 1

Insertion Sort– 1

3

6 3 5 1 2 4 8 7 9

Page 14: CSED101 Introduction to Computing Sorting 1

Insertion Sort– 1

4

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

Page 15: CSED101 Introduction to Computing Sorting 1

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

Page 16: CSED101 Introduction to Computing Sorting 1

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

Page 17: CSED101 Introduction to Computing Sorting 1

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

Page 18: CSED101 Introduction to Computing Sorting 1

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

Page 19: CSED101 Introduction to Computing Sorting 1

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

Page 20: CSED101 Introduction to Computing Sorting 1

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

else x::h::t ;;

– 20

Page 21: CSED101 Introduction to Computing Sorting 1

– 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]

Page 22: CSED101 Introduction to Computing Sorting 1

– 22

Insertion Sort let rec insertion_sort l =

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