Intersection Study - Algorithm(Sort)

66
ALGORITHM

Transcript of Intersection Study - Algorithm(Sort)

ALGORITHM

ALGORITHMSORT

Selection Sort Quick Sort

ALGORITHM

SORT INTRO

What is Sort?

ALGORITHM

SORT INTRO

Make data in Sequence

4 3 8 6 7 13 10 14 9

3 4 6 7 8 9 10 13 14

ALGORITHM

SORT INTRO

Make data in Sequence

Key(comparing data)

Sort Order(Ascending, Descending)

Sort Efficiency

(time complexity)

SortStability(keep sort order)

ALGORITHM

SORT Selection Sort

Selection Sort

ALGORITHM

SORT Selection Sort

select best-fit-data every time

s s d d .. d d d d

sorted unsorted

select data fits this position

ALGORITHM

SORT Selection Sort

4 3 8 6 7 13 10 14 9

Selection Sort in Ascend order

Min Data

unsorted

ALGORITHM

SORT Selection Sort

3 4 8 6 7 13 10 14 9

Selection Sort in Ascend order

unsorted

swap

sorted

ALGORITHM

SORT Selection Sort

3 4 8 6 7 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 8 6 7 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

ALGORITHM

SORT Selection Sort

3 4 8 6 7 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 8 7 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

swap

ALGORITHM

SORT Selection Sort

3 4 6 8 7 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 7 8 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

swap

ALGORITHM

SORT Selection Sort

3 4 6 7 8 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 7 8 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

ALGORITHM

SORT Selection Sort

3 4 6 7 8 13 10 14 9

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 14 13

Selection Sort in Ascend order

unsortedsorted

swap

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 14 13

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 14 13

Selection Sort in Ascend order

unsortedsorted

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 14 13

Selection Sort in Ascend order

unsortedsorted

Min Data

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 13 14

Selection Sort in Ascend order

unsortedsorted

swap

ALGORITHM

SORT Selection Sort

3 4 6 7 8 9 10 13 14

Selection Sort in Ascend order

sorted

ALGORITHM

SORT Quick Sort

Quick Sort

divide dataset with pivot

s … p ... d d d d d

less than p bigger than p

pivot

ALGORITHM

SORT Quick Sort

select pivot

ALGORITHM

SORT Quick Sort

4 3 8 6 7 13 2 14 9

pivot

left right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

4 3 8 6 7 13 2 14 9

pivot

left right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

4 3 8 6 7 13 2 14 9

pivot

left right

swap left and right

ALGORITHM

SORT Quick Sort

4 3 2 6 7 13 8 14 9

pivot

left right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

4 3 2 6 7 13 8 14 9

pivot

left right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

4 3 2 6 7 13 8 14 9

pivot

left

right

swap right with pivot

ALGORITHM

SORT Quick Sort

2 3 4 6 7 13 8 14 9

pivot

swap right with pivot

ALGORITHM

SORT Quick Sort

2 3 4 6 7 13 8 14 9

pivot

less than 4 bigger than 4

sorting problem divided into two problems

ALGORITHM

SORT Quick Sort

2 3 4 6 7 13 8 14 9

less than 4 bigger than 4

sort left partition

ALGORITHM

SORT Quick Sort

2 3

pivot

left

right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

2 3

pivot

left

right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

2 3

pivot

left

right

sorted

ALGORITHM

SORT Quick Sort

2 3

try right part

ALGORITHM

SORT Quick Sort

2 3 4 6 7 13 8 14 9

sorted bigger than 4

sort right partition

ALGORITHM

SORT Quick Sort

6 7 13 8 14 9

pivot

left

right

ALGORITHM

SORT Quick Sort

6 7 13 8 14 9

pivot

left

right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

6 7 13 8 14 9

pivot

left

right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

sorting problem divided into two problems but left is none

6 7 13 8 14 9…

bigger than 6

ALGORITHM

SORT Quick Sort

sort right partition

7 13 8 14 9

pivot

left

right

ALGORITHM

SORT Quick Sort

7 13 8 14 9

pivot

left

right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

7 13 8 14 9

pivot

left

right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

7 13 8 14 9…

bigger than 7

sorting problem divided into two problems but left is none

ALGORITHM

SORT Quick Sort

13 8 14 9

sort right partition

pivot

left

right

ALGORITHM

SORT Quick Sort

13 8 14 9

pivot

left

right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

13 8 14 9

pivot

left

Move right to first data less than pivot

right

ALGORITHM

SORT Quick Sort

13 8 9 14

pivot

left

right

swap left and right

ALGORITHM

SORT Quick Sort

9 8 13 14

pivot

swap pivot and right

right

ALGORITHM

SORT Quick Sort

9 8 13 14

less than 13 bigger than 13

sorting problem divided into two problems

ALGORITHM

SORT Quick Sort

9 8 13 14

less than 13 sorted

no need to sort one element

ALGORITHM

SORT Quick Sort

9 8

sort left partition

pivot

left

right

ALGORITHM

SORT Quick Sort

9 8

pivot

left

right

Move left to first data bigger than pivot

ALGORITHM

SORT Quick Sort

9 8

pivot

left

right

Move right to first data less than pivot

ALGORITHM

SORT Quick Sort

8

Swap left and right

9

pivot

ALGORITHM

SORT Quick Sort

8

sorted

9

ALGORITHM

SORT Quick Sort

8

sorted

9

ALGORITHM

SORT Quick Sort

8 9 13 14

less than 13 sorted

sorted

ALGORITHM

SORT Quick Sort

sorted

7 8 9 13 14

bigger than 7

ALGORITHM

SORT Quick Sort

sorted

6 7 8 9 13 14

bigger than 6

sorted

ALGORITHM

SORT Quick Sort

2 3 4 6 7 8 9 13 14

sorted bigger than 4

sort complete

ALGORITHM

SORT Quick Sort

2 3 4 6 7 8 9 13 14

sorted