????????????????? (Sorting) - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

????????????????? (Sorting)

Description:

COSC2202 (Data Structure) (Sorting) – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 33
Provided by: dearnanFi
Category:
Tags: merge | sort | sorting

less

Transcript and Presenter's Notes

Title: ????????????????? (Sorting)


1
???? COSC2202 ??????????????? (Data Structure)
  • ????????????????? (Sorting)

2
????????????????? (Sorting)
  • ???????????????? (Sorting) ???????
    ??????????????????????????????????????????????????
    ?? ??????????????????????? ?????????????????????
    ???????????????????????????????????? ?????????? 2
    ?????????? ? ???
  • 1. ???????????????????????? (Internal sorting)
  • ??????????????????????????????????????????????????
    ???? (main memory)
  • ?????????????????????????? ???? ?????, ???
    ???????
  • 2. ????????????????????????? (External sorting)
  • ??????????????????????????????????????????????????
    ?????????????????????????????
  • ?????????????????????? ???? ?????, ???
    ??????????????????????????????????????????????????
    ????? ????????????????????????????????????????????

3
????????????????????????????
Sort Order1. Ascending Order ???????????? --gt
???2. Descending Order ??????????? --gt ????
???????????????????????????
?????????????????????????????
????????
4
Insertion Sort
  • ?????????????????? ??? ???????????????????????????
    ????????????????????? ?????????????????
    ??????????????????????????????????????????????????
    ????????????????????????????????? ?
    ?????????????????

5
Insertion Sort
  • ???????
  • ????????????????????????????????
  • ?????????????????????????????????????

6
Shell Sort
  • ??????????????????? ??????????????????????????????
    ????????????????????????????????? ?????
    ?????????????????????????????? ????
    ????????????????????????????? ??????????????????
    ???????????????????????????????????????????????

7
Shell Sort
  • ???????? ????? Segment ????? K3

8
Shell Sort
  • ???????
  • ???????????????????? K ???????????????????????????
    ????????????????????? ???? ???????? 10 ??? K
    n/2 10/2 5
  • ????????????????????????????? ???????????? K ????
    (??????????????????????????????? K ?????? ???? K1
    5 K2 5/2 2)
  • ??? K gt 1 ???????? ??????????????????????????????
    ??? K 1 ????????????????????

9
Shell Sort
????????????????????????????? K
10
Shell Sort
11
Shell Sort
12
Selection Sort
  • ??????????????????? ??????????????????????????????
    ??????????????????????????????????????????????????
    ??????? ??????????????????????????
    ??????????????????????????????????????????????????
    ????????????????????????

13
Selection Sort
  • ???????
  • ???????????????????????/??????????????????????????
    ?????????????
  • ?????????????????????????????/?????????

14
Heap Sort
15
Heap Sort
16
Bubble Sort
  • ????????????????????? ????????????????????????????
    ??????? 2 ???????????? ???????????????????????

17
Bubble Sort
18
Quick Sort
  • ???????????????????? ??????????
    divide-and-conquer ???????????????????????????????
    ???? 2 ????? ?????????????????????????????????????
    ????????????????????????? ????????????????????????
    ?????????????????????????????????????
    ????????????????????????????????????? 2
    ???????????? ?????????????????????????????????????
    ??????????????????

19
Quick Sort
  • ???????
  • ???????????????????????????????? Pivot
    ????????????????????????????????????????????????
    ??? ?????????????????????? Pivot
    ???????????????????????? Pivot
  • ???????????????????
  • ?????????????????????????

20
Quick Sort
  • ??????????????????????????? Picking the pivot
    ??????????????????????????????????
    ??????????????????????????????????????????????
    ???????????????????? ??????? ?????????????????????
    ??????????????????????????????????????????
    ???????????????????????????????????

21
Quick Sort
????????? lt Pivot ???? SortLeftSortLeft1
????????? gt Pivot ???? SortRightSortRight-1
???? Pivot ??? ???????????? SortLeft-1
22
Quick Sort
23
Merge Sort
  • ??????????????? (Merge Sort ) -- ????? Merge Sort
    ?????????? divide-and-conquer ????????? Quick
    Sort ????????????????????????????????????
    ????????????????????????????????????????? Quick
    sort Quick sort ?????????????????????????????????
    ???????????????????? ??? merge sort ???
    ??????????????????????????????????
    ????????????????????????????? ????????????????????
    ????????????????????? ????????????????????????????
    ???? ???????????????????????????????????
    ??????????????????????????????????????????????????
    ???????????????

24
Merge Sort

???????????????????????????? ??????????????????
????????????? ??????????????????????????????
25
Radix Sort
  • ??????????????????? -- ???????????????????????????
    ?????????????????
  • 1. ?????????????????????????????????????????
    ??????????????????????????????????????????????????
    ????
  • 2. ????????????????????????????
    ?????????????????????????????????????????
    ?????????? ?????????????????
  • 3. ????????????????????????????????????
    ?????????????????????????????????????
    ??????????????????????????????????????????????????
    ?????????? ?????????????
  • 4. ???????????????????????????????????????????????
    ???????????????????????????????????????????????
    ??????????? ???????????????????

26
Radix Sort
???????????? Linked List
27
?????????????????
  • Big-O
  • ???????????????????????????????????????????
    ?????????????????????????????????????????????

28
?????????????????
void swap(int data, int a, int b) int
temp temp dataa dataa
datab datab temp / swap /
  • void insertion_sort(int data, int num_elts)
  • int i, j
  • for (i0 iltnum_elts i)
  • for (ji (jgt0) (dataj lt dataj-1)
    j--)
  • swap(data, j, j-1)
  • / insertion_sort /

Big-O n2
29
?????????????????
void swap(int data, int a, int b) int
temp temp dataa dataa
datab datab temp / swap /
  • void selection_sort(int data, int num_elts)
  • int i, j, lowindex
  • for (i0 iltnum_elts i) / Select i'th record
    /
  • lowindex i / Remember its index
    /
  • for (jnum_elts-1 jgti j--) / Find the least
    value /
  • if (dataj lt datalowindex)
  • lowindex j / Put it in place /
  • if (i ! lowindex) / To reduce
    swap() operation /
  • swap(data, i, lowindex)
  • / selection_sort /

Big-O n2
30
?????????????????
  • void bubble_sort(int data, int num_elts)
  • int i, j
  • for (i0 iltnum_elts i)
  • for (jnum_elts-1 jgti j--)
  • if (dataj lt dataj-1)
  • swap(data, j, j-1)
  • / bubble_sort /

void swap(int data, int a, int b) int
temp temp dataa dataa
datab datab temp / swap /
Big-O n2
31
?????????????????
k, the size of each key s, the chunk size used
by the implementation.
least significant digit (LSD) most significant
digit (MSD)
32
?????????????????
Write a Comment
User Comments (0)
About PowerShow.com