Quick Sort algorithm in Haskell

download Quick Sort algorithm in Haskell

of 8

Transcript of Quick Sort algorithm in Haskell

  • 7/22/2019 Quick Sort algorithm in Haskell

    1/8

    Yujoa _hrtIbchrjtmd usjkc

    Misalbb

    Pj9:707

    Iucust 7, 509?

  • 7/22/2019 Quick Sort algorithm in Haskell

    2/8

    Ohktlkts

    Ohktlkts

    0.9 Jktrhguotjhk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    0.5 Yujoa _hrt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    0.5.9 Pirtjhkjkc tml Bjst . . . . . . . . . . . . . . . . . . . . .

    0.5.5 _lblotjkc Uikghd Pjvht . . . . . . . . . . . . . . . . . . .

    0.5.? Jdpbldlktitjhk . . . . . . . . . . . . . . . . . . . . . . . .

    Ulflrlkols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  • 7/22/2019 Quick Sort algorithm in Haskell

    3/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    0.9 JktrhguotjhkJk tmjs irtjobl wl wjbb bhha it jdpbldlktitjhk hf Yujoa _hrt Ibchrjtmd

    usjkc Misalbb.

    0.5 Yujoa _hrtYujoa shrt js i gjvjgl ikg ohkqulr ibchrjtmd.Yujoa shrt fjrst gjvjglsi bircl bjst jkth twh sdibblr sue bjsts.Jt tmlk rloursjvlby shrts tmlsue bjsts. Wmjs js sjdjbir th dlrcl shrt.Jkput tml ibchrjtmd js ukshrtlg irriy,ikg hutput js tml shrtlg irriy

    ikg phssjeby kudelr hf jtlritjhks. Blt O el tml hutput irriy hfblkctm

    Blt us issudl wl mivl spbjt tml bjst jkth twh pirts I

    ikg E iehut i lbldlkt hf jkglx .Wmjs lbldlkt js oibblg tml pjvht

    lbldlkt.

    Ibb tml lbldlkts th blft hf pjvht lbldlkt irl blss tmik pjvht lbldlktikg ibb tml lbldlkts hf bjst th rjcmt hf pjvht lbldlkt irl crlitlrtmik pjvht lbldlkt.Wml sue bjsts I ikg E irl shrtlg rloursjvlby.Wmus qujoa shrt hplritjhk ohksjsts hf fhbbhwjkc stlps

    omhhsl pjvht lbldlkt

    pirtjtjhkjkc tml bjst

    Uloursjhk

    0.5.9 Pirtjtjhkjkc tml Bjst

    Jf wl irl ibbhwlg th omhhsl iggjtjhkib irriy fhr sthricl,tml pirtj-tjhkjkc hplritjhk oik el plrfhrdlg ey chjkc hvlr tml bjst hkol tmitjs

    .

    Wml ijd hf pirtjtjhkjkc ibchrjtmd js th dhvl ibb tml lbldlkts wmjomirl blss tmik pjvht lbldlkt th el pbiolg th blft hf pjvht lbldlktikg ibb lbldlkts crlitlr tmik pjvht th tml rjcmt.

  • 7/22/2019 Quick Sort algorithm in Haskell

    4/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    Sjtmhut bhss hf clklribjty blt us issudl tmit fjrst lbldlkt hf tmlbjst js omhhsl is tml pjvht lbldlkt. Jf iky htmlr lbldlkt js omhslk

    is pjvht lbldlkt,is jkjtjibjzitjhk stlp rlpbiol tml fjrst lbldlkt hf

    tml bjst wjtm tml pjvht lbldlkt ikg prhollg wjtm tml prlslkt ibch-rjtmd.Wh plrfhrd jk pbiol shrtjkc wl dijktijk 5 jkglxls

    .Wml jkglx j

    jkgjoitls tml lbldlkts hf tml bjst tmit irl elbhw pjvht lbldlkt,frhdjkglx j th n irl lbldlkts iehvl pjvht lbldlkt.Wml lbldlkt iehvljkglx n irl lbldlkts tmit irl ylt th el ikibyzlg.Jkjtjibby tml jkglx j4-9 ikg n4-9 wmjom js i trjvjib oisl.Wml jkglx n jsjkorldlktlg Khw ohdpirl tml lbldlkt it jkglx j wjtm jkglx n .Bltvibul hf pjvht lbldlkt el rlprlslktlg is p.jf

    tmlk wl kllg

    th igg

    .

    jk tml iehvl lxidpbl

    ,mhwlvlr sjkol j ikg n phjkt th sidl jkglxbhoitjhk kh swippjkc js plrfhrdlg.Wml j ikg n jkglxls irl sjdpbyjkorldlktlg ey 9 ikg upgitlg jkglxs irl diralg

    Khw tml pjvht lbldlkt js ohdpirlg wjtm ?,jk tmjs oisl

    ,n jsjkorldlktlg ey 9, ikg ? js swipplg wjtm

  • 7/22/2019 Quick Sort algorithm in Haskell

    5/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    rlqujrlg th plrfhrd jk-pbiol pirtjtjhkjkc ikg jt ghls kht rlqujrliggjtjhkib dldhry sthricl.Mhwlvlr fukotjhkib bikcuicl glibs wjtm jddutiebl giti typl tml bjst

    oikkht el ibtlrlg.jk pbiol hplritjhks irl kht ibbhwlg. Sl oikplrfhrd triksfhrditjhk hk tml ourrlkt bjst ikg rlturk i klw bjst.usjkc iehvl dltmhg hf swippjkc ikg ohdpirjkc prhvls th el dhrllxplksjvl.

    Wh pirtjtjhk i bjst hf blkctm K ,

    ohdpirjshks irl rlqujrlg.furtmlr liom stlp swippjkc js plrfhrdlg wmjom furtmlr rlqujrls Kohdputitjhks. ikg tmjs stlp jtslbf bligs th

    ohdputitjhks.

    Jk wmjom oisl iehvl ipprhiom ghls kht prhvl th el uslfub.I rloursjvldltmhg th pirtjtjhk tml bjst js uslg.

    It liom stlp tml pjvht lbldlkt js ohdpirlg th tml mlig hf tml

    bjst ikg jf trul tml lbldlkt js ipplkglg th hkl bjst lbsl jt jsipplkglg th ikhtmlr bjst.\sjkc i rloursjvl dltmhg ,wl sjdpby plrfhrd i sjkcbl ohdpirjshk

    ikg sjkcbl ohpy hplritjhks it liom jtlritjhk bhhp.Ikg pirtjtjhkjkc jsplrfhrdlg jk

    ohdputitjhks.

    Wmlsl bjst irl uslg is ik iooudubithr.Wmly irl pisslg th jkput hftml fukotjhk ikg triksfhrdlg bjst js lxplotlg it hutput hf tmlfukotjhk.Wml pirtjtjhkjkc ibchrjtmd ioolpts tml pjvht lbldlkt,tml jkput bjst,ikg

    twh iooudubithr suebjsts.Wml pirtjtjhkjkc js plrfhrdlg rloursjvlby tjbb ibb tml lbldlkts hfjkput bjst th el pirtjtjhklg irl lxmiustlg.

    Blt us ohksjglr i pirtXbjst9 ibchrjtmd. I stiebl shrtjkc ibchrjtmdallps tml lbldlkts wjtm sidl shrtjkc aly jk hrglr. Wmly rlturktml sidl hutput hk tml sidl jkput.Jk tml prlslkt oisl jf wl mivl gupbjoitl lbldlkts ,tmljr hrglrsmhubg el prlslrvlg. Wml pirtXbjst9 ikibyzls tml bjst frhd mlig,ikg klw lbldlkts irl ipplkglg th tml tijb hf tml bjst.Wmus rlbitjvl

    hrglr hf gupbjoitl lbldlkts wjbb ibwiys el rlvlrslg.Wmjs dhrl hvlr gul th sh sjdpbl wiy th igg lbldlkt th lkg hfldpty bjst. jl

    js kht ibbhwlg wmjbl

    js ibbhwlg.Ikhtmlr shbu-

    tjhk whubg el th rlvlrsl tml bjst.Wmhucmt jt js icijk

    iggjtjhkibohdputitjhk.

    Ibsh jk prhcrid wl dust ivhjg ohdputitjhk hf blkctm hf bjst wmjomicijk tials

    tjdl.

    \sjkc rikghd pjvht slblotjhk wl oik ibsh clt ivlricl oisl plrfhr-dikol hf

    .

  • 7/22/2019 Quick Sort algorithm in Haskell

    6/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    0.5.5 _lblotjkc Uikghd Pjvht

    Wml trul strlkctm hf qujoa shrt ibchrjtmd js rikghd slblotjhk hfpjvht. Jf wl slblot tml fjrst lbldlkt hf pjvht ikg irriy js i shrtlg

    bjst jt wjbb plrfhrd

    ohdputitjhks,sjkol it liom jtlritjhk tmlsjzl hf sdibblr sueprhebld jk

    .Jf tml dlgjik js slblotlg ispjvht jk liom hf tml suebjsts,sjzl hf sueprhebld js

    tmjs cjvls us

    plrfhrdikol.

    Wml rikghd kudelr clklrithr js rlqujrlg ey qujoa shrt ibchrjtmd.Jk tml stikgirg dltmhg wl rldhvl tml pjvht lbldlkt,ohk-oit

    tml rldijkjkc irriy ikg tmlk prhollg wjtm pirtjtjhkjkc hplri-tjhks,mhwlvlr tmjs rlqujrls ohpyjkc hf tml irriy ikg icijk

    iggjtjhkib hplritjhk.

    Uitmlr tmik orlitjkc i klw irriy,wl dijktijk tml jkglx hf tml pjvhtlbldlkt ikg gurjkc pirtjtjhkjkc hplritjhks wmlk wl irl plrfhrdjkctml ohdpirjshk hplritjhk tml jkglx hf ourrlkt lbldlkt js omloalgicijkst tml pjvht jkglx ikg sajpplg gurjkc prholssjkc.Wmjs ghls kht blig th iggjtjhkib ohpy hf tml irriy.Wmus dhgjfjlg pirtjtjhk dltmhg js is elbhw

    Jk tml iehvl dltmhg wmjbl slblotjkc tml pjvht lbldlkt wl kllgth akhw tml blkctm hf tml irriy.Uitmlr tmik ohdputjkc jt lvlrytjdl.Wmjs js alpt trioa hf gurjkc tml pirtjtjhkjkc hplritjhk.Elbhw js tml dltmhg fhr eisjo qujoa shrt ibchrjtmd

  • 7/22/2019 Quick Sort algorithm in Haskell

    7/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    Wypjoibby ohkoitjkitjkc bjsts irl lxplksjvl hplritjhks.Jk oisl hf fuko-tjhkib bikcuicl jt smhubg el ivhjglg.Is uslg jk pirtjtjhkjkc dltmhgi dltmhg wmjom usls iooudubithr irriys whubg el dhrl elklfjojib.

    Blt us ohksjglr tml oisl tmit wl mivl twh bjsts ik ukshrtlg bjstikg iooudubithr bjst wmjom ohktijks tml shrtlg phrtjhk.It liom jtlritjhk wl fjrst pirtjtjhk ukshrtlg bjst jkth I ikg E.Wmlk rloursjvlby shrt E,blt _ el shrtlg vlrsjhk hf E ikg O eljkput iooudubithr ikg P js pjvht lbldlkt

    Wml shrtlg irriy it tmjs stlp js P+_+O ikg wjbb el tml klw ioou-dubithr.

    Wml shrtjkc rhutjkl js dhgjfjlg th ioolpt tml iooudubithr is jkputikg hutputs _+O is tml shrtlg irriy .It tml bhwlst lbldlkt hf rloursjhk wl ipplkg tml mjcmlr lbldlkthf E wjtm O It tml klxt mjcmlst blvlb wl ipplkg tml klxt mjcmlstlbldlkt wjtm (E+O) ikg sh hk. Wmlrley iggjkc th jkput iooudubithr

    it liom stlp th prhguol tml hutput bjst.

  • 7/22/2019 Quick Sort algorithm in Haskell

    8/8

    Yujoa _hrt Ibchrjtmd usjkc Misalbb

    0.5.? Jdpbldlktitjhk

    Jbb el jdpbldlktjkc ibb tml ibchrjtmds usjkc misalb. _jkol rloursjhkjs eisjo dlomikjsd th bhhp jk Misalbb ,jt fjts jk kituribby wjtmjk

    rloursjvl ibchrjtmdjo struoturl hf gjvjgl ikg ohkqulr ibchrjtmds.Wml ohgl oik el fhukg it

    https://github.com/pi19404/m19404/blob/master/Algorithm/sort/sort.hshttps://github.com/pi19404/m19404/blob/master/Algorithm/sort/sort.hs