Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting

Description:

Title: Sorting( ) Author: WEN Last modified by: MIS Created Date: 10/15/2002 2:22:50 AM Document presentation format: Other titles – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 54
Provided by: Wen114
Category:
Tags: radix | sorting | tree

less

Transcript and Presenter's Notes

Title: Sorting


1
??
  • Sorting

2
????
  • ???????,????????
  • ?????????
  • ????????????
  • ????????

3
 ??
  • ??(Sorting)???????????????,???????????,???????????
    ???(???)??????????
  • ??????????
  • ????(Internal Sorting)??????,?????????????
  • ????(External Sorting)??????,??????????,?????????
    ???????

4
  • ?????????????
  • ?????(Insertion Sort)
  • ?????(Selection Sort)
  • ?????(Bubble Sort)
  • ?????(Shell Sort)
  • ?????(Quick Sort)
  • ?????(Merge Sort)
  • ?????(Heap Sort)
  • ?????(Radix Sort)

5
  • ???????????,?????????,??????????????????
  • ??????
  • ????????????????????
  • ???????????O(n(logn)2)?????????O(Ns),1ltslt2
  • ???????????????????????????????O(n2)?

6
  • ??????
  • ???????????????,???????,???????????,????(Decision
    Tree)???
  • ???????????????
  • ??????????????O(n2),????????????????????O(nlogn)?
  • ??????
  • ?????????,????????????O(nlogpk),
  • ?????????O(nlogpk) O(n)
  • ??p??????(Base,Radix),k???????

7
?????
  • (Insertion Sort)

8
??
  • ????????????????????????????,?????????????
  • ?????????
  • ???????????????,???????????????
  • ??????????????,?????,??????????????????

9
????
index 0 1 2 3 4 5
i 60 70 15 40 80 99 ??
60 60??
70 60 70?????index?0???
70 60 15 15??
70 60 40 15 40?????index?2???
80 70 60 40 15 80?????index?0???
99 80 70 60 40 15 99?????index?0???
10
???
  • gt?????
  • ??????
  • procedure insertionsort(int data , int n)
    index i, j int item for (i ?1
    n-1) ???datai????????????
  • ??????????? ??????????? ?????????
    ??

11
  • gtC?????
  • insertionsrot(int data , int n) index i,
    j ,item for (i1 iltn i) /?n1?/
    itemdatai
  • ji /?datai??????????,?????????/while
    (jgt0 aj-1gtitem)
  • datajdataj-1 j- -
    dataj item /?ai??????/

12
  • gt???????
  • ??????????
  • ??????????????O(n2)
  • ??????????

13
????-????
  • include ltstudio.hgt
  • void insertion_sort ( int , int )
  • void main ( )
  • int data 20
  • int size 0, i
  • printf ( \nPlease enter number to sort (enter 0
    when end)\n )
  • printf ( Number )

14
  • do
  • scanf ( d , data size )
  • while (data size ! 0 )
  • for ( i 0 i lt 60 i ) printf ( - )
  • printf ( \nSorting )
  • for ( I 0 I lt size i )
  • printf ( d , data i )

15
  • void insertion_sort (int data , int size )
  • int base, compare, temp, i
  • for (base 1 base lt size base )
  • temp data base
  • compare base
  • while ( compare gt 0 data compare 1 gt temp)

16
  • data compare data compare 1
  • compare - -
  • data compare temp
  • printf ( Access )
  • for ( I 0 i lt size i )
  • printf ( d , data i)
  • printf ( \n )

17
?????
  • (Bubble Sort)

18
??
  • ????????????????
  • ??????(Swap sort)???
  • ???????????????,???????????,???????????????,??????
    ????????????,?????????

19
????
  • ?????????????????????
  • ?????????????,????????????,????????????????????,??
    ???

