Click here to load reader

Ex01 Sorting

  • View
    4

  • Download
    1

Embed Size (px)

DESCRIPTION

cấu trúc dữ liệu

Text of Ex01 Sorting

  • n 01: CC THUT TON SP XPMn Cu Trc D Liu & Gii Thut

    Lp 11CK2

    Ngy 12 thng 3 nm 2012

    1 Ti sao phi sp xp d liu?

    Gi s ta c mt tp d liu v phi thc hin tc v tm kim trn tp dliu ny. V d, cho mng s nguyn a v ta cn tm s nguyn k trong mnga ny. Nu tm thy, tr v ch s ca phn t tng ng trong mng; ngcli, tr v 1.

    Cch n gin nht l s dng thut ton tm kim tun t:

    1 // Thuat toan tim kiem tuan tu2 i n t LinearSearch ( i n t a [ ] , i n t n , i n t k )3 {4 f o r ( i n t i = 0 ; i < n ; i++)5 i f ( a [ i ] == k)6 re turn i ;7 re turn 1;8 }

    Thy ngay phc tp ca thut ton tm kim tun t l O(n). Nhngnu mng s nguyn c sp xp theo th t th ta c th gim phc tp xung cn O(log n) bng cch s dng thut ton tm kim nhphn (binary search):

    1

  • 1 // Thuat toan tim kiem nhi phan , a l a mang da duoc sap xep tangdan

    2 i n t BinarySearch ( i n t a [ ] , i n t n , i n t k )3 {4 i n t l e f t = 0 ;5 i n t r i g h t = n 1 ;6 whi le ( l e f t a [ mid ] )10 l e f t = mid + 1 ;11 e l s e i f ( k < a [ mid ] )12 r i g h t = mid 1 ;13 e l s e14 re turn mid ;15 }16 re turn 1;17 }

    Ta thy thut ton tm kim nh phn chy nhanh hn thut ton tmkim tun t, nhng tp d liu cn phi c sp xp sn. Do , nu tathng xuyn phi thc hin tc v tm kim trn mt tp d liu th chinlc khn ngoan l: thc hin sp xp d liu trc; v t v sau, tadng thut ton tm kim nh phn. Tht vy, gi s ta phi thc hin mln tm kim trn mng d liu c kch thc n. Nu dng thut ton tmkim tun t th tng thi gian thc hin l O(mn). Nhng, nu ta thc hinsp xp d liu trc trong khong thi gian T (n) (vi T l mt hm no), v sau dng thut ton tm kim nh phn th tng thi gian s lO(T (n) +m log n): nhanh hn nhiu so vi O(mn) khi m ln.

    2 Cc thut ton sp xp

    Trong bi ny, ta xt bi ton sp xp tng dn.

    2.1 Bubble Sort

    Bubble (n): S ni bt.

    2

  • 2.1.1 tng

    Bubble Sort chia mng thnh hai phn: phn c sp v phn chac sp. Ban u, phn c sp khng c phn t no, phn chac sp gm nguyn mng a[0..n-1].

    Ta tin hnh m rng dn phn c sp nh sau:

    Ln x l th 1: ta duyt t cui n u phn cha c sp aphn t nh nht trong phn cha c sp (lc ny l phn t

    NH NHT ca mng) ni ln u phn cha c sp. Sau bcny, phn c sp c c phn t u tin: chnh l phn t va

    ni ln - phn t NH NHT ca mng. Phn cha c sp thuhp li mt phn t.

    Ln x l th 2: ta li duyt t cui n u phn cha c sp a phn t nh nht trong phn cha c sp (lc ny l

    phn t NH TH HAI ca mng) ni ln u phn cha c sp.Sau bc ny, phn c sp m rng sang phi mt phn t: chnh l

    phn t va ni ln - phn t NH TH HAI ca mng. Phn chac sp thu hp li mt phn t.

    . . . C th cho n khi phn c sp gm a[0..n-2], phn cha c

    sp ch cn mt phn t l a[n-1]: sp xp xong!

    Ta duyt t cui n u phn cha c sp a phn t nhnht trong phn cha c sp ni ln u phn cha c sp: cth nh th no?

    Gi s phn cha c sp hin thi l a[i..n-1]. Ta a phn t nhnht trong a[i..n-1] ni ln u bng cch duyt t cui v u nh sau:

    1 f o r ( i n t j = n1; j > i ; j)2 {3 i f ( a [ j ] < a [ j 1])4 {5 Swap( a [ j ] , a [ j 1]) ; // Hoan v i6 }7 }

    3

  • 2.1.2 Thut ton

    1 void BubbleSort ( i n t a [ ] , i n t n)2 {3 f o r ( i n t i = 0 ; i < n1; i++) // Phan chua duoc sap l a a [ i . . n1]4 {5 // Dua phan tu nho nhat trong phan chua duoc sap no i l en

    dau6 f o r ( i n t j = n1; j > i ; j)7 {8 i f ( a [ j ] < a [ j 1])9 {10 Swap( a [ j ] , a [ j 1]) ; // Hoan v i11 }12 }13 }14 }

    2.1.3 nh gi thut ton

    Thy ngay trong mi trng hp s php so snh lun l:n2i=0

    n i 1 = n(n 1)/2 = O(n2)

    S php hon v (1 php hon v = 3 php gn): Trng hp xu nht: khi mng ban u c th t ngc, lc ny

    ta tnn2

    i=0 n i 1 = n(n 1)/2 = O(n2) php hon v. Trng hp tt nht: khi mng ban u c th t, lc ny ta

    tn 0 php hon v.

    Khuyt im ca Bubble Sort: Khng nhn din c tnh trng mng ban u c th t

    (hoc c th t tng phn).

    Trong khi cc phn t nh c a v v tr ng rt nhanh thcc phn t ln c a v v tr ng rt chm.

    Thut ton Shaker Sort khc phc cc khuyt im ny ca BubbleSort.

    4

  • 2.2 Shaker Sort

    Shaker (n): Sng lc (lc qua tri, lc qua phi).

    2.2.1 tng

    Nh ni trn, Shaker Sort l ci tin ca Bubble Sort. Vi Bubble Sort,phn c sp nm u mng v m rng dn sang phi; mi ln x l,ch m rng thm mt phn t. Vi Shaker Sort, phn c sp nm cu v cui mng v m rng dn t c u v cui vo gia. Hnna, mi ln x l, Shaker Short khn ngoan hn Bubble Sort: n nhndin c tnh trng c th t tng phn ca mng v nh , c thm rng hn mt phn t.

    Ban u, phn c sp u mng v cui mng khng c phn t no,

    phn cha c sp gm nguyn mng a[0..n-1]. mi ln x l, ta mrng dn phn c sp t c u v cui vo gia nh sau:

    Lt i: Ta duyt t cui v u phn cha c sp a[l..r] a phn

    t nh nht trong phn cha c sp ni ln u phn chac sp. Qu trnh duyt ny ging nh Bubble Sort: so snhhai cp phn t k cn a[j] v a[j-1], nu a[j] < a[j-1] thhon v a[j] v a[j-1]. Tuy nhin, Shaker Sort khn ngoanhn: n lu li ch s j ng vi ln xy ra hon v sau cng (tahy gi l j_lastSwap); bng cch ny, nu phn u ca phncha c sp c th t, chng hn a[i..k], th Shaker Sorts nhn din c tnh trng ny (k = j_lastSwap - 1).

    M rng phn sp u mng sang phi: thay v ch lun mrng sang phi 1 phn t nh Bubble Sort, ta s m rng nphn t j_lastSwap - 1. Nh vy, pha tri ca phn cha csp s thu hp li: l = j_lastSwap.

    Lt v: Ta duyt t u v cui phn cha c sp a[l..r] a phn

    t ln nht trong phn cha c sp chm xung cui phncha c sp. Qu trnh duyt ny tng t Bubble Sort: sosnh hai cp phn t k cn a[j] v a[j+1], nu a[j] > a[j+1]

    5

  • th hon v a[j] v a[j+1]. Ging nh trn, ta cng lu li chs j_lastSwap ng vi ln xy ra hon v sau cng.

    M rng phn sp cui mng sang tri: m rng n phn tj_lastSwap + 1. Nh vy, pha phi ca phn cha c sp thuhp li: r = j_lastSwap.

    C th, ta tin hnh x l nhiu ln . . . cho n khi l >= r (phn chac sp ch cn mt phn t hoc khng cn phn t no): sp xp xong!

    2.2.2 Thut ton

    1 void ShakerSort ( i n t a [ ] , i n t n)2 {3 i n t l = 0 ;4 i n t r = n 1 ;5 i n t j_lastSwap = n 1 ; // Luu ch i so ung vo i lan hoan v i sau

    cung6

    7 whi le ( l < r )8 {9 // ====== Bat dau luo t d i ======10 // 1 . Dua phan tu nho no i l en dau11 f o r ( i n t j = r ; j > l ; j)12 {13 i f ( a [ j ] < a [ j 1])14 {15 Swap( a [ j ] , a [ j 1]) ;16 j_lastSwap = j ;17 }18 }19 // 2 . Thu hep phia t r a i cua phan chua duoc sap20 l = j_lastSwap ;21 // ====== Ket thuc luo t d i ======22

    23 // ====== Bat dau luo t ve ======24 // 1 . Dua phan tu lon chim xuong cuo i25 f o r ( i n t j = l ; j < r ; j++)26 {27 i f ( a [ j ] > a [ j +1])28 {29 Swap( a [ j ] , a [ j +1]) ;30 j_lastSwap = j ;31 }

    6

  • 32 }33 // 2 . Thu hep phia phai cua phan chua duoc sap34 r = j_lastSwap ;35 // ====== Ket thuc luo t ve ======36 }37 }

    2.2.3 nh gi thut ton

    Trng hp xu nht: khi mng ban u c th t ngc. Trong trnghp ny, n ging nh Bubble Sort: tn O(n2) php so snh v O(n2)php hon v.

    Shaker Sort ch tt hn Bubble Sort khi mng ban u c th thoc c th t tng phn. Trong trng hp tt nht l mng ban u c th t, Shaker Sort nhn din ra ngay tnh trng ny sau lt iu tin v kt thc thut ton; n ch tn n 1 = O(n) php so snhv khng tn php hon v no.

    2.3 Selection Sort

    Selection (n): S chn la.

    2.3.1 tng

    Selection Sort chia mng thnh 2 phn: phn c sp v phn chac sp. Ban u, phn c sp khng c phn t no, phn cha

    c sp gm nguyn mng a[1..n-1]. mi bc x l, ta m rng dnphn c sp (v do , thu hp dn phn cha c sp) nh sau: taCHN phn t nh nht trong phn cha c sp v hon v n vi phnt u tin ca phn cha c sp. Sau bc ny, phn c sp m rngsang phi mt phn t (chnh l phn t u tin ca phn cha c sp);phn cha c sp thu hp li mt phn t. C th, ta tin hnh x lnhiu ln . . . cho n khi phn c sp gm a[1..n-2], phn cha csp ch cn mt phn t a[n-1]: sp xp xong!

    7

  • 2.3.2 Thut ton

    1 void S e l e c t i o nSo r t ( i n t a [ ] , i n t n)2 {3 f o r ( i n t i = 0 ; i < n1; i++) // Phan chua sap : a [ i . . n1]4 {5 // 1 . Tim phan tu nho nhat trong phan chua sap6 i n t min = i ;7 f o r ( i n t j = i +1; j < n ; j++)8 {9 i f ( a [ j ] < a [ min ] )10 {11 min = j ;12 }13 }14 // 2 . Hoan v i phan tu nho nhat vo i phan tu dau15 i f (min != i )16 {17 Swap( a [ i ] , a [ min ] ) ;18 }19 }20 }

    2.3.3 nh gi thut ton

    tm phn t nh nht trong phn cha sp a[i..n-1], ta phi thchin n-i-1 php so snh. Do vy, trong mi trng hp, s php sosnh lun l:

    n2i=0 (n i 1) = n(n 1)/2 = O(n2).

    S php hon v: Trng hp xu nht: O(n) (khi mng ban u c th t ngc).

    Trng hp tt nht: 0 (khi mng ban u c th t).

    2.4 Insertion Sort

    Insertion (n): S chn vo.

    8

  • 2.4.1 tng

    Insertion Sort chia mng thnh hai phn: phn c sp v phn chac sp. Ban u, phn c sp ch c mt phn t l a[0], phn

    cha c sp gm cc phn t cn li a[1..n-1]. mi ln x l, ta mrng dn phn sp bng cch ly ra phn t u tin ca phn cha sp

    v CHN vo v tr thch hp trong phn sp sao cho: sau khi chn phn sp vn gi nguyn tnh th t. C th, ta tin hnh x l nhiu ln. . . cho n khi phn sp gm a[0..n-1], phn cha sp khng cn phnt no: sp xp xong!

    2.4.2 Thut ton

    1 void I n s e r t i o nSo r t ( i n t a [ ] , i n t n)2 {3 i n t temp ; // Bien de luu l a i g i a t r i cua phan tu can chen4 f o r ( i n t i = 1 ; i < n ; i++)5 {6 temp = a [ i ] ; // Luu l a i g i a t r i cua phan tu can chen7 // Tim v i t r i trong phan da sap de chen temp8 i n t j ;9 f o r ( j = i 1; j >= 0 && a [ j ] > temp ; j)10 {11 a [ i +1] = a [ i ] ; // Dich chuyen cac phan tu cua phan da sap

    sang phai12 }13 a [ j +1] = temp ; // Chen14 }15 }

    2.4.3 nh gi thut ton

    Trng hp xu nht: khi mng ban u c th t ngc. Lc ny, chn phn t a[i] (vi i chy t 1 n n-1) vo phn spa[0..i-1], ta lun phi so snh a[i] vi tt c phn t ca phn sp, dch chuyn tt c cc phn t ca phn sp sang phiv chn a[i] vo v tr 0. Do , trong trng hp xu nht, ta tn:1 + 2 + 3 + + (n 1) = n(n 1)/2 = O(n2) php so snh v dchchuyn.

    9

  • Trng hp tt nht: khi mng ban u c th t. Ta ch tn: O(n)php so snh v 0 php dch chuyn.

    2