20
index 0 1 2 3 4 5
i 60 70 15 40 80 99 (a) ????
70 60 15 40 80 99 (b) ?????
70 60 15 40 80 99 (c) ?????
70 60 40 15 80 99 (d) ?????
70 60 40 80 15 99 (e) ?????
70 60 40 80 99 15 (f) ?????
70 60 80 99 40 15 ?????
70 80 99 60 40 15 ?????
80 99 70 60 40 15 ?????
99 80 70 60 40 15 ?????(??)
21
???
  • gt?????
  • Bubble_sort(int a , int size)
  • for (pass size-1 to 1)
  • for ( j 1 to size 1)
  • if (a j gta j1
  • ??a j , a j1

22
  • gtC?????
  • Bubble_sort(int a , int size)
  • int i , j , t
  • for (i size-1 igt0 i- -)
  • for ( j 0 jlt1 j )
  • if (a j gta j1
  • ta j
  • a j a j1
  • a j1 t

23
  • gt???????
  • ??????????
  • ??????????????O(n2),
  • ??????????

24
????
  • include ltstdio.hgt
  • define n 10            // ??????
  • void bubble(int )       // ???????
  • void show(int )         // ????
  • void main()
  • int datan 9,1,7,3,4,8,6,2,5,0
  • printf("The default is ")
  • show(data)              // ??????
  • bubble(data)            // ???????
  • printf("The answer is ")
  • show(data)              // ????

25
  • void bubble(int data)
  • int temp,i,j,flag
  • for (in-1igt0i--)                // ??i??
  • flag 0                // ???????0?
  • for (j0jlt ij)
  • if(dataj gt dataj1)           // ?dataj gt
    dataj1????
  • printf("\ndatad gt datad",j ,j1)
  • printf(" datad,datad swap,",j ,j1)
  • temp dataj1
  • dataj1 dataj
  • dataj temp
  • flag 
  • // ????????flag

26
  • else                           // ?dataj lt
    dataj1???
  • printf("\ndatad ltdatad",j ,j1)
  • printf(" datad,datad not swap",j,j1)
  • if(flag0)               
  • //flag?0???????,?????????
  • break

27
  • printf("\nThe d time is ",-(i-n))      //
    ??????
  • show(data)                    // ????????????
  • printf("\n")
  • void show(int data)
  • int i
  • for(i0ilt ni)
  • printf("d,",(datai))

28
?????
  • (Selection Sort)

29
??
  • ???????????????(???)??,???????,??????(???)????????
    0??,???????????????????????

30
?????
31
  • ???????????,???????????????????,????????,?????????
    ?????,???????????????,??,???????????????????

32
????????
33
????
  • ?????(selection sort)??????????????????????(??????
    )?

34
index 0 1 2 3 4 5
i 60 70 15 40 99 80 ????
15 70 60 40 99 80 ?????
15 40 70 60 99 80 ?????
15 40 60 70 99 80 ?????
15 40 60 70 99 80 ?????
15 40 60 70 80 99 ?????(??)
35
???
  • gt?????
  • select_sort (int a , int size)
  • index i , j
  • for (i n 1?1)
  • ??? data 0
  • for ( j 1 ? i)
  • if ( a j gt??? )
  • ??? a j
  • ?a j ? a i ?????

36
  • gtC?????
  • select_sort(int a , int size)
  • int i, j , index, larger
  • //?1i???,???????a i ?//
  • for (i n-1 Igt0 i--)
  • larger a0
  • index 0
  • for (j1 jlti j )

37
  • if(ajgtlarger)
  • larger aj
  • index
  • aindex ai
  • ai larger

38
  • gt???????
  • ???????n-1?,???n-i?,
  • ?????????????????????????
  • ??????????????,??O(n2 )??????
  • ??????????O(n)??

39
????????
  • include ltstdio.hgt
  • define n 10
  • void selection(int )            // ?????????
  • void show(int )                 // ????
  • void main()
  • int datan 9,1,7,3,4,8,6,2,5,0
  • printf("The default is ")
  • show(data)
  • selection(data)
  • printf("The answer is ")
  • show(data)

40
  • void selection(int data)
  • int temp,i,j,flag
  • for (i0ilt ni)
  • for (jijlt nj)
  • if(datai gt dataj)         // ?datai gt
    dataj???
  • printf("\ndatad gt datad",i ,j)
  • printf(" datad,datad swap,",i ,j)
  • temp dataj
  • dataj datai
  • datai temp
  • // ?datai lt dataj????

41
  • else
  • printf("\ndatad ltdatad",i ,j)
  • printf(" datad,datad not swap",i ,j)
  • printf("\nThe d time is ",i1)
  • show(data)
  • printf("\n")

42
  • void show(int data)
  • int i
  • for(i0ilt ni)
  • printf("d,",(datai))

43
?????
  • (Quick Sort)

44
??
  • ?C. A. R. Hoare ?????????
  • ?????????????,????????
  • ??????????????????????????(pivot),
  • ??????????????????
  • ???????????????,?????????

45
????
  • ??????????
  • K(1),K(2),,K(n)
  • ???????????????????????

46
  • ??????
  • ?n1,?return
  • ????K(p)????????
  • ??????????K(i),??K(i)gtK(p)
  • ??????????K(j),??K(j)ltK(p)
  • ?iltj?K(i)?K(j)??,?????2
  • ?i?j,??K(j)?K(p)??,??j?????????????,??????????????
    15?

47
???
  • gtC?????
  • quick_sort(int a , int l, int r)
  • int p, i, j, t
  • if ( l lt r)
  • i l 1
  • j r
  • p a l

48
  • do
  • while (a i lt p)
  • i
  • while (a j gt p)
  • j - -
  • if ( i lt j )
  • t a i
  • a i a j
  • a j t

49
  • while ( i lt j )
  • a l a j
  • a j p
  • qsort ( a, l, j 1)
  • qsort ( a, j 1, r )

50
  • gt???????
  • ???? ????pivot??????,?????
  • ???? ??????pivot????????????????,??????

51
  • ???? ?????pivot??????????,?????????
    ?T(j),?T(n-j-1),j?????1?n??????,??
  • T(n) O(nlogn)

52
????
  • include ltstudio.hgt
  • void quick_sort ( int , int )
  • void main ( )
  • int data 20
  • int size 0, i
  • printf ( \nPlease enter number to sort (enter 0
    when end)\n )
  • printf ( Number )
  • do
  • scanf ( d , data size )
  • while (data size ! 0 )
  • for ( i 0 i lt 60 i ) printf ( - )
  • printf ( \nSorting )
  • for ( I 0 I lt size i )
  • printf ( d , data i )

53
  • void quick_sort (int data , int left, int
    right, int size )
  • int lbase, rbase, compare, temp, i
  • for (base 1 base lt size base )
  • temp data base
  • compare base
  • while ( compare gt 0 data compare 1 gt temp)
  • data compare data compare 1
  • compare - -
  • data compare temp
  • printf ( Access )
  • for ( I 0 i lt size i )
  • printf ( d , data i)
  • printf ( \n )
Write a Comment
User Comments (0)
About PowerShow.com