Chapter 13 - Searching and Sorting

Chapter 13 - Searching and Sorting - &KDSWHU 6HDUFKLQJ...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU 6HDUFKLQJ DQG 6RUWLQJ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS ,PSOHPHQWLQJ 6HDUFKLQJ $OJRULWKPV 6HTXHQWLDO 6HDUFK %LQDU\ 6HDUFK 6HDUFKLQJ 2EMHFWV ,PSOHPHQWLQJ 6RUWLQJ $OJRULWKPV 6HOHFWLRQ 6RUW 0HUJH 6RUW 2WKHU 6RUWLQJ $OJRULWKPV 6HDUFKLQJ DQG 6RUWLQJ LQ WKH -DYD &ODVV /LEUDULHV %LQDU\ 6HDUFK 6RUWLQJ 6KXIIOLQJ &XVWRP 2UGHULQJ ZLWK &RPSDUDWRUV 3URJUDP (IILFLHQF\ $OJRULWKP 5XQWLPHV &RPSOH[LW\ &ODVVHV ,QWURGXFWLRQ :KHQ ZH GHDO ZLWK ODUJH DPRXQWV RI GDWD ZH RIWHQ ZDQW WKH DELOLW\ WR VHDUFK WKH GDWD IRU D SDUWLFXODU YDOXH )RU H[DPSOH \RX PLJKW ZDQW WR VHDUFK WKH LQWHUQHW IRU D ZHE SDJH WKDW FRQWDLQV D FHUWDLQ NH\ZRUG RU \RX PLJKW ZLVK WR VHDUFK D SKRQH ERRN IRU D SHUVRQ V SKRQH QXPEHU ,W V DOVR D XVHIXO RSHUDWLRQ WR EH DEOH WR UHDUUDQJH D FROOHFWLRQ RI GDWD LQWR VRUWHG RUGHU )RU H[DPSOH \RX PD\ ZLVK WR VRUW D OLVW RI VWXGHQWV FRXUVH JUDGH GDWD E\ QDPH E\ VWXGHQW ,' RU E\ JUDGH ,Q WKLV FKDSWHU ZH OO ORRN DW ZD\V WR XVH -DYD V FODVV OLEUDULHV WR VHDUFK DQG VRUW GDWD $IWHUZDUG ZH OO OHDUQ KRZ WR LPSOHPHQW VRPH VHDUFKLQJ DQG VRUWLQJ DOJRULWKPV RI RXU RZQ DV ZHOO DV WDONLQJ PRUH JHQHUDOO\ DERXW KRZ WR REVHUYH DQG DQDO\]H WKH UXQWLPH RI DOJRULWKPV 653 6HDUFKLQJ DQG 6RUWLQJ LQ WKH -DYD &ODVV /LEUDULHV ,I \RX KDYH DQ $UUD\/LVW FROOHFWLRQ \RX FDQ VHDUFK LW E\ FDOOLQJ LWV LQGH[2I PHWKRG 7KLV PHWKRG H[DPLQHV HDFK HOHPHQW RI WKH OLVW ORRNLQJ IRU D WDUJHW YDOXH ,W UHWXUQV WKH ILUVW LQGH[ DW ZKLFK WKH WDUJHW YDOXH RFFXUV LQ WKH OLVW RU LI WKH WDUJHW GRHVQ W RFFXU ,PDJLQH D ODUJH $UUD\/LVW FRQWDLQLQJ DOO ZRUGV LQ D ERRN // read book into ArrayList Scanner in = new Scanner(new File("mobydick.txt")); ArrayList<String> words = new ArrayList<String>(); while (in.hasNext()) { words.add(in.next()); } return words; <RX FRXOG XVH WKH LQGH[2I PHWKRG WR VHH ZKHWKHU D JLYHQ ZRUG DSSHDUV LQ WKH ERRN DQG DW ZKDW LQGH[ // search list using indexOf System.out.print("Your word? "); Scanner console = new Scanner(System.in); String word = console.nextLine(); int index = words.indexOf(word); if (index >= 0) { System.out.println(word + " is word #" + index + " in the book."); } else { System.out.println(word + " isn't found in the book."); } 7KH LQGH[2I PHWKRG LV ZKDW ZH FDOO D VHTXHQWLDO VHDUFK EHFDXVH LW H[DPLQHV HDFK HOHPHQW RI WKH OLVW LQ VHTXHQFH XQWLO LW ILQGV WKH RQH \RX UH ORRNLQJ IRU ,I LW UHDFKHV WKH HQG RI WKH OLVW ZLWKRXW ILQGLQJ \RXU HOHPHQW LW UHWXUQV :KHQ VHDUFKLQJ D HOHPHQW OLVW IRU DQ HOHPHQW DW LQGH[ D VHTXHQWLDO VHDUFK ZRXOG KDYH WR H[DPLQH DOO HOHPHQWV XS WR WKDW SRLQW EHIRUH LW IRXQG WKH ULJKW YDOXH ,I \RX KDYH DQ DUUD\ RI GDWD LQVWHDG RI D OLVW WKHUH V QR SUHZULWWHQ PHWKRG WR VHTXHQWLDOO\ VHDUFK LW <RX OO KDYH WR ZULWH WKH FRGH \RXUVHOI DV ZH OO GR ODWHU LQ WKLV FKDSWHU RU SXW WKH DUUD\ V HOHPHQWV LQWR DQ $UUD\/LVW ILUVW DQG VHDUFK WKDW ZLWK LQGH[2I %LQDU\ 6HDUFK 6RPHWLPHV ZH ZDQW WR VHDUFK WKURXJK HOHPHQWV RI DQ DUUD\ RU OLVW WKDW ZH NQRZ LV LQ VRUWHG RUGHU )RU H[DPSOH PD\EH ZH ZDQW WR NQRZ ZKHWKHU TXHDV\ LV D UHDO (QJOLVK ZRUG VR ZH VHDUFK WKH FRQWHQWV RI DQ DOSKDEHWL]HG GLFWLRQDU\ WH[W ILOH 2U SHUKDSV \RX UH ORRNLQJ IRU D ERRN ZULWWHQ E\ 5REHUW /RXLV 6WHYHQVRQ LQ D VWDFN RI ERRNV VRUWHG E\ WKH DXWKRU V ODVW QDPH ,I WKH GLFWLRQDU\ LV ODUJH RU WKH VWDFN RI ERRNV LV WDOO LW V XQOLNHO\ WKDW \RX G ZDQW WR VHTXHQWLDOO\ H[DPLQH DOO RI WKHP 654 7KHUH V D EHWWHU DOJRULWKP QDPHG ELQDU\ VHDUFK WKDW VHDUFKHV VRUWHG GDWD PXFK IDVWHU WKDQ D VHTXHQWLDO VHDUFK $ QRUPDO VHTXHQWLDO VHDUFK RI D PLOOLRQ HOHPHQW DUUD\ PD\ KDYH WR H[DPLQH DOO WKH HOHPHQWV EXW D ELQDU\ VHDUFK ZLOO RQO\ QHHG WR ORRN DW DURXQG RI WKHP -DYD V FODVV OLEUDULHV FRQWDLQ PHWKRGV WKDW LPSOHPHQW WKH ELQDU\ VHDUFK DOJRULWKP IRU DUUD\V DQG OLVWV %LQDU\ VHDUFK EHJLQV E\ H[DPLQLQJ WKH FHQWHU HOHPHQW RI WKH DUUD\ ,I WKH FHQWHU HOHPHQW LV VPDOOHU WKDQ WKH WDUJHW ZH UH VHDUFKLQJ IRU WKHUH V QR UHDVRQ WR H[DPLQH DQ\ HOHPHQWV WR WKH OHIW RI WKH FHQWHU RQHV DW ORZHU LQGH[HV &RQYHUVHO\ LI WKH FHQWHU HOHPHQW LV ODUJHU WKDQ WKH WDUJHW ZH UH VHDUFKLQJ IRU WKHUH V QR UHDVRQ WR H[DPLQH DQ\ HOHPHQWV WR WKH ULJKW RI WKH FHQWHU RQHV DW JUHDWHU LQGH[HV 2Q HDFK SDVV RI WKH DOJRULWKP KDOI WKH VHDUFK VSDFH LV HOLPLQDWHG IURP FRQVLGHUDWLRQ VR WKH WDUJHW YDOXH LV IRXQG PXFK IDVWHU WKDQ LQ D VHTXHQWLDO VHDUFK &RQVLGHU WKH H[DPSOH RI D JXHVVLQJ JDPH ZKHUH WKH FRPSXWHU WKLQNV RI D QXPEHU EHWZHHQ DQG DQG WKH XVHU WULHV WR JXHVV LW $IWHU HDFK LQFRUUHFW JXHVV WKH SURJUDPV JLYHV WKH XVHU D KLQW DERXW ZKHWKHU LWV JXHVV ZDV WRR KLJK RU WRR ORZ $ SRRU DOJRULWKP IRU WKLV JDPH LV WR JXHVV DQG VR RQ $ VPDUWHU DOJRULWKP LV WR JXHVV WKH PLGGOH QXPEHU DQG FXW WKH UDQJH LQ KDOI HDFK WLPH EDVHG RQ ZKHWKHU WKH JXHVV ZDV WRR KLJK RU WRR ORZ 3DVVHV RI D ELQDU\ VHDUFK IRU D QXPEHU EHWZHHQ 0 10 20 30 40 50 guess = 50 ^ incorrect. hint: higher 0 10 20 30 40 guess = 75 incorrect. hint: lower 50 60 70 80 DQG 90 100 60 70 ^ 80 90 100 0 10 20 30 40 50 guess = 62 incorrect. hint: higher 0 10 20 30 40 50 guess = 69 incorrect. hint: higher 0 10 20 30 40 guess = 72 incorrect. hint: lower 0 10 20 guess = 71 correct! 30 40 50 60 ^ 70 80 90 100 60 70 ^ 80 90 100 60 70 ^ 80 90 100 50 60 70 ^ 80 90 100 %LQDU\ VHDUFK XVHV WKLV VDPH LGHD ZKHQ VHDUFKLQJ D VRUWHG DUUD\ IRU D WDUJHW YDOXH 7KH DOJRULWKP VFDOHV DPD]LQJO\ ZHOO WR ODUJH LQSXW GDWD ZKHQ VHDUFKLQJ D RQH PLOOLRQ HOHPHQW OLVW D ELQDU\ VHDUFK ZRXOG KDYH WR H[DPLQH RQO\ DURXQG HOHPHQWV 7KH $UUD\V FODVV LQ WKH MDYD XWLO SDFNDJH FRQWDLQV D VWDWLF PHWKRG QDPHG ELQDU\6HDUFK WKDW LPSOHPHQWV WKH ELQDU\ VHDUFK DOJRULWKP ,W DFFHSWV DQ DUUD\ RI DQ\ VXLWDEOH W\SH DQG D WDUJHW YDOXH DV LWV SDUDPHWHUV DQG UHWXUQV WKH LQGH[ ZKHUH \RX FDQ ILQG WKH WDUJHW HOHPHQW ,I WKH HOHPHQW LVQ W IRXQG D QHJDWLYH LQGH[ LV UHWXUQHG +HUH V D TXLFN H[DPSOH XVDJH RI WKLV PHWKRG 655 // demonstrate the Arrays.binarySearch method String strings = {"a", "b", "c", "d", "e", "f", "g", "h"}; int index = Arrays.binarySearch(strings, "e"); // 4 System.out.println("e is found at index " + index); ,I \RX UH XVLQJ D OLVW VXFK DV DQ $UUD\/LVW LQVWHDG &ROOHFWLRQV ELQDU\6HDUFK WR VHDUFK WKH OLVW RI HOHPHQWV \RX FDQ FDOO WKH VWDWLF PHWKRG String strings = {"a", "b", "c", "d", "e", "f", "g", "h"}; ArrayList<String> list = new ArrayList<String>(); for (String s : strings) { list.add(s); } // demonstrate the Collections.binarySearch method int index = Collections.binarySearch(list, "e"); // 4 System.out.println("e is found at index " + index); ,Q WKH FDVH RI DUUD\V RU OLVWV WKH GDWD PXVW EH LQ VRUWHG RUGHU WR XVH WKH ELQDU\6HDUFK PHWKRG EHFDXVH LW UHOLHV RQ WKH VRUWHGQHVV WR PRUH TXLFNO\ ILQG WKH WDUJHW YDOXH ,I \RX FDOO ELQDU\6HDUFK RQ XQVRUWHG GDWD WKH UHVXOWV DUH XQGHILQHG DQG WKH DOJRULWKP GRHVQ W SURPLVH WKDW LW ZLOO UHWXUQ WKH ULJKW DQVZHU 7KH IROORZLQJ SURJUDP GHPRQVWUDWHV D ELQDU\ VHDUFK IRU D ZRUG LQ D GLFWLRQDU\ ILOH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import java.io.*; import java.util.*; public class FindWords { public static void main(String args) throws FileNotFoundException { Scanner in = new Scanner(new File("words.txt")); ArrayList<String> words = new ArrayList<String>(); while (in.hasNext()) { words.add(in.next()); } System.out.print("Your word? "); Scanner console = new Scanner(System.in); String word = console.nextLine().trim().toLowerCase(); int index = Collections.binarySearch(words, word); if (index >= 0) { System.out.println(word + " is word #" + index + " in the dictionary."); } else { System.out.println(word + " isn't found in the dictionary."); } } } +HUH DUH WZR H[DPSOH UXQV RI WKH SURJUDP DQG WKHLU UHVXOWLQJ RXWSXW Your word? tugboat tugboat is word #159226 in the dictionary. Your word? googlymoogly googlymoogly isn't found in the dictionary. 656 6RUWLQJ 6RUWLQJ LV D YHU\ FRPPRQ DQG LPSRUWDQW FRPSXWLQJ WDVN :KHQ EURZVLQJ \RXU KDUG GULYH \RX PLJKW VRUW \RXU ILOHV E\ ILOHQDPH H[WHQVLRQ DQG GDWH :KHQ SOD\LQJ PXVLF \RX PLJKW VRUW \RXU VRQJV E\ DUWLVW \HDU RU JHQUH :H DOVR VDZ WKDW VRUWHG DUUD\V DQG OLVWV FDQ EH VHDUFKHG YHU\ TXLFNO\ ZLWK WKH ELQDU\ VHDUFK DOJRULWKP 6RUWLQJ IXQFWLRQDOLW\ LV SURYLGHG E\ WKH -DYD FODVV OLEUDULHV IRU DUUD\V DQG OLVWV $Q DUUD\ FDQ EH VRUWHG ZLWK WKH $UUD\V VRUW PHWKRG // demonstrate the Arrays.sort method String strings = {"c", "b", "g", "h", "d", "f", "e", "a"}; Arrays.sort(strings); System.out.println(Arrays.toString(strings)); 7KH SUHFHGLQJ FRGH SURGXFHV WKH IROORZLQJ RXWSXW [a, b, c, d, e, f, g, h] 7KH DUUD\ PXVW EH RI D W\SH WKDW FDQ EH FRPSDUHG WKDW LV RQH WKDW LPSOHPHQWV WKH &RPSDUDEOH LQWHUIDFH )RU H[DPSOH \RX FDQ VRUW DQ DUUD\ RI LQWV RU 6WULQJV EXW \RX FDQ W VRUW DQ DUUD\ RI 3RLQW REMHFWV RU &RORU REMHFWV 7KHUH LV DOVR D PHWKRG QDPHG &ROOHFWLRQV VRUW WKDW DFFHSWV D OLVW VXFK DV DQ $UUD\/LVW DV D SDUDPHWHU DQG SXWV LWV HOHPHQWV LQWR VRUWHG RUGHU 7KH IROORZLQJ FRGH SURGXFHV WKH VDPH RXWSXW // demonstrate the Collections.sort method ArrayList<String> list = new ArrayList<String>(); for (String s : strings) { // uses previous array list.add(s); } Collections.sort(list); System.out.println(list); :KHQ XVHG ZLWK SULPLWLYH GDWD WKH $UUD\V VRUW PHWKRG XVHV DQ DOJRULWKP QDPHG TXLFNVRUW &ROOHFWLRQV VRUW DQG $UUD\V VRUW XVH D GLIIHUHQW DOJRULWKP QDPHG PHUJH VRUW ZKHQ GHDOLQJ ZLWK REMHFW GDWD :H OO GLVFXVV LPSOHPHQWDWLRQ RI PHUJH VRUW LQ GHWDLO ODWHU LQ WKH FKDSWHU 6KXIIOLQJ 7KH RSSRVLWH RI VRUWLQJ PLJKW EH WKH WDVN RI VKXIIOLQJ GDWD RU UHDUUDQJLQJ LWV HOHPHQWV LQWR D UDQGRP RUGHU :K\ ZRXOG RQH ZDQW WR GR WKLV" 2QH FDVH ZRXOG EH ZKHQ \RX ZDQW D UDQGRP SHUPXWDWLRQ RI D OLVW RI QXPEHUV 6D\ WKDW \RX ZDQW D UDQGRP SHUPXWDWLRQ RI WKH QXPEHUV IURP WKURXJK <RX FRXOG PDNH DQ DUUD\ RU OLVW DQG SXW ILYH UDQGRP YDOXHV LQWR LW EXW \RX G KDYH WR ZULWH VRPH FOXPV\ FRGH WR FRQILUP WKDW WKH UDQGRP YDOXHV ZHUH XQLTXH DQG DOO IHOO LQ WKH SURSHU UDQJH $ EHWWHU ZD\ WR GR LW ZRXOG EH WR MXVW VWRUH WKH QXPEHUV WKURXJK LQWR D OLVW DQG VKXIIOH WKH OLVW 657 $QRWKHU H[DPSOH LV ZKHQ LPSOHPHQWLQJ D FDUG JDPH <RX PLJKW KDYH D FDUG GHFN VWRUHG DV D OLVW RI &DUG REMHFWV 7R VKXIIOH WKH GHFN RI FDUGV \RX G OLNH WR UHDUUDQJH WKH FDUG REMHFWV LQWR D UDQGRP RUGHULQJ 7KHUH V D PHWKRG QDPHG &ROOHFWLRQV VKXIIOH WKDW DFFHSWV D OLVW DV LWV SDUDPHWHU DQG UHDUUDQJHV LWV HOHPHQWV UDQGRPO\ +HUH V D TXLFN FRGH H[DPSOH WKDW FUHDWHV D GHFN RI FDUG 6WULQJV VKXIIOHV LW WKHQ H[DPLQHV WKH FDUG DW WKH HQG RI WKH GHFN String ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; String suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; ArrayList<String> deck = new ArrayList<String>(); for (String rank : ranks) { for (String suit : suits) { deck.add(rank + " of " + suit); } } Collections.shuffle(deck); System.out.println("Top card = " + deck.get(deck.size() - 1)); 7KH FRGH UDQGRPO\ SURGXFHV RXWSXW VXFK DV WKH IROORZLQJ D GLIIHUHQW RXWSXW RQ HDFK UXQ Top card = 10 of Spades 7KH IROORZLQJ WDEOH EULHIO\ VXPPDUL]HV WKH XVHIXO VWDWLF PHWKRGV LQ -DYD V FODVV OLEUDULHV IRU VHDUFKLQJ VRUWLQJ DQG VKXIIOLQJ 6HDUFKLQJ DQG 6RUWLQJ LQ -DYD V &ODVV /LEUDULHV 0HWKRG Arrays.binarySearch(array) 'HVFULSWLRQ 5HWXUQV WKH LQGH[ RI WKH JLYHQ YDOXH LQ WKH JLYHQ DUUD\ DVVXPLQJ WKDW WKH DUUD\ V HOHPHQWV DUH FXUUHQWO\ LQ VRUWHG RUGHU Arrays.sort(array) $UUDQJHV WKH JLYHQ DUUD\ V HOHPHQWV LQWR VRUWHG RUGHU Collections.binarySearch(list) 5HWXUQV WKH LQGH[ RI WKH JLYHQ YDOXH LQ WKH JLYHQ OLVW DVVXPLQJ WKDW WKH OLVW V HOHPHQWV DUH FXUUHQWO\ LQ VRUWHG RUGHU Collections.shuffle(list) $UUDQJHV WKH JLYHQ OLVW V HOHPHQWV LQWR D UDQGRP RUGHU Collections.sort(list) $UUDQJHV WKH JLYHQ OLVW V HOHPHQWV LQWR VRUWHG RUGHU &XVWRP 2UGHULQJ ZLWK &RPSDUDWRUV 6RPHWLPHV \RX ZDQW WR VHDUFK RU VRUW D FROOHFWLRQ RI REMHFWV LQ DQ RUGHULQJ GLIIHUHQW WKDW WKDW RI LWV &RPSDUDEOH LPSOHPHQWDWLRQ )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FRGH WKDW VRUWV DQ DUUD\ RI 6WULQJV DQG SULQWV WKH UHVXOW String strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings); System.out.println(Arrays.toString(strings)); 7KH IROORZLQJ RXWSXW LV SURGXFHG ZKLFK PD\ QRW EH ZKDW \RX H[SHFWHG 658 [Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel] 1RWLFH WKDW WKH HOHPHQWV DUH LQ FDVH VHQVLWLYH DOSKDEHWLFDO RUGHULQJ ZLWK DOO XSSHUFDVH VWULQJV FRPLQJ EHIRUH DOO ORZHUFDVH RQHV 7KH SUREOHP LV WKDW WKH FRPSDUH7R PHWKRG RI 6WULQJ REMHFWV XVHV FDVH VHQVLWLYH RUGHULQJ ,W V FRQFHLYDEOH WKDW ZH PLJKW ZDQW WR VRUW LQ PDQ\ RUGHUV VXFK DV E\ OHQJWK E\ D FDVH LQVHQVLWLYH RUGHULQJ RU HYHQ LQ UHYHUVH RUGHU ,W V SRVVLEOH WR GHILQH \RXU RZQ RUGHULQJV IRU REMHFWV ZLWK VSHFLDO REMHFWV QDPHG FRPSDUDWRUV WKDW LPSOHPHQW FRPSDULVRQV EHWZHHQ SDLUV RI REMHFWV 7KH LQWHUIDFH &RPSDUDWRU LQ WKH MDYD XWLO SDFNDJH KDV D PHWKRG QDPHG FRPSDUH WKDW GHVFULEHV VXFK D FRPSDULVRQ 7KH $UUD\V VRUW &ROOHFWLRQV VRUW DQG $UUD\V ELQDU\6HDUFK PHWKRGV DOO KDYH YDULDWLRQV WKDW DFFHSW D &RPSDUDWRU DV DQ DGGLWLRQDO SDUDPHWHU 7KLV FRPSDUDWRU ZLOO EH XVHG WR GHWHUPLQH WKH RUGHULQJ EHWZHHQ WKH HOHPHQWV RI \RXU DUUD\ RU FROOHFWLRQ <RX FDQ LPSOHPHQW \RXU RZQ FRPSDUDWRUV RU XVH VRPH WKDW FRPH ZLWK -DYD V FODVV OLEUDULHV )RU H[DPSOH LI \RX G OLNH WR VRUW 6WULQJV LQ FDVH LQVHQVLWLYH DOSKDEHWLFDO RUGHU WKHUH V D VWDWLF FRQVWDQW &RPSDUDWRU REMHFW LQ WKH 6WULQJ FODVV QDPHG &$6(B,16(16,7,9(B25'(5 WKDW \RX FDQ XVH String strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER); System.out.println(Arrays.toString(strings)); 7KH DUUD\ LV RXWSXW LQ WKH IROORZLQJ RUGHU [alpha, bravo, Charlie, DELTA, echo, Foxtrot, golf, hotel] ,PSOHPHQWLQJ D &RPSDUDWRU LV PXFK OLNH LPSOHPHQWLQJ WKH &RPSDUDEOH LQWHUIDFH H[FHSW WKDW LQVWHDG RI SODFLQJ WKH FRGH LQVLGH WKH FODVV WR EH FRPSDUHG \RX ZULWH LW DV LWV RZQ FODVV WKDW DFFHSWV WZR SDUDPHWHUV IRU WKH REMHFWV WR FRPSDUH &RPSDUDWRU LV D JHQHULF LQWHUIDFH WKDW PXVW EH WROG WKH W\SH RI REMHFWV \RX OO EH FRPSDULQJ VXFK DV &RPSDUDWRU6WULQJ! RU &RPSDUDWRU3RLQW! $V \RX UHFDOO IURP WKH LPSOHPHQWDWLRQ RI WKH &RPSDUDEOH LQWHUIDFH ZH VKRXOG UHWXUQ D QHJDWLYH QXPEHU LI WKH ILUVW SDUDPHWHU FRPHV HDUOLHU LQ RUGHU WKDQ WKH VHFRQG LI WKH\ DUH HTXDO DQG D SRVLWLYH QXPEHU LI WKH ILUVW SDUDPHWHU FRPHV ODWHU WKDQ WKH VHFRQG 5HFDOO WKDW LPSOHPHQWLQJ &RPSDUDEOH LQ &KDSWHU ZH VDZ WKDW D FRPSDUH7R PHWKRG H[DPLQLQJ LQWHJHU GDWD FDQ RIWHQ VLPSO\ VXEWUDFW RQH QXPEHU IURP WKH RWKHU DQG UHWXUQ WKH UHVXOW 2XU FRPSDUH PHWKRG LV DOVR EDVHG RQ WKLV LGHD 1 2 3 4 5 6 7 8 import java.util.*; // Compares String objects by length. public class LengthComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.length() - s2.length(); } } 1RZ WKDW ZH YH ZULWWHQ D OHQJWK FRPSDUDWRU ZH FDQ SDVV RQH ZKHQ VRUWLQJ DQ DUUD\ RU OLVW RI 6WULQJ REMHFWV 659 Arrays.sort(strings, new LengthComparator()); System.out.println(Arrays.toString(strings)); +HUH V WKH RXWSXW ZKHQ XVHG RQ WKH VDPH 6WULQJ DUUD\ IURP HDUOLHU LQ WKLV VHFWLRQ 1RWLFH WKDW WKH VWULQJV DSSHDU LQ RUGHU RI LQFUHDVLQJ OHQJWK [echo, golf, alpha, bravo, hotel, DELTA, Foxtrot, Charlie] 6RPHWLPHV \RX ZDQW WR VHDUFK RU VRUW D FROOHFWLRQ RI REMHFWV WKDW GRQ W LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH )RU H[DPSOH WKH 3RLQW FODVV GRHVQ W LPSOHPHQW &RPSDUDEOH EXW \RX PLJKW ZLVK WR VRUW DQ DUUD\ RI 3RLQW REMHFWV E\ [ FRRUGLQDWH EUHDNLQJ WLHV E\ \ FRRUGLQDWH $Q H[DPSOH &RPSDUDWRU WKDW FRPSDUHV 3RLQW REMHFWV LQ WKLV ZD\ LV WKH IROORZLQJ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.awt.*; import java.util.*; // Compares Point objects by x coordinate and then by y coordinate. public class PointComparator implements Comparator<Point> { public int compare(Point p1, Point p2) { int dx = p1.x - p2.x; int dy = p1.y - p2.y; if (dx < 0 || (dx == 0 && dy < 0)) { return -1; } else if (dx > 0 || (dx == 0 && dy > 0)) { return 1; } else { return 0; } } } 7KH IROORZLQJ VKRUWHU YHUVLRQ XVLQJ WKH FRPSDUH7R VXEWUDFWLRQ WULFN DOVR ZRUNV public int compare(Point p1, Point p2) { int dx = p1.x - p2.x; int dy = p1.y - p2.y; if (dx == 0) { return dy; } else { return dx; } } )RU H[DPSOH WKH IROORZLQJ FRGH XVHV RXU 3RLQW&RPSDUDWRU WR VRUW DQ DUUD\ RI IRXU 3RLQW REMHFWV Point points = { new Point(4, -2), new Point(-1, 15), new Point(3, 7), new Point(0, 9) }; Arrays.sort(points, new PointComparator()); $IWHU WKLV FRGH WKH SRLQWV DSSHDU LQ WKH IROORZLQJ RUGHU 660 (-1, 15), (0, 9), (3, 7), (4, -2) 7KH IROORZLQJ WDEOH VXPPDUL]HV VHYHUDO XVHIXO SODFHV &RPSDUDWRUV DSSHDU LQ WKH -DYD FODVV OLEUDULHV 8VHIXO &RPSDUDWRU )XQFWLRQDOLW\ LQ -DYD V &ODVV /LEUDULHV &RPSDUDWRU 0HWKRG Arrays.binarySearch(array, value, comparator) 'HVFULSWLRQ 5HWXUQV WKH LQGH[ RI WKH JLYHQ YDOXH LQ WKH JLYHQ DUUD\ DVVXPLQJ WKDW WKH DUUD\ LV FXUUHQWO\ VRUWHG LQ WKH FRPSDUDWRU V RUGHU 6RUWV WKH JLYHQ DUUD\ LQ WKH RUGHULQJ RI WKH JLYHQ FRPSDUDWRU 5HWXUQV WKH LQGH[ RI WKH JLYHQ YDOXH LQ WKH JLYHQ OLVW DVVXPLQJ WKDW WKH OLVW LV FXUUHQWO\ VRUWHG LQ WKH RUGHULQJ RI WKH JLYHQ FRPSDUDWRU 5HWXUQV WKH ODUJHVW YDOXH RI WKH FROOHFWLRQ DFFRUGLQJ WR WKH RUGHULQJ RI WKH JLYHQ FRPSDUDWRU 5HWXUQV WKH VPDOOHVW YDOXH RI WKH FROOHFWLRQ DFFRUGLQJ WR WKH RUGHULQJ RI WKH JLYHQ FRPSDUDWRU 6RUWV WKH JLYHQ OLVW LQ WKH RUGHULQJ RI WKH JLYHQ FRPSDUDWRU Arrays.sort(array, comparator) Collections.binarySearch(list, value, comparator) Collections.max(collection, comparator) Collections.min(collection, comparator) Collections.sort(list, comparator) &RPSDUDWRUV FDQ DOVR EH XVHG ZLWK YDULRXV FROOHFWLRQV WKDW XVH HOHPHQW RUGHULQJ VXFK DV 7UHH6HW RU 3ULRULW\4XHXH 3URJUDP (IILFLHQF\ $V ZH SURJUHVV LQ WKLV WH[WERRN WKH SURJUDPV ZH UH ZULWLQJ DUH JHWWLQJ PRUH FRPSOH[ :H UH DOVR VHHLQJ WKDW WKHUH DUH PDQ\ ZD\V WR VROYH WKH VDPH SUREOHP +RZ GR ZH FRPSDUH GLIIHUHQW VROXWLRQV WR WKH VDPH SUREOHP WR VHH ZKLFK LV EHWWHU" $OVR ZH VD\ WKDW DQ DOJRULWKP WKDW VROYHV D SUREOHP TXLFNO\ DQG FOHYHUO\ LV HIILFLHQW %XW KRZ IDVW LV IDVW HQRXJK" $Q DOJRULWKP WKDW ORRNV XS D ZRUG LQ GLFWLRQDU\ DQG WDNHV PLQXWHV WR UXQ LV SUREDEO\ WRR VORZ %XW DQ DOJRULWKP WKDW UHQGHUV D FRPSOH[ GLPHQVLRQDO DQLPDWLRQ LQ PLQXWHV LV SUREDEO\ YHU\ IDVW 7KH DFFHSWDEOH SHUIRUPDQFH IRU D JLYHQ SURJUDP GHSHQGV RQ ZKDW WKH SURJUDP LV GRLQJ 6R KRZ GR ZH JDXJH FRPSXWLQJ SHUIRUPDQFH" 2QH ZD\ LV WR ZULWH WKH SURJUDP UXQ LW DQG PHDVXUH KRZ ORQJ LW WDNHV WR UXQ 7KLV LV VRPHWLPHV FDOOHG DQ HPSLULFDO DQDO\VLV RI WKH DOJRULWKP )RU H[DPSOH ZKLFK LV WKH IDVWHU SURJUDP WR VHDUFK DQ DUUD\ RQH WKDW VHTXHQWLDOO\ VHDUFKHV IRU WKH GHVLUHG WDUJHW HOHPHQW RU RQH WKDW ILUVW VRUWV WKH DUUD\ WKHQ SHUIRUPV D ELQDU\ VHDUFK RQ WKH VRUWHG DUUD\" :H FRXOG HPSLULFDOO\ DQDO\]H LW E\ ZULWLQJ ERWK SURJUDPV UXQQLQJ WKHP DQG WLPLQJ WKHP WR GHWHUPLQH ZKLFK SHUIRUPV EHWWHU 661 %XW HPSLULFDOO\ DQDO\]LQJ DQ DOJRULWKP LVQ W D YHU\ UHOLDEOH PHDVXUH EHFDXVH RQ D GLIIHUHQW FRPSXWHU ZLWK GLIIHUHQW SURFHVVRU VSHHG DQG PHPRU\ WKH SURJUDP PD\ QRW UXQ LQ WKH VDPH DPRXQW RI WLPH $OVR WR HPSLULFDOO\ WHVW DQ DOJRULWKP ZH PXVW ZULWH LW DQG WLPH LW ZKLFK FDQ EH D FKRUH $ PRUH QHXWUDO ZD\ WR PHDVXUH D SURJUDP V SHUIRUPDQFH LV WR H[DPLQH LWV FRGH RU SVHXGRFRGH DQG FRXQW URXJKO\ KRZ PDQ\ VWDWHPHQWV DUH H[HFXWHG 7KLV LV D OLJKW YHUVLRQ RI ZKDW V FDOOHG DOJRULWKP DQDO\VLV ZKLFK FRQVLVWV RI WHFKQLTXHV WR PDWKHPDWLFDOO\ SUHGLFW DQG FRPSXWH WKH SHUIRUPDQFH RI YDULRXV FRPSXWLQJ DOJRULWKPV ,W V KDUG WR NQRZ H[DFWO\ KRZ WR FRXQW KRZ PDQ\ VWDWHPHQWV D SLHFH RI FRGH H[HFXWHV EHFDXVH QRW DOO VWDWHPHQWV UHTXLUH WKH VDPH DPRXQW RI WLPH WR H[HFXWH )RU H[DPSOH PXOWLSOLFDWLRQ LV VORZHU WKDQ DGGLWLRQ IRU D &38 WR FRPSXWH DQG D PHWKRG FDOO LV JHQHUDOO\ VORZHU WKDQ HYDOXDWLQJ WKH ERROHDQ WHVW RI DQ LI HOVH VWDWHPHQW %XW IRU SXUSRVHV RI VLPSOLILFDWLRQ OHW V FRQVLGHU WKH IROORZLQJ DFWLRQV DV UHTXLULQJ H[DFWO\ XQLW RI WLPH WR H[HFXWH YDULDEOH GHFODUDWLRQV DQG DVVLJQPHQWV HYDOXDWLQJ PDWKHPDWLFDO DQG ORJLFDO H[SUHVVLRQV DFFHVVLQJ RU PRGLI\LQJ DQ LQGLYLGXDO HOHPHQW RI DQ DUUD\ VRPH PHWKRG FDOOV 7KH TXHVWLRQ DULVHV RI KRZ WR JDXJH WKH UXQWLPH RI PXOWLSOH VWDWHPHQWV SODFHG WRJHWKHU 6WDWHPHQWV SODFHG LQ VHTXHQWLDO RUGHU FDXVH WKHLU UXQWLPHV WR EH DGGHG WRJHWKHU statement1 statement2 statement3 \ > 3 / $ ORRS WKDW UHSHDWV 1 WLPHV H[HFXWHV URXJKO\ 1 WLPHV WKH QXPEHU RI VWDWHPHQWV LQ WKH ERG\ for (N times) { statement1 statement2 statement3 } \ \ > 3N / / $ SLHFH RI FRGH ZLWK ORRSV SODFHG VHTXHQWLDOO\ ZLWK RWKHU VWDWHPHQWV LQFOXGLQJ RWKHU ORRSV VHHV WKH UXQWLPHV RI WKRVH VWDWHPHQWV DGGHG WRJHWKHU statement1 for (N times) { statement2 } for (M times) { statement3 } statement4 statement5 \ \ > N / \ > M / / \ \ \ > M + N + 3 / / / / :KHQ WZR ORRSV DUH QHVWHG WKH UXQWLPH RI WKH LQQHU ORRS LV PXOWLSOLHG E\ WKH QXPEHU RI UHSHWLWLRQV RI WKH RXWHU ORRS 662 for (M times) { for (N times) { statement1 statement2 statement3 } } \ \ > 3 / / / / / \ \ > 3N / \ \ > 3MN 1RUPDOO\ WKH ORRSV LQ RXU ORQJ UXQQLQJ DOJRULWKPV DUH SURFHVVLQJ VRPH VRUW RI GDWD 0DQ\ DOJRULWKPV UXQ YHU\ TXLFNO\ LI WKH LQSXW GDWD VL]H LV VPDOO VR ZH JHQHUDOO\ RQO\ ZRUU\ DERXW WKH SHUIRUPDQFH IRU ODUJH GDWD VL]HV &RQVLGHU WKH IROORZLQJ ORRSV WKDW H[DPLQH DQ DUUD\ RI 1 HOHPHQWV for (N times) { for (N times) { statement1 } } for (N times) { statement2 statement3 } \ \ > N2 / / \ > 2N / / / \ \ \ \ > N2 + 2N / / / / :KHQ DQDO\]LQJ FRGH OLNH WKLV ZH RIWHQ WKLQN DERXW ZKLFK OLQH LV WKH PRVW IUHTXHQWO\ H[HFXWHG LQ WKH FRGH )RU ODUJH YDOXHV RI 1 WKH ILUVW 1 ORRS H[HFXWHV LWV VWDWHPHQW IDU PRUH WLPHV WKDQ WKH VHFRQG 1 ORRS H[HFXWHV LWV WZR VWDWHPHQWV )RU H[DPSOH LI 1 LV VWDWHPHQW H[HFXWHV RU WLPHV ZKLOH VWDWHPHQW DQG VWDWHPHQW HDFK H[HFXWH RQO\ WLPHV ,Q RWKHU ZRUGV ZKHQ WZR EORFNV RI FRGH DSSHDU LQ VHTXHQWLDO RUGHU WKH EORFN UDLVHG WR WKH KLJKHU SRZHU RI 1 GRPLQDWHV WKH RYHUDOO UXQWLPH 6WDWHPHQW LV WKH PRVW IUHTXHQWO\ H[HFXWHG LQ WKH SUHFHGLQJ FRGH ,Q IDFW ZKHQ SHUIRUPLQJ DOJRULWKP DQDO\VLV ZH RIWHQ LJQRUH DOO EXW WKH PRVW IUHTXHQWO\ H[HFXWHG SDUW RI WKH FRGH EHFDXVH WKH RWKHU SDUWV ZLOO JHQHUDOO\ EH RXWZHLJKHG E\ LWV UXQWLPH ,W ZRXOG EH 1 DOJRULWKP FRPPRQ WR UHIHU WR WKH SUHFHGLQJ FRGH DV DQ 1 DOJRULWKP UDWKHU WKDQ DQ 1 EHFDXVH WKH 1 GRHVQ W PDWWHU YHU\ PXFK LI 1 LV ODUJH HQRXJK :H OO UHYLVLW WKLV LGHD ODWHU LQ WKH FKDSWHU $ NH\ FRQFHSW WR WDNH DZD\ IURP DOJRULWKP DQDO\VLV LV KRZ H[SHQVLYH LW LV WR SHUIRUP QHVWHG ORRSV RYHU ODUJH LQSXW GDWD $OJRULWKPV WKDW PDNH PDQ\ SDVVHV HVSHFLDOO\ QHVWHG ORRSV RYHU D YHU\ ODUJH DUUD\ RU FROOHFWLRQ ZLOO VHH SRRU SHUIRUPDQFH VR LW V LPSRUWDQW WR FRPH XS ZLWK HIILFLHQW DOJRULWKPV WKDW GRQ W H[DPLQH WKH GDWD QHHGOHVVO\ :H OO QRZ VHH WKHVH LGHDV LQ DFWLRQ DV ZH REVHUYH WKH UXQWLPHV RI VRPH DFWXDO DOJRULWKPV WR VROYH D SURJUDPPLQJ SUREOHP RQ ODUJH GDWD VHWV $OJRULWKP 5XQWLPHV &RQVLGHU WKH WDVN RI FRPSXWLQJ WKH UDQJH RI QXPEHUV LQ DQ DUUD\ 7KH UDQJH LV RQH PRUH WKDQ WKH ODUJHVW GLIIHUHQFH EHWZHHQ DQ\ SDLU RI HOHPHQWV LQ WKH DUUD\ $Q LQLWLDO VROXWLRQ PLJKW XVH QHVWHG ORRSV WR H[DPLQH HYHU\ SDLU RI HOHPHQWV LQ WKH DUUD\ FRPSXWLQJ WKHLU GLIIHUHQFH DQG UHPHPEHULQJ WKH ODUJHVW GLIIHUHQFH IRXQG 663 max = 0. for each index i, for each index j, if elements [i] and [j] differ by more than max, update max. 7KH IROORZLQJ FRGH LPSOHPHQWV WKH UDQJH PHWKRG DV GHVFULEHG // Returns the range of numbers in the given array. public static int range(int numbers) { int maxDiff = 0; for (int i = 1; i < numbers.length; i++) { for (int j = 1; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]) + 1; maxDiff = Math.max(maxDiff, diff); } } return maxDiff; } 6LQFH WKH FRGH KDV WZR IRU ORRSV WKDW HDFK SURFHVV WKH HQWLUH DUUD\ ZH FDQ K\SRWKHVL]H WKDW WKH DOJRULWKP H[HFXWHV URXJKO\ Q VWDWHPHQWV RU VRPH PXOWLSOH WKHUHRI :H FDQ PHDVXUH WKH VSHHG RI WKLV UDQJH DOJRULWKP LQ PLOOLVHFRQGV E\ FDOOLQJ UDQJH RQ YDULRXV DUUD\V DQG PHDVXULQJ WKH WLPH HODSVHG 7KH WLPLQJ LV GRQH E\ DFTXLULQJ WKH FXUUHQW WLPH IRXQG XVLQJ D PHWKRG QDPHG System.currentTimeMillis EHIRUH DQG DIWHU FDOOLQJ UDQJH RQ D ODUJH DUUD\ DQG VXEWUDFWLQJ WKH HQG WLPH IURP WKH VWDUW WLPH n 1000 2000 4000 8000 16000 32000 64000 Range 219 423 396 194 381 891 41549 Runtime (ms) 15 47 203 781 3110 12563 49937 1RWLFH WKDW DV WKH LQSXW VL]H 1 GRXEOHV WKH UXQWLPH RI WKH UDQJH PHWKRG DSSUR[LPDWHO\ TXDGUXSOHV 7KLV LV FRQVLVWHQW ZLWK RXU K\SRWKHVLV ,I WKH DOJRULWKP WDNHV Q VWDWHPHQWV WR UXQ DQG ZH LQFUHDVH WKH LQSXW VL]H WR Q WKH QHZ UXQWLPH LV URXJKO\ WKH RULJLQDO UXQWLPH Q RU Q ZKLFK LV WLPHV DV ODUJH DV 2XU FRGH LVQ W YHU\ HIILFLHQW IRU D ODUJH DUUD\ ,W QHHGHG RYHU VHFRQGV WR H[DPLQH LQWHJHUV ,Q UHDO ZRUOG GDWD SURFHVVLQJ VLWXDWLRQV ZH ZRXOG H[SHFW WR VHH IDU ODUJHU LQSXW GDWD WKDQ WKLV 7KH UXQWLPH LVQ W DFFHSWDEOH IRU JHQHUDO XVH /RRNLQJ DW D SLHFH RI FRGH DQG WU\LQJ WR VSHHG LW XS FDQ EH GHFHSWLYHO\ GLIILFXOW ,W V WHPSWLQJ WR ZDQW WR WZHDN WKH FRGH E\ ORRNLQJ DW HDFK OLQH DQG WU\LQJ WR UHGXFH WKH DPRXQW RI FRPSXWDWLRQ LW SHUIRUPV )RU H[DPSOH \RX PD\ KDYH QRWLFHG WKDW RXU UDQJH PHWKRG DFWXDOO\ H[DPLQHV HYHU\ SDLU RI HOHPHQWV LQ WKH DUUD\ WZLFH )RU XQLTXH LQWHJHUV L DQG M ZH H[DPLQH WKH SDLU QXPEHUV>L@ 664 QXPEHUV>M@ DV ZHOO DV WKH SDLU QXPEHUV>M@ QXPEHUV>L@ :H FDQ SHUIRUP D WZHDN RI RXU UDQJH PHWKRG FRGH E\ VWDUWLQJ HDFK LQQHU M ORRS DKHDG RI L VR WKDW ZH ZRQ W H[DPLQH DQ\ SDLU L M ZLWK L ! M // Returns the largest of all integers in the given array. public static int range2(int numbers) { int maxDiff = 0; for (int i = 1; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]) + 1; maxDiff = Math.max(maxDiff, diff); } } return maxDiff; } 6LQFH DERXW KDOI RI WKH SRVVLEOH SDLUV RI L M YDOXHV DUH HOLPLQDWHG E\ WKLV WZHDN ZH G KRSH WKDW WKH FRGH ZRXOG UXQ DERXW WZLFH DV IDVW 7KH IROORZLQJ WLPLQJ WHVW VKRZV LWV DFWXDO UXQWLPH n 1000 2000 4000 8000 16000 32000 64000 Range 598 122 540 548 5910 1582 24051 Runtime (ms) 16 16 110 406 1578 6265 25031 $V ZH HVWLPDWHG WKH VHFRQG YHUVLRQ LV DERXW WZLFH DV IDVW DV WKH ILUVW ,Q IDFW WKHUH DUH RWKHU PLQRU WZHDNV ZH FRXOG SOD\ ZLWK VXFK DV UHSODFLQJ WKH 0DWK PD[ FDOO ZLWK D VLPSOH LI WHVW ZKLFK VSHHGV XS WKH DOJRULWKP E\ DURXQG PRUH %XW WKHUH V D PRUH LPSRUWDQW SRLQW WR EH PDGH :KHQ WKH LQSXW VL]H GRXEOHV WKH UXQWLPH RI HLWKHU DOJRULWKP URXJKO\ TXDGUXSOHV 7KH HIIHFW RI WKLV LV WKDW UHJDUGOHVV RI ZKLFK YHUVLRQ ZH XVH LI WKH LQSXW DUUD\ LV YHU\ ODUJH WKH PHWKRG ZLOO EH WRR VORZ 5DWKHU WKDQ WU\LQJ WR IXUWKHU WZHDN RXU QHVWHG ORRS VROXWLRQ OHW V WU\ WR WKLQN RI D VPDUWHU DOJRULWKP DOWRJHWKHU 7KH UDQJH RI YDOXHV LQ DQ DUUD\ LV UHDOO\ MXVW RQH PRUH WKDQ WKH GLIIHUHQFH EHWZHHQ WKH DUUD\ V ODUJHVW DQG VPDOOHVW HOHPHQW :H GRQ W UHDOO\ QHHG WR H[DPLQH DOO SDLUV RI YDOXHV RQO\ WKH SDLU UHSUHVHQWLQJ WKH ODUJHVW YDOXH DQG VPDOOHVW YDOXH :H FDQ GLVFRYHU ERWK RI WKHVH YDOXHV LQ D VLQJOH ORRS RYHU WKH DUUD\ 7KH IROORZLQJ QHZ DOJRULWKP GHPRQVWUDWHV WKLV LGHD // Returns the largest of all integers in the given array. public static int range3(int numbers) { int max = numbers[0]; int min = max; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } else if (numbers[i] < min) { min = numbers[i]; } } return max - min + 1; } 665 6LQFH WKLV DOJRULWKP RQO\ SDVVHV RYHU WKH DUUD\ RQFH ZH G KRSH WKDW LWV UXQWLPH LV RQO\ SURSRUWLRQDO WR WKH DUUD\ V OHQJWK ,I WKH DUUD\ OHQJWK GRXEOHV WKH UXQWLPH VKRXOG GRXEOH QRW TXDGUXSOH 7KH IROORZLQJ WLPLQJ WHVW VKRZV LWV DFWXDO UXQWLPH n 1000 2000 4000 8000 16000 32000 64000 128000 256000 512000 1024000 2048000 4096000 8192000 16384000 32768000 65536000 131072000 262144000 Range 121 152 1739 1431 548 16534 16458 26128 43257 78586 62718 501992 3228947 621097 2309851 7856008 39748656 7021645 51030232 Runtime (ms) 0 0 0 0 0 0 0 0 0 0 0 16 31 47 94 188 453 797 1578 2XU UXQWLPH DSSUR[LPDWLRQV ZHUH URXJKO\ FRUUHFW $V WKH VL]H RI WKH DUUD\ GRXEOHV WKH UXQWLPH RI WKLV QHZ UDQJH DOJRULWKP URXJKO\ GRXEOH DV ZHOO 7KH RYHUDOO UXQWLPH RI WKLV DOJRULWKP LV PXFK EHWWHU ZH FDQ H[DPLQH RYHU D KXQGUHG PLOOLRQ LQWHJHUV LQ XQGHU RQH VHFRQG 7KHUH DUH D IHZ YHU\ LPSRUWDQW REVHUYDWLRQV WR WDNH DZD\ IURP WKH SUHFHGLQJ DOJRULWKPV DQG WKHLU UHVSHFWLYH UXQWLPHV 7ZHDNLQJ DQ DOJRULWKP V FRGH RIWHQ LVQ W DV SRZHUIXO DQ RSWLPL]DWLRQ DV ILQGLQJ D EHWWHU DOJRULWKP $Q DOJRULWKP V UDWH RI JURZWK RU WKH DPRXQW LWV UXQWLPH LQFUHDVHV ZKHQ WKH LQSXW GDWD VL]H LQFUHDVHV LV RQH RI WKH PRVW LPSRUWDQW PHDVXUHV RI WKH HIILFLHQF\ RI WKH DOJRULWKP 7KH LGHD RI JURZWK UDWH LV VLPLODU WR ORRNLQJ RQO\ DW WKH PRVW IUHTXHQWO\ H[HFXWHG OLQH RI FRGH LQ DQ DOJRULWKP DQDO\VLV DQG LV DQ LPSRUWDQW LGHD ZKHQ ZH ZLVK WR FDWHJRUL]H DOJRULWKPV E\ HIILFLHQF\ &RPSOH[LW\ &ODVVHV :H FDWHJRUL]H UDWHV RI JURZWK EDVHG RQ WKHLU SURSRUWLRQ WR WKH LQSXW GDWD VL]H 1 :H FDOO WKHVH FDWHJRULHV FRPSOH[LW\ FODVVHV &RPSOH[LW\ &ODVV $ FDWHJRU\ RI DOJRULWKP HIILFLHQF\ EDVHG XSRQ LWV UHODWLRQVKLS WR WKH LQSXW GDWD VL]H 666 7KH FRPSOH[LW\ FODVV RI D SLHFH RI FRGH LV GHWHUPLQHG E\ ORRNLQJ DW WKH PRVW IUHTXHQWO\ H[HFXWHG OLQH RI FRGH GHWHUPLQLQJ WKH QXPEHU RI WLPHV LW LV H[HFXWHG DQG H[WUDFWLQJ WKH KLJKHVW SRZHU RI 1 )RU H[DPSOH LI WKH PRVW IUHTXHQW OLQH H[HFXWHV 1 1 WLPHV ZH VD\ WKDW WKH DOJRULWKP LV LQ WKH RUGHU 1 FRPSOH[LW\ FODVV RU 2 1 IRU VKRUW 7KH VKRUWKDQG QRWDWLRQ ZLWK WKH FDSLWDO 2 LV FDOOHG ELJ 2K QRWDWLRQ DQG LV XVHG FRPPRQO\ LQ DOJRULWKP DQDO\VLV +HUH DUH VRPH RI WKH PRVW FRPPRQ FRPSOH[LW\ FODVVHV OLVWHG LQ RUGHU IURP VORZHVW WR IDVWHVW JURZWK IURP PRVW WR OHDVW HIILFLHQW &RQVWDQW WLPH RU 2 DOJRULWKPV DUH WKRVH ZKRVH UXQWLPH GRHVQ W GHSHQG RQ DQ\ LQSXW VL]H 2QH VXFK DOJRULWKP LV WKH FRGH WR UHDG DQ DPRXQW RI SHQQLHV IURP WKH XVHU DQG UHSRUW KRZ PDQ\ TXDUWHUV FDQ EH PDGH IURP WKRVH SHQQLHV /RJDULWKPLF DOJRULWKPV RU 2 ORJ 1 IRU VKRUW GLYLGH D SUREOHP VSDFH UHSHDWHGO\ LQ KDOI XQWLO LW LV VROYHG $Q H[DPSOH ORJDULWKPLF WLPH DOJRULWKP LV RQH ZKHUH WKH XVHU WKLQNV RI D QXPEHU EHWZHHQ DQG DQG WKH FRPSXWHU WULHV WR JXHVV LW JLYLQJ WKH FRPSXWHU D KLQW HDFK WLPH DERXW ZKHWKHU LWV JXHVV ZDV WRR KLJK RU WRR ORZ 2Q HDFK JXHVV WKH FRPSXWHU FDQ JXHVV WKH PLGGOH QXPEHU RI LWV FXUUHQW UDQJH DQG FXW WKH UDQJH LQ KDOI EDVHG RQ ZKHWKHU LW ZDV WRR KLJK RU WRR ORZ /LQHDU RUGHU 1 DOJRULWKPV RU 2 1 IRU VKRUW DUH WKRVH ZKRVH UXQWLPH LV GLUHFWO\ FRUUHODWHG WR 1 RQH ZKRVH UXQWLPH URXJKO\ GRXEOHV ZKHQ 1 GRXEOHV 7KH ODVW YHUVLRQ RI WKH UDQJH PHWKRG LQ WKH SUHYLRXV VHFWLRQ LV D OLQHDU DOJRULWKP 3HUIRUPLQJ D ORJDULWKPLF RSHUDWLRQ RYHU HYHU\ HOHPHQW RI DQ LQSXW GDWDVHW RI VL]H 1 OHDGV WR DQ DOJRULWKPLF UXQWLPH RI 2 1 ORJ 1 VRPHWLPHV FDOOHG ORJ OLQHDU DOJRULWKPV 4XDGUDWLF RU 2 1 DOJRULWKPV DUH WKRVH ZKRVH UXQWLPH URXJKO\ TXDGUXSOHV ZKHQ 1 GRXEOHV 7KH LQLWLDO YHUVLRQV RI WKH UDQJH DOJRULWKP ZHUH TXDGUDWLF DOJRULWKPV &XELF RU 2 1 DOJRULWKPV JHQHUDOO\ PDNH WKUHH OHYHOV RI QHVWHG SDVVHV RYHU WKH LQSXW GDWD 7KH WDVN RI FRXQWLQJ WKH QXPEHU RI FROLQHDU WULRV RI SRLQWV LQ D ODUJH 3RLQW DUUD\ ZRXOG EH D FXELF DOJRULWKP ([SRQHQWLDO DOJRULWKPV UHTXLUH URXJKO\ 1 RSHUDWLRQV WR SHUIRUP 2QH H[DPSOH LV WKH WDVN RI SULQWLQJ WKH SRZHU VHW RI D GDWD VHW ZKLFK LV WKH VHW RI DOO SRVVLEOH VXEVHWV RI WKH GDWD ([SRQHQWLDO DOJRULWKPV DUH VR VORZ WKDW WKH\ VKRXOG RQO\ EH H[HFXWHG RQ VPDOO LQSXW GDWD VL]HV 7KH IROORZLQJ WDEOH SUHVHQWV VHYHUDO K\SRWKHWLFDO DOJRULWKP UXQWLPHV DV DQ LQSXW VL]H 1 JURZV 1RWLFH WKDW HYHQ LI WKH\ DOO VWDUW DW WKH VDPH UXQWLPH IRU D VPDOO LQSXW VL]H WKH RQHV LQ KLJKHU FRPSOH[LW\ FODVVHV TXLFNO\ EHFRPH VR VORZ DV WR EH LPSUDFWLFDO 667 LQSXW VL]H 1 2 PV PV PV PV PV PV 2 ORJ 1 PV PV PV PV PV PV 21 PV PV PV PV VHF VHF 2 1 ORJ 1 PV PV PV VHF VHF VHF 21 PV PV VHF VHF VHF PLQ VHF 21 PV PV VHF VHF PLQ VHF PLQ VHF 2 1 PV VHF GD\V PLOOLRQ \HDUV \HDUV \HDUV )RU ODUJH GDWD VHWV LW V YHU\ LPSRUWDQW WR FKRRVH WKH PRVW HIILFLHQW DOJRULWKPV SRVVLEOH LQ WHUPV RI FRPSOH[LW\ FODVV ,I \RX NQRZ WKH DOJRULWKP \RX ZDQW WR UXQ LV 2 1 RU ZRUVH \RX SUREDEO\ VKRXOGQ W FDOO LW RQ H[WUHPHO\ ODUJH GDWD VHWV :H OO QRZ H[DPLQH DOJRULWKPV WR VHDUFK DQG VRUW GDWD NHHSLQJ WKLV OHVVRQ LQ PLQG ,PSOHPHQWLQJ 6HDUFKLQJ $OJRULWKPV ,Q WKLV VHFWLRQ ZH OO LPSOHPHQW PHWKRGV WKDW VHDUFK IRU DQ LQWHJHU LQ DQ DUUD\ RI LQWHJHUV DQG UHWXUQ WKH LQGH[ ZKHUH LW ZDV IRXQG ,I WKH LQWHJHU GRHVQ W DSSHDU LQ WKH DUUD\ ZH OO UHWXUQ D QHJDWLYH QXPEHU :H OO H[DPLQH WZR PDMRU VHDUFKLQJ DOJRULWKPV VHTXHQWLDO DQG ELQDU\ VHDUFK DQG GLVFXVV WKH WUDGHRIIV EHWZHHQ WKHP 6HTXHQWLDO 6HDUFK 3HUKDSV WKH VLPSOHVW ZD\ WR VHDUFK DQ DUUD\ LV WR ORRS RYHU WKH HOHPHQWV RI WKH DUUD\ DQG FKHFN HDFK RQH WR VHH LI LW LV WKH WDUJHW QXPEHU $V PHQWLRQHG HDUOLHU WKLV LV FDOOHG D VHTXHQWLDO VHDUFK EHFDXVH LW H[DPLQHV HYHU\ HOHPHQW LQ VHTXHQFH 7KH FRGH IRU D VHTXHQWLDO VHDUFK LV UHODWLYHO\ VWUDLJKWIRUZDUG XVLQJ D IRU ORRS :H OO UHWXUQ IRU ORRS FRPSOHWHV ZLWKRXW ILQGLQJ WKH WDUJHW QXPEHU // Sequential search algorithm. // Returns the index at which the given target number first // appears in the given input array, or -1 if it is not found. public static int indexOf(int numbers, int target) { for (int i = 0; i < numbers.length; i++) { if (numbers[i] == target) { return i; // found it! } } return -1; } // not found LI WKH 668 %DVHG RQ WKH UXOHV VWDWHG LQ WKH SUHYLRXV VHFWLRQ ZH SUHGLFW WKDW WKH VHTXHQWLDO VHDUFK DOJRULWKP LV D OLQHDU 2 1 DOJRULWKP EHFDXVH LW FRQWDLQV RQH ORRS WKDW WUDYHUVHV DW PRVW 1 HOHPHQWV LQ DQ DUUD\ :H VD\ DW PRVW EHFDXVH LI WKH DOJRULWKP ILQGV WKH WDUJHW HOHPHQW LW VWRSV DQG LPPHGLDWHO\ UHWXUQV 1H[W ZH OO WLPH LW WR YHULI\ RXU SUHGLFWLRQ 7KH IROORZLQJ UXQWLPH WDEOH VKRZV DFWXDO UHVXOWV RI UXQQLQJ WKH VHTXHQWLDO DOJRULWKP RQ UDQGRPO\ JHQHUDWHG DUUD\V RI LQWHJHUV n 400000 800000 1600000 3200000 6400000 12800000 25600000 51200000 102400000 204800000 Index 10789 328075 249007 1410489 2458760 5234632 7695239 5096538 8938751 29198530 Runtime 0 0 0 15 16 37 32 45 72 125 Index -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Runtime 0 0 16 16 31 47 93 203 422 875 :KHQ VHDUFKLQJ IRU DQ LQWHJHU WKDW LVQ W LQ WKH DUUD\ WKH DOJRULWKP UXQV VRPHZKDW VORZHU EHFDXVH LW FDQ W EDLO RXW HDUO\ XSRQ ILQGLQJ WKH WDUJHW 7KLV UDLVHV WKH TXHVWLRQ RI ZKHWKHU ZH VKRXOG MXGJH WKH DOJRULWKP E\ LWV IDVWHVW RU VORZHVW UXQWLPH 2IWHQ ZH FDUH PRVW DERXW WKH H[SHFWHG EHKDYLRU ZKHQ IHG D UDQGRP LQSXW RIWHQ FDOOHG DQ DYHUDJH FDVH DQDO\VLV %XW LQ FHUWDLQ FRQGLWLRQV ZH DOVR FDUH DERXW WKH IDVWHVW SRVVLEOH RXWFRPH WKH EHVW FDVH DQDO\VLV DQG RU WKH VORZHVW SRVVLEOH RXWFRPH WKH ZRUVW FDVH DQDO\VLV %LQDU\ 6HDUFK &RQVLGHU D PRGLILHG YHUVLRQ RI WKH VHDUFKLQJ SUREOHP ZKHUH ZH FDQ DVVXPH WKDW WKH HOHPHQWV RI WKH LQSXW DUUD\ DUH LQ VRUWHG RUGHU 'RHV WKLV DIIHFW RXU DOJRULWKP" 2XU H[LVWLQJ DOJRULWKP ZLOO VWLOO ZRUN FRUUHFWO\ EXW QRZ ZH FDQ DFWXDOO\ VWRS VHDUFKLQJ LI ZH HYHU JHW WR D QXPEHU ODUJHU WKDQ RXU WDUJHW ZLWKRXW ILQGLQJ WKH WDUJHW )RU H[DPSOH LI ZH UH VHDUFKLQJ WKH DUUD\ [1, 4, 5, 7, 7, 9, 10, 12, 56] IRU WKH WDUJHW QXPEHU ZH FDQ VWRS VHDUFKLQJ RQFH ZH VHH WKH ,W PLJKW VHHP WKDW VXFK D PRGLILFDWLRQ WR RXU VHTXHQWLDO VHDUFK DOJRULWKP ZRXOG OHDG WR D VLJQLILFDQW VSHHGXS %XW LQ DFWXDOLW\ LW GRHVQ W PDNH PXFK GLIIHUHQFH 7KH RQO\ FDVH LQ ZKLFK LW VSHHGV XS WKH DOJRULWKP QRWLFHDEO\ LV ZKHQ VHDUFKLQJ IRU D UHODWLYHO\ VPDOO YDOXH WKDW LVQ W IRXQG LQ WKH DUUD\ ,Q IDFW ZKHQ VHDUFKLQJ IRU D ODUJH YDOXH RQH WKDW UHTXLUHV WKH FRGH WR H[DPLQH PRVW RU DOO RI WKH DUUD\ HOHPHQWV WKH PRGLILHG DOJRULWKP DFWXDOO\ SHUIRUPV PRUH VORZO\ WKDQ WKH RULJLQDO EHFDXVH LW KDV WR SHUIRUP D IHZ PRUH ERROHDQ WHVWV 0RVW LPSRUWDQWO\ WKH DOJRULWKP LV VWLOO 2 1 ZKLFK UXQV UHDVRQDEO\ TXLFNO\ EXW LVQ W WKH RSWLPDO VROXWLRQ 2QFH DJDLQ DQ DOJRULWKP WZHDN GRHVQ W PDNH DV PXFK GLIIHUHQFH DV ILQGLQJ D VPDUWHU DOJRULWKP LQ WKH ILUVW SODFH ,I ZH NQRZ WKDW RXU LQSXW DUUD\ LV LQ VRUWHG RUGHU VHTXHQWLDO VHDUFK LVQ W WKH EHVW FKRLFH :RXOG \RX ORRN XS VRPHRQH V SKRQH QXPEHU LQ WKH SKRQH ERRN E\ WXUQLQJ WR WKH ILUVW SDJH WKHQ WKH VHFRQG DQG VR RQ XQWLO \RX IRXQG WKH SKRQH QXPEHU" 2I FRXUVH QRW <RX NQRZ WKDW WKH SDJHV DUH VRUWHG E\ QDPH VR \RX G IOLS RSHQ WKH ERRN WR VRPHZKHUH QHDU WKH PLGGOH RI WKH ERRN WKHQ QDUURZ GRZQ WRZDUG WKH ILUVW OHWWHU RI WKH SHUVRQ \RX ZHUH ORRNLQJ IRU 669 7KH ELQDU\ VHDUFK DOJRULWKP GLVFXVVHG SUHYLRXVO\ LQ WKLV FKDSWHU WDNHV DGYDQWDJH RI WKH VRUWHGQHVV RI WKH DUUD\ %LQDU\ VHDUFK NHHSV WUDFN RI WKH UDQJH RI WKH DUUD\ WKDW LV FXUUHQWO\ RI LQWHUHVW ,QLWLDOO\ WKLV UDQJH LV WKH ZKROH DUUD\ 7KH DOJRULWKP UHSHDWHGO\ H[DPLQHV WKH FHQWHU HOHPHQW RI WKH DUUD\ DQG XVHV WKLV WR HOLPLQDWH KDOI RI WKH UDQJH RI LQWHUHVW ,I WKH FHQWHU HOHPHQW LV VPDOOHU WKDQ WKH WDUJHW ZH UH VHDUFKLQJ IRU ZH HOLPLQDWH WKH ORZHU KDOI RI WKH UDQJH ,I WKH FHQWHU HOHPHQW LV ODUJHU WKDQ WKH WDUJHW ZH HOLPLQDWH WKH XSSHU KDOI :H FDQ LPSOHPHQW WKH SUHFHGLQJ LGHD E\ NHHSLQJ WUDFN RI WKUHH LQGH[HV WKDW ZLOO FKDQJH DV RXU DOJRULWKP UXQV WKH PLGGOH LQGH[ WKH PLQLPXP LQGH[ WR H[DPLQH DQG WKH PD[LPXP LQGH[ ,I ZH H[DPLQH WKH PLGGOH HOHPHQW DQG LW V WRR VPDOO ZH ZLOO HOLPLQDWH DOO HOHPHQWV EHWZHHQ PLG DQG PLQ IURP FRQVLGHUDWLRQ ,I WKH PLGGOH HOHPHQW LV WRR ODUJH ZH ZLOO HOLPLQDWH DOO HOHPHQWV EHWZHHQ PLQ DQG PD[ IURP FRQVLGHUDWLRQ &RQVLGHU WKH IROORZLQJ DUUD\ int numbers = {11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98}; /HW V ELQDU\ VHDUFK WKH DUUD\ IRU D WDUJHW YDOXH RI :H OO VWDUW DW WKH PLGGOH HOHPHQW ZKLFK LV WKH RQH DW LQGH[ VL]H RU RU 7KH IROORZLQJ GLDJUDPV VKRZ WKH PLQ PLG DQG PD[ DW HDFK VWHS RI WKH DOJRULWKP index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 element [11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98] ^ ^ ^ step 1 min mid max numbers[mid] is too small; move right. index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 element [11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98] ^ ^ ^ step 2 min mid max numbers[mid] is too large; move left. index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 element [11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98] ^ ^ ^ step 3 min mid max numbers[mid] is too small; move right. index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 element [11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98] ^ step 4 min mid max found it! 670 :KDW DERXW ZKHQ VHDUFKLQJ IRU DQ HOHPHQW WKDW LVQ W IRXQG LQ WKH DUUD\" ,PDJLQH WKDW ZH G EHHQ VHDUFKLQJ IRU WKH YDOXH LQVWHDG RI 7KH VWHSV RI WKH DOJRULWKP ZRXOG KDYH EHHQ WKH VDPH H[FHSW RQ WKH ODVW IRXUWK SDVV ZH ZRXOG KDYH VHHQ DQG QRW WKH GHVLUHG 7KH DOJRULWKP ZRXOG NQRZ WKDW LW VKRXOG VWRS RQFH LW V WULPPHG LWV UDQJH GRZQ WR D VLQJOH HOHPHQW DQG WKDW HOHPHQW LV QRW WKH WDUJHW $QRWKHU ZD\ WR GHVFULEH LW LV WR ORRS XQWLO WKH PLQ DQG PD[ KDYH FURVVHG HDFK RWKHU 7KH IROORZLQJ FRGH LPSOHPHQWV WKH ELQDU\ VHDUFK DOJRULWKP DV GHVFULEHG ,WV ORRS UHSHDWV XQWLO WKH WDUJHW LV IRXQG RU XQWLO WKH PLQ DQG PD[ KDYH FURVVHG // Binary search algorithm. // Returns the index at which the given target number first // appears in the given input array, or -1 if it is not found. // pre: array is sorted. public static int binarySearch(int numbers, int target) { int min = 0; int max = numbers.length - 1; while (min <= max) { int mid = (max + min) / 2; System.out.println(mid + 1); if (numbers[mid] == target) { return mid; // found it! } else if (numbers[mid] < target) { min = mid + 1; // too small } else { // numbers[mid] > target max = mid - 1; // too large } mid = (max + min) / 2; } return -1; } // not found 7KH IROORZLQJ UXQWLPH WDEOH VKRZV DFWXDO UHVXOWV RI UXQQLQJ WKH ELQDU\ VHDUFK DOJRULWKP RQ UDQGRPO\ JHQHUDWHG DUUD\V RI LQWHJHUV n 400000 800000 1600000 3200000 6400000 12800000 25600000 51200000 102400000 204800000 Index 15268 377855 787480 1267940 764109 2593634 5294376 13978599 44963064 8317859 Runtime 0 0 0 0 0 0 0 0 0 0 Index -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Runtime 0 0 0 0 0 0 0 0 0 0 7KH UHVXOWV DUH DOPRVW VKRFNLQJ %LQDU\ VHDUFK LV VR IDVW LW URXQGV GRZQ WR ]HUR ZKHQ PHDVXUHG E\ WKH FRPSXWHU V FORFN :KLOH WKLV LV DQ LPSUHVVLYH UHVXOW LW PDNHV LW KDUGHU IRU XV WR HPSLULFDOO\ H[DPLQH WKH UXQWLPH :KDW LV WKH FRPSOH[LW\ FODVV RI WKH ELQDU\ VHDUFK DOJRULWKP" 671 7KH IDFW WKDW LW ILQLVKHV VR TXLFNO\ PLJKW WHPSW XV WR FRQFOXGH WKDW WKH DOJRULWKP LV FRQVWDQW WLPH RU 2 %XW LW GRHVQ W VHHP ULJKW WKDW D PHWKRG ZLWK D ORRS LQ LW ZRXOG WDNH D FRQVWDQW DPRXQW RI WLPH ,Q DFWXDOLW\ WKH UXQWLPH LV UHODWHG WR WKH LQSXW VL]H EHFDXVH WKH ODUJHU WKH LQSXW WKH PRUH WLPHV ZH PXVW FKRS RXU PLQ PD[ UDQJH LQ KDOI WR DUULYH D D VLQJOH HOHPHQW :H FRXOG PDNH WKH IROORZLQJ VWDWHPHQW DERXW WKH QXPEHU RI UHSHWLWLRQV DV UHODWHG WR WKH LQSXW VL]H 1 UHSHWLWLRQV a 1 RI ERWK VLGHV RI WKH HTXDWLRQ 8VLQJ VRPH DOJHEUD DQG WDNLQJ D ORJDULWKP EDVH UHSHWLWLRQV a ORJ 1 :H FRQFOXGH WKDW WKH ELQDU\ VHDUFK DOJRULWKP LV LQ WKH ORJDULWKPLF FRPSOH[LW\ FODVV RU 2 ORJ 1 %LQDU\ VHDUFK DOVR GRHVQ W KDYH YHU\ PXFK GLIIHUHQFH EHWZHHQ LWV EHVW DQG ZRUVW FDVHV LQ WKH EHVW FDVH LW OXFNV RXW DQG ILQGV LWV WDUJHW YDOXH LQ WKH PLGGOH RQ WKH ILUVW FKHFN ,Q WKH ZRUVW FDVH LW PXVW SHUIRUP WKH IXOO ORJ 1 FRPSDULVRQV %XW VLQFH ORJV DUH VPDOO QXPEHUV WKH ORJ RI LV URXJKO\ WKLV LV QRW PXFK RI D KLW 7KH IROORZLQJ LV DQ DOWHUQDWLYH UHFXUVLYH YHUVLRQ RI WKH ELQDU\ VHDUFK XVLQJ UHFXUVLRQ FRQFHSWV DV SUHVHQWHG LQ &KDSWHU // Recursive binary search algorithm. // Returns the index at which the given target number first // appears in the given input array, or -1 if it is not found. // pre: array is sorted. public static int binarySearchR(int numbers, int target) { return binarySearchR(numbers, target, 0, numbers.length - 1); } public static int binarySearchR(int numbers, int target, int min, int max) { // base case if (min > max) { return -1; // not found } else { // recursive case int mid = (max + min) / 2; if (numbers[mid] == target) { return mid; } else if (numbers[mid] < target) { return binarySearchR(numbers, target, mid + 1, max); } else { return binarySearchR(numbers, target, min, mid - 1); } } } 6RPH IRONV GRQ W OLNH UHFXUVLYH YHUVLRQV RI PHWKRGV OLNH ELQDU\ VHDUFK EHFDXVH WKHUH LV D QRQ UHFXUVLYH VROXWLRQ WKDW V IDLUO\ HDV\ WR ZULWH DQG EHFDXVH WKH\ KDYH KHDUG WKDW UHFXUVLRQ KDV SRRU UXQWLPH SHUIRUPDQFH :KLOH LW V WUXH WKDW UHFXUVLRQ FDQ EH D ELW VORZ EHFDXVH RI WKH H[WUD PHWKRG FDOOV LW JHQHUDWHV WKDW GRHVQ W SRVH D SUREOHP KHUH 7KH UXQWLPH RI WKH UHFXUVLYH YHUVLRQ LV FHUWDLQO\ 672 VWLOO 2 ORJ 1 EHFDXVH LW V HVVHQWLDOO\ SHUIRUPLQJ WKH VDPH FRPSXWDWLRQ LW V VWLOO FXWWLQJ WKH LQSXW LQ KDOI DW HDFK VWHS ,Q IDFW WKH UHFXUVLYH YHUVLRQ LV VWLOO IDVW HQRXJK WKDW WKH FRPSXWHU VWLOO FDQ W WLPH LW DFFXUDWHO\ LW SURGXFHV D UXQWLPH RI PV HYHQ RQ DUUD\V RI WHQV RI PLOOLRQV RI LQWHJHUV ,Q JHQHUDO DQDO\]LQJ WKH UXQWLPH RI UHFXUVLYH DOJRULWKPV LV WULFN\ DQG UHTXLUHV XQGHUVWDQGLQJ RI D WHFKQLTXH FDOOHG UHFXUUHQFH UHODWLRQV 7KDW V D WRSLF IRU D ODWHU FRXUVH WKDW ZRQ W EH FRYHUHG LQ WKLV WH[WERRN 'LG \RX .QRZ 6RPH 4XLUNV RI %LQDU\ 6HDUFK 7KHUH DUH D IHZ LQWHUHVWLQJ WKLQJV ZH GLGQ W PHQWLRQ \HW DERXW ELQDU\ VHDUFK SDUWLFXODUO\ 6XQ V LPSOHPHQWDWLRQ IRXQG LQ $UUD\V ELQDU\6HDUFK DQG &ROOHFWLRQV ELQDU\6HDUFK 7DNH D ORRN DW WKLV WH[W IURP WKH -DYDGRF GRFXPHQWDWLRQ RI 6XQ V ELQDU\6HDUFK PHWKRG 7KH DUUD\ PXVW EH VRUWHG DV E\ WKH VRUW PHWKRG DERYH SULRU WR PDNLQJ WKLV FDOO ,I LW LV QRW VRUWHG WKH UHVXOWV DUH XQGHILQHG ,I WKH DUUD\ FRQWDLQV PXOWLSOH HOHPHQWV ZLWK WKH VSHFLILHG YDOXH WKHUH LV QR JXDUDQWHH ZKLFK RQH ZLOO EH IRXQG %LQDU\ VHDUFK GHSHQGV RQ WKH DUUD\ EHLQJ VRUWHG ,I LW LVQ W VRUWHG 6XQ VD\V WKH UHVXOWV DUH XQGHILQHG :KDW GRHV WKDW PHDQ" :K\ GRHVQ W 6XQ MXVW VRUW WKH DUUD\ IRU \RX LI LW V XQVRUWHG" 7KHUH DUH WZR SUREOHPV ZLWK WKDW LGHD ERWK HVVHQWLDOO\ UHODWHG WR UXQWLPH SHUIRUPDQFH )RU RQH VRUWLQJ WDNHV PXFK ORQJHU 2 1 ORJ 1 WLPH WKDQ ELQDU\ VHDUFKLQJ 2 ORJ 1 WLPH 6HFRQG WR HYHQ GLVFRYHU WKDW RQH QHHGV WR VHDUFK UHTXLUHV ORRNLQJ DW HDFK HOHPHQW WR HQVXUH WKDW WKH\ UH LQ RUGHU ZKLFK WDNHV 2 1 WLPH (VVHQWLDOO\ WKH FRVW ZRXOG EH WRR JUHDW WR H[DPLQH WKH DUUD\ DQG VRUW LW LI QHFHVVDU\ (YHQ LI WKH FRVW ZHUHQ W VR ODUJH WR VRUW WKH DUUD\ WKH FOLHQW RI WKH ELQDU\6HDUFK PHWKRG SUREDEO\ GRHVQ W ZLVK WR KDYH WKHLU DUUD\ PHVVHG ZLWK E\ WKH ELQDU\6HDUFK PHWKRG 6HDUFKLQJ LV VXSSRVHG WR EH D UHDG RQO\ RSHUDWLRQ QRW RQH WKDW UHDUUDQJHV WKH DUUD\ /HW V ORRN DW WKH RWKHU SDUW RI WKH SUHYLRXV TXRWH ,I WKH DUUD\ FRQWDLQV PXOWLSOH HOHPHQWV ZLWK WKH VSHFLILHG YDOXH WKHUH LV QR JXDUDQWHH ZKLFK RQH ZLOO EH IRXQG ,W V VRPHWKLQJ ZH GLGQ W PHQWLRQ HDUOLHU ZKHQ GLVFXVVLQJ WKH DOJRULWKP EXW LQ WKH FDVH RI GXSOLFDWHV ELQDU\ VHDUFK LVQ W JXDUDQWHHG WR ILQG WKH ILUVW RFFXUUHQFH RI WKH HOHPHQW EHFDXVH WKH PRPHQW LW ]HURHV LQ RQ DQ RFFXUUHQFH LW VWRSV +HUH V DQRWKHU LQWHUHVWLQJ EOXUE IURP WKH ELQDU\6HDUFK GRFXPHQWDWLRQ 5HWXUQV LQGH[ RI WKH VHDUFK NH\ LI LW LV FRQWDLQHG LQ WKH OLVW RWKHUZLVH LQVHUWLRQ SRLQW 7KH LQVHUWLRQ SRLQW LV GHILQHG DV WKH SRLQW DW ZKLFK WKH NH\ ZRXOG EH LQVHUWHG LQWR WKH OLVW WKH LQGH[ RI WKH ILUVW HOHPHQW JUHDWHU WKDQ WKH NH\ RU OLVW VL]H LI DOO HOHPHQWV LQ WKH OLVW DUH OHVV WKDQ WKH VSHFLILHG NH\ 1RWH WKDW WKLV JXDUDQWHHV WKDW WKH UHWXUQ YDOXH ZLOO EH ! LI DQG RQO\ LI WKH NH\ LV IRXQG 5DWKHU WKDQ UHWXUQLQJ RQ DQ XQVXFFHVVIXO VHDUFK 6XQ V ELQDU\6HDUFK UHWXUQV D QHJDWLYH QXPEHU UHODWHG WR WKH VSRW ZKHUH WKH HOHPHQW ZRXOG KDYH EHHQ LI LW KDG EHHQ LQ WKH DUUD\ )RU H[DPSOH FDOOLQJ ELQDU\6HDUFK IRU WKH YDOXH RQ WKH IROORZLQJ DUUD\ ZRXOG UHWXUQ EHFDXVH LV WKH LQGH[ ZKHUH ZRXOG KDYH DSSHDUHG [11, 18, 29, 37, 42, 49, 51, 63, 69, 72, 77, 82, 88, 91, 98] 673 7KH UHDVRQ 6XQ UHWXUQV WKLV YDOXH LQVWHDG RI LV EHFDXVH LI \RX UH PDLQWDLQLQJ D VRUWHG DUUD\ RU OLVW DQG ZDQW WR DGG WR LW LQ WKH SURSHU LQGH[ WR UHWDLQ VRUWHG RUGHU \RX FDQ FDOO ELQDU\6HDUFK JHW WKH QHJDWLYH UHVXOW QHJDWH DQG VXEWUDFW IURP LW DQG YRLOD \RX YH JRW WKH LQGH[ DW ZKLFK \RX VKRXOG LQVHUW 7KLV LV PXFK IDVWHU WKDQ OLQHDUO\ VHDUFKLQJ IRU WKH SODFH WR DGG WKH YDOXH RU DGGLQJ WKH YDOXH DW WKH HQG DQG UH VRUWLQJ WKH DUUD\ :H FRXOG PRGLI\ RXU RZQ ELQDU\ VHDUFK FRGH WR PDWFK 6XQ V EHKDYLRU E\ FKDQJLQJ WKH ODVW OLQH RI WKH PHWKRG V ERG\ WR WKH IROORZLQJ return -min - 1; // not found 6HDUFKLQJ 2EMHFWV 6HDUFKLQJ IRU D SDUWLFXODU REMHFW LQ DQ DUUD\ RI REMHFWV UHTXLUHV D IHZ PRGLILFDWLRQV WR RXU VHDUFKLQJ FRGH IURP WKH SUHYLRXV VHFWLRQV /HW V ORRN DW D VHTXHQWLDO REMHFW VHDUFK ILUVW EHFDXVH LW ZLOO ZRUN ZLWK DQ\ W\SH RI REMHFWV 7KH 0RVW LPSRUWDQW PRGLILFDWLRQ WR PDNH WR WKH FRGH LV WKDW LW PXVW XVH WKH HTXDOV PHWKRG 7KH RSHUDWRU GRHVQ W FRUUHFWO\ FRPSDUH REMHFWV VR WKH FDOO RQ HTXDOV WHVWV DQ DUUD\ HOHPHQW IRU HTXDOLW\ ZLWK WKH WDUJHW // Sequential search algorithm. // Returns the index at which the given target number first // appears in the given input array, or -1 if it is not found. public static int indexOf(Object objects, Object target) { for (int i = 0; i < objects.length; i++) { if (objects[i].equals(target)) { return i; // found it! } } return -1; } // not found ,I ZH G OLNH WR GR D ELQDU\ VHDUFK RQ REMHFWV WKH DUUD\ RU FROOHFWLRQ PXVW FRQVLVW RI REMHFWV WKDW KDYH D QDWXUDO RUGHULQJ ,Q RWKHU ZRUGV WKH REMHFWV PXVW LPSOHPHQW WKH &RPSDUDEOH LQWHUIDFH 2QH FRPPRQ H[DPSOH ZRXOG EH DQ DUUD\ RI 6WULQJV 6LQFH ZH FDQ W XVH UHODWLRQDO RSHUDWRUV OLNH DQG ! RQ REMHFWV ZH PXVW FDOO WKH FRPSDUH7R PHWKRG RQ SDLUV RI 6WULQJ REMHFWV DQG H[DPLQH LWV UHWXUQ YDOXH 7KH IROORZLQJ FRGH LPSOHPHQWV WKH ELQDU\ VHDUFK RQ DQ DUUD\ RI 6WULQJV 674 // Binary search algorithm that works with String objects. // Returns the index at which the given target String // appears in the given input array, or -1 if it is not found. // pre: array is sorted. public static int binarySearch(String strings, String target) { int min = 0; int max = strings.length - 1; while (min <= max) { int mid = (max + min) / 2; int compare = strings[mid].compareTo(target); if (compare == 0) { return mid; // found it! } else if (compare < 0) { min = mid + 1; // too small } else { // compare > 0 max = mid - 1; // too large } } return -1; } // not found ,PSOHPHQWLQJ 6RUWLQJ $OJRULWKPV :KLOH WKHUH DUH OLWHUDOO\ KXQGUHGV RI DOJRULWKPV WR VRUW GDWD ZH OO FRYHU WZR LQ GHWDLO LQ WKLV FKDSWHU 7KH ILUVW LV RQH RI WKH PRVW LQWXLWLYH WKRXJK SRRUO\ SHUIRUPLQJ DOJRULWKPV 7KH VHFRQG LV RQH RI WKH IDVWHVW VRUWLQJ DOJRULWKPV XVHG LQ SUDFWLFH WRGD\ 7KH FRGH ZH OO ZULWH LQ WKLV VHFWLRQ ZLOO RQO\ VRUW DUUD\V RI LQWHJHU YDOXHV LQWV EXW LW FRXOG EH DGDSWHG WR VRUW &RPSDUDEOH REMHFWV VXFK DV 6WULQJV 7KH LGHDV EHKLQG VXFK D FRQYHUVLRQ DUH WKH VDPH DV FRYHUHG LQ WKH SUHFHGLQJ VHFWLRQ RQ VHDUFKLQJ REMHFWV VXFK DV XVLQJ WKH FRPSDUH7R DQG HTXDOV PHWKRGV WR FRPSDUH REMHFWV UDWKHU WKDQ UHODWLRQDO RSHUDWRUV OLNH DQG ! 6HOHFWLRQ 6RUW 7KH VHOHFWLRQ VRUW DOJRULWKP LV D ZHOO NQRZQ VRUWLQJ WHFKQLTXH WKDW PDNHV PDQ\ SDVVHV RYHU DQ LQSXW DUUD\ WR SXW LWV HOHPHQWV LQWR VRUWHG RUGHU (DFK WLPH WKURXJK D ORRS WKH VPDOOHVW YDOXH LV VHOHFWHG DQG SXW LQ WKH SURSHU SODFH QHDU WKH IURQW RI WKH DUUD\ *LYHQ WKLV DUUD\ int nums = {12, 123, 1, 28, 93, 16}; [0] [1] [2] [3] [4] [5] +-------+-------+-------+-------+-------+-------+ | 12 | 123 | 1 | 28 | 93 | 16 | +-------+-------+-------+-------+-------+-------+ +RZ GR \RX SXW WKLV DUUD\ LQWR RUGHU IURP VPDOOHVW WR ODUJHVW" 2QH VWHS ZRXOG EH WR VFDQ WKH DUUD\ DQG ILQG WKH VPDOOHVW QXPEHU ,Q WKLV OLVW WKH VPDOOHVW LV QXPV> @ ZKLFK HTXDOV <RX ZRXOG EH FORVHU WR \RXU JRDO LI \RX ZHUH WR VZDS QXPV> @ DQG QXPV> @ 675 [0] [1] [2] [3] [4] [5] +-------+-------+-------+-------+-------+-------+ | | 123 | 12 | 28 | 93 | 16 | 1 +-------+-------+-------+-------+-------+-------+ 7KH QXPV> @ QRZ KDV WKH YDOXH LW VKRXOG DQG \RX RQO\ KDYH WR RUGHU WKH HOHPHQWV > @ WKURXJK > @ <RX FDQ UHSHDW WKLV DOJRULWKP VFDQQLQJ WKH UHPDLQLQJ ILYH HOHPHQWV IRU WKH VPDOOHVW QXPV> @ ZKLFK HTXDOV DQG VZDSSLQJ LW ZLWK QXPV> @ [0] [1] [2] [3] [4] [5] +-------+-------+-------+-------+-------+-------+ | 1 | 12 | 123 | 28 | 93 | 16 | +-------+-------+-------+-------+-------+-------+ 1RZ QXPV> @ DQG QXPV> @ KDYH WKH FRUUHFW YDOXHV <RX FDQ FRQWLQXH IRU QXPV> @ VFDQQLQJ WKH UHPDLQLQJ IRXU HOHPHQWV IRU WKH VPDOOHVW QXPV> @ ZKLFK HTXDOV DQG VZDSSLQJ LW ZLWK QXPV> @ [0] [1] [2] [3] [4] [5] +-------+-------+-------+-------+-------+-------+ | 1 | 12 | 16 | 28 | 93 | 123 | +-------+-------+-------+-------+-------+-------+ 7KLV JLYHV \RX WKH FRUUHFW YDOXHV IRU QXPV> @ QXPV> @ DQG QXPV> @ <RX FDQ FRQWLQXH WKLV SURFHVV XQWLO DOO HOHPHQWV KDYH WKH SURSHU YDOXHV +HUH LV DQ DWWHPSW DW D SVHXGRFRGH GHVFULSWLRQ RI WKH DOJRULWKP scan elements [0] through [5] for the smallest value. swap element [0] with the smallest element found in the scan. scan elements [1] through [5] for the smallest value. swap element [1] with the smallest element found in the scan. scan elements [2] through [5] for the smallest value. swap element [2] with the smallest element found in the scan. scan elements [3] through [5] for the smallest value. swap element [3] with the smallest element found in the scan. scan elements [4] through [5] for the smallest value. swap element [4] with the smallest element found in the scan. scan elements [5] through [5] for the smallest value. swap element [5] with the smallest element found in the scan. 7KH DOJRULWKP LQYROYHV D VFDQ IROORZHG E\ D VZDS <RX UHSHDW WKH VFDQ VZDS ILYH WLPHV <RX GRQ W QHHG WR SHUIRUP D VL[WK VFDQ VZDS VLQFH WKH VL[WK HOHPHQW ZLOO DXWRPDWLFDOO\ KDYH WKH FRUUHFW YDOXH LI WKH ILUVW ILYH KDYH WKH FRUUHFW YDOXHV 7KLV SVHXGRFRGH LV FOXPV\ EHFDXVH LW GRHVQ W WDNH LQWR DFFRXQW WKH REYLRXV ORRSLQJ WKDW LV JRLQJ RQ +HUH LV D EHWWHU DSSUR[LPDWLRQ for i from 0 to 4: scan elements [i] through [5] for the smallest value. swap element [i] with the smallest element found in the scan. <RX FDQ SHUIRUP WKH VFDQ ZLWK WKH IROORZLQJ 676 smallest = (lowest array index of interest). for (all other index values of interest): if (value at [index]) < (value at [smallest]), smallest = index. <RX FDQ LQFRUSRUDWH WKLV LQWR \RXU ODUJHU SVHXGRFRGH for i from 0 to 4: smallest = i. for index going (i + 1) to 5 do if element [index] < element [smallest], smallest = index. swap element [i] with element [smallest]. 7KLV SVHXGRFRGH LV DOPRVW GLUHFWO\ WUDQVODWDEOH LQWR -DYD H[FHSW IRU WKH VZDS SURFHVV )RU WKLV \RX VKRXOG XVH WKH IROORZLQJ PHWKRG // Swaps a[i] with a[j]. public static void swap(int a, int i, int j) { if (i != j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } 1RZ \RX FDQ ZULWH WKH PHWKRG SURSHUO\ // Places the elements of the given array into sorted order // using the selection sort algorithm. // post: array is in sorted (nondecreasing) order public static void selectionSort(int a) { for (int i = 0; i < a.length - 1; i++) { // find index of smallest element int smallest = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[smallest]) { smallest = j; } } swap(a, i, smallest); } } // swap smallest to front 6LQFH VHOHFWLRQ VRUW PDNHV 1 SDVVHV RYHU DQ DUUD\ RI 1 HOHPHQWV LWV SHUIRUPDQFH LV 2 1 7HFKQLFDOO\ LW H[DPLQHV URXJKO\ 1 1 1 HOHPHQWV 7KHUH V D PDWKHPDWLFDO LGHQWLW\ WKDW WKH VXP RI DOO LQWHJHUV IURP WR DQ\ PD[LPXP YDOXH 1 HTXDOV 1 1 ZKLFK LV MXVW RYHU 1 7KH IROORZLQJ HPSLULFDO UXQWLPH FKDUW FRQILUPV WKLV DQDO\VLV EHFDXVH WKH UXQWLPH TXDGUXSOHV HYHU\ WLPH WKH LQSXW VL]H 1 LV GRXEOHG ZKLFK LV FKDUDFWHULVWLF RI DQ 1 DOJRULWKP 677 n 1000 2000 4000 8000 16000 32000 64000 128000 256000 Runtime (ms) 0 16 47 234 657 2562 10265 41141 164985 0HUJH 6RUW 7KHUH DUH RWKHU DOJRULWKPV VLPLODU WR VHOHFWLRQ VRUW WKDW PDNH PDQ\ SDVVHV RYHU WKH DUUD\ DQG VZDS YDULRXV HOHPHQWV RQ HDFK SDVV )XQGDPHQWDOO\ DOJRULWKPV WKDW VZDS HOHPHQWV LQWR RUGHU LQ WKLV ZD\ DUH OLPLWHG WR 2 1 EDUULHU DYHUDJH UXQWLPH :H QHHG WR H[DPLQH D EHWWHU DOJRULWKP WKDW EUHDNV WKLV 7KH PHUJH VRUW DOJRULWKP GHULYHV IURP WKH REVHUYDWLRQ WKDW LI \RX KDYH WZR VRUWHG VXEDUUD\V \RX FDQ HDVLO\ PHUJH WKHP LQWR D VLQJOH VRUWHG DUUD\ )RU H[DPSOH LI \RX KDYH WKH IROORZLQJ DUUD\ int nums = {1, 3, 6, 7, 2, 4, 5, 8}; <RX FDQ WKLQN RI LW DV EHLQJ WZR KDOYHV HDFK RI ZKLFK KDSSHQV WR EH VRUWHG {1, 3, 6, 7, 2, 4, 5, 8} | | | | +--------+ +--------+ subarray subarray 7KH EDVLF LGHD RI WKH PHUJH VRUW DOJRULWKP LV mergeSort(array): if array is larger than 2 elements, split array into two halves. sort left half. sort right half. merge halves. /HW V ORRN DW VSOLWWLQJ WKH DUUD\ DQG PHUJLQJ KDOYHV ILUVW WKHQ ODVWO\ ZH OO WDON DERXW WKH VRUWLQJ 6SOLWWLQJ RQH DUUD\ LQWR LWV WZR KDOYHV LV UHODWLYHO\ VWUDLJKWIRUZDUG :H OO VHW D PLGSRLQW DW RI WKH OHQJWK RI WKH DUUD\ DQG FRQVLGHU HYHU\WKLQJ EHIRUH WKLV PLGSRLQW WR EH SDUW RI WKH OHIW KDOI DQG HYHU\WKLQJ DIWHU WR EH LQ WKH ULJKW KDOI +HUH DUH WZR TXLFN PHWKRGV IRU H[WUDFWLQJ WKH KDOYHV RXW RI DQ DUUD\ 678 // Returns the first half of the given array. public static int leftHalf(int array) { int size1 = array.length / 2; int left = new int[size1]; for (int i = 0; i < size1; i++) { left[i] = array[i]; } return left; } // Returns the second half of the given array. public static int rightHalf(int array) { int size1 = array.length / 2; int size2 = array.length - size1; int right = new int[size2]; for (int i = 0; i < size2; i++) { right[i] = array[i + size1]; } return right; } 1RZ OHW V SUHWHQG WKDW WKH KDOYHV DUH VRUWHG ZH OO FRPH EDFN WR KRZ WR VRUW WKHP ODWHU DQG ORRN DW KRZ ZH G PHUJH WZR VRUWHG VXEDUUD\V ,PDJLQH WKDW \RX KDYH WZR VWDFNV RI H[DP SDSHUV HDFK VRUWHG E\ QDPH DQG \RX QHHG WR FRPELQH WKHP LQWR D VLQJOH VWDFN VRUWHG E\ QDPH 7KH VLPSOHVW DOJRULWKP LV WR SODFH ERWK VWDFNV LQ IURQW RI \RX DQG ORRN DW WKH WRS SDSHU RI HDFK VWDFN <RX JUDE WKH SDSHU WKDW FRPHV ILUVW LQ DOSKDEHWLFDO RUGHU DQG SXW LW IDFH GRZQ LQWR \RXU PHUJHG SLOH 7KHQ \RX UHSHDW ORRNLQJ DW ERWK LQSXW VWDFNV DJDLQ XQWLO RQH VWDFN LV HPSW\ 2QFH RQH LV HPSW\ \RX MXVW JUDE WKH HQWLUH UHPDLQLQJ VWDFN DQG SODFH LW DW WKH HQG RI \RXU PHUJHG SLOH 7KH LGHD LV VLPLODU KHUH H[FHSW WKDW LQVWHDG RI SK\VLFDOO\ UHPRYLQJ SDSHUV LQWHJHUV IURP WKH SLOHV VXEDUUD\V VLQFH WKDW LV VORZ ZH OO NHHS DQ LQGH[ LQWR HDFK VXEDUUD\ DQG LQFUHPHQW WKDW LQGH[ DV ZH SURFHVV D JLYHQ HOHPHQW +HUH LV D SVHXGRFRGH GHVFULSWLRQ RI WKH PHUJLQJ DOJRULWKP left index i1 = 0. right index i2 = 0. for (number of elements in entire array): if left value at i1 is smaller than right value at i2, include value from left array in new array. increase i1 by one. else, include value from right array in new array. increase i2 by one. +HUH LV D WUDFH RI WKH HLJKW VWHSV LQYROYHG LQ PHUJLQJ WKH WZR VXEDUUD\V LQWR D QHZ VRUWHG DUUD\ 679 Subarrays Next Include Merged Array -----------------------------------------------------------{1, 3, 6, 7} {2, 4, 5, 8} 1 from left {1} ^ ^ i1 i2 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} ^ i1 {1, 3, 6, 7} {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 {2, 4, 5, 8} ^ i2 2 from right {1, 2} 3 from left {1, 2, 3} 4 from right {1, 2, 3, 4} 5 from right {1, 2, 3, 4, 5} 6 from left {1, 2, 3, 4, 5, 6} 7 from left {1, 2, 3, 4, 5, 6, 7} 8 from right {1, 2, 3, 4, 5, 6, 7, 8} $IWHU WKH VHYHQWK VWHS \RX KDYH LQFOXGHG DOO RI WKH HOHPHQWV LQ WKH OHIW DUUD\ VR WKH OHIW LQGH[ QR ORQJHU SRLQWV WR DQ\WKLQJ 7KLV FRPSOLFDWHV WKH SVHXGRFRGH VRPHZKDW EHFDXVH \RX KDYH WR GHDO ZLWK WKH VSHFLDO FDVH WKDW RQH RI WKH LQGH[HV PLJKW QRW SRLQW WR DQ\WKLQJ 7KH VLPSOH WHVW LQ WKH SVHXGRFRGH QHHGV WR EH H[SDQGHG if i2 has passed the end of the right array, or left element at i1 is smaller than right element at i2, take from left. else, take from right. 7KH DFWXDO FRGH WR LPSOHPHQW WKH PHUJLQJ EHKDYLRU LV WKH IROORZLQJ 680 // pre : result is empty; left/right are sorted // post: result contains the result of merging the sorted lists; // list1 is empty; list2 is empty public static void merge(int result, int left, int right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++) { if (i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) { result[i] = left[i1]; // take from left i1++; } else { result[i] = right[i2]; // take from right i2++; } } } 1RZ ZH YH ZULWWHQ WKH FRGH WR VSOLW DQ DUUD\ LQWR KDOYHV DQG WKH FRGH WR PHUJH VRUWHG KDOYHV LQWR D VRUWHG ZKROH %XW WKH KDOYHV DUHQ W VRUWHG KRZ GR ZH VRUW WKH KDOYHV" :H FRXOG FDOO VHOHFWLRQ VRUW RQ WKH KDOYHV EXW WKHUH V D PXFK EHWWHU WKLQJ ZH FDQ GR PHUJH VRUW WKHP :H FDQ UHFXUVLYHO\ FDOO RXU RZQ PHUJH VRUW PHWKRG RQ WKH KDOYHV DQG LI LW V ZULWWHQ FRUUHFWO\ LW OO SXW WKH KDOYHV LQWR VRUWHG RUGHU 2XU SVHXGRFRGH ZRXOG PRUH DFFXUDWHO\ EH ZULWWHQ DV WKH IROORZLQJ split array into two halves. mergeSort left half. mergeSort right half. merge halves. ,I ZH UH PDNLQJ RXU PHUJH VRUW DOJRULWKP UHFXUVLYH LW QHHGV D EDVH FDVH DQG D UHFXUVLYH FDVH 7KH DERYH SVHXGRFRGH VSHFLILHV WKH UHFXUVLYH FDVH %XW IRU WKH EDVH FDVH ZKDW DUH WKH VLPSOHVW DUUD\V WR VRUW" $Q DUUD\ ZLWK HLWKHU QR HOHPHQWV RU MXVW RQH HOHPHQW GRHVQ W QHHG WR EH VRUWHG DW DOO ,W WDNHV DW OHDVW WZR HOHPHQWV EHIRUH \RX FDQ JHW VRPHWKLQJ LQ WKH ZURQJ RUGHU VR WKH VLPSOH FDVHV DUH DUUD\V RI OHQJWK OHVV WKDQ +HUH LV D PHWKRG WKDW LPSOHPHQWV WKH FRPSOHWH PHUJH VRUW DOJRULWKP // Places the elements of the given array into sorted order // using the merge sort algorithm. // post: array is in sorted (nondecreasing) order public static void mergeSort(int array) { if (array.length > 1) { // split array into two halves int left = leftHalf(array); int right = rightHalf(array); // recursively sort the two halves mergeSort(left); mergeSort(right); // merge the sorted halves into a sorted whole merge(array, left, right); } } 681 +HUH LV D G\QDPLF VFRSH WUDFH RI ZKDW KDSSHQV ZKHQ \RX FDOO WKLV PHWKRG RQ DQ DUUD\ RI HLJKW HOHPHQWV int array = {3, 7, 6, 1, 5, 4, 8, 2}; mergeSort(array); {3, split split split merge merge merge {3, {3, 7, 7, 6, 6, 1, 5, 4, 4, 8, 8, 2} 2} 2} 1} {5, 1} {5, 7} {6, 4} {8, {3} {7} {6} {1} {5} {4} {8} {2} {3, {1, {1, 7} {1, 3, 2, 6, 3, 6} {4, 7} {2, 4, 5, 5} {2, 4, 6, 5, 7, 8} 8} 8} +HUH V D YLHZ RI DOO FDOOV WKDW DUH PDGH WR WKH PHUJH6RUW DQG PHUJH PHWKRGV LQ RUGHU mergeSort({3, 7, 6, 1, 5, 4, 8, 2}) mergeSort({3, 7, 6, 1}) mergeSort({3, 7}) mergeSort({3}) mergeSort({7}) merge({3}, {7}) mergeSort({6, 1}) mergeSort({6}) mergeSort({1}) merge({6}, {1}) merge({3, 7}, {1, 6}) mergeSort({5, 4, 8, 2}) mergeSort({5, 4}) mergeSort({5}) mergeSort({4}) merge({5}, {4}) mergeSort({8, 2}) mergeSort({8}) mergeSort({2}) merge({8}, {2}) merge({4, 5}, {2, 8}) merge({1, 3, 6, 7}, {2, 4, 5, 8}) 7KH IROORZLQJ WDEOH GHPRQVWUDWHV WKH SHUIRUPDQFH RI PHUJH VRUW 682 n 1000 2000 4000 8000 16000 32000 64000 128000 256000 512000 1024000 2048000 4096000 8192000 16384000 32768000 65536000 131072000 Runtime (ms) 0 0 0 0 0 15 16 47 125 250 532 1078 2265 4781 9828 20422 42406 88344 )LUVW RI DOO PHUJH VRUW V SHUIRUPDQFH LV PXFK EHWWHU WKDQ WKDW RI VHOHFWLRQ VRUW :KDW LV LWV FRPSOH[LW\ FODVV" ,W ORRNV DOPRVW OLNH DQ 2 1 DOJRULWKP EHFDXVH WKH UXQWLPH VOLJKWO\ PRUH WKDQ GRXEOHV ZKHQ ZH GRXEOH WKH DUUD\ VL]H ,W WXUQV RXW WKDW PHUJH VRUW LV DFWXDOO\ DQ 2 1 ORJ 1 DOJRULWKP )RU DQ DUUD\ RI VL]H Q ZH KDYH WR VSOLW LW LQ KDOI URXJKO\ ORJ 1 WLPHV LQ WKLV DOJRULWKP DQG DW HDFK VWHS ZH KDYH WR GR D OLQHDU RSHUDWLRQ RI RUGHU 1 PHUJLQJ WKH KDOYHV DIWHU WKH\ UH VRUWHG SURGXFHV D 2 1 ORJ 1 RYHUDOO UXQWLPH 0XOWLSO\LQJ WKHVH RSHUDWLRQV $V ZLWK RWKHU UHFXUVLYH DOJRULWKPV D SUHFLVH DQDO\VLV RI PHUJH VRUW V SHUIRUPDQFH LV FRPSOLFDWHG DQG UHTXLUHV PDWKHPDWLFDO WHFKQLTXHV VXFK DV UHFXUUHQFH UHODWLRQV ZKLFK ZRQ W EH GLVFXVVHG LQ WKLV WH[WERRN 2WKHU 6RUWLQJ $OJRULWKPV 7KRXJK ZH ZRQ W GLVFXVV WKHP LQ GHWDLO KHUH WKHUH DUH VHYHUDO RWKHU LPSRUWDQW VRUWLQJ DOJRULWKPV LQ FRPPRQ XVH WRGD\ 2QH VRUWLQJ DOJRULWKP QRW \HW PHQWLRQHG LV LQVHUWLRQ VRUW ZKLFK LV WKH DOJRULWKP ZH LQVWUXFWRUV RIWHQ XVH ZKHQ VRUWLQJ D VWDFN RI VWXGHQWV H[DP SDSHUV LQWR DOSKDEHWLFDO RUGHU :H KDYH D VRUWHG SLOH RI SDSHUV DQG FRQVLGHU WKH UHVW WR EH WKH XQVRUWHG SLOH :H UHSHDWHGO\ SOXFN WKH WRS SDSHU IURP WKH XQVRUWHG SLOH DQG LQVHUW LW LQWR LWV SURSHU SODFH LQ WKH VRUWHG SLOH ,QVHUWLRQ VRUW XVHV WKLV LGHD WR VOLGH HOHPHQWV LQWR SODFH LQ D JURZLQJ VRUWHG IURQW SRUWLRQ RI DQ DUUD\ RU OLVW ,QVHUWLRQ VRUW V DYHUDJH UXQWLPH LV 2 1 OLNH VHOHFWLRQ VRUW EXW LW LV VRPHZKDW IDVWHU RYHUDOO %HFDXVH RI WKLV LQVHUWLRQ VRUW LV RIWHQ XVHG LQ SODFH RI VHOHFWLRQ VRUW ZKHQ D EDVLF Q LV GHVLUHG $ QLFH IHDWXUH RI LQVHUWLRQ VRUW LV WKDW LW UXQV YHU\ TXLFNO\ LI \RX UXQ LW RQ DQ LQSXW DUUD\ WKDW LV 683 DOUHDG\ VRUWHG EHFDXVH QR HOHPHQWV QHHG WR EH PRYHG 2QH GUDZEDFN WR LQVHUWLRQ VRUW LV WKDW LW KDV D SRRU ZRUVW FDVH UXQWLPH ZKLFK RFFXUV ZKHQ WKH LQSXW DUUD\ LV LQ EDFNZDUGV RUGHU ,QVHUWLRQ VRUW V ZRUVW FDVH UXQWLPH LV ZRUVH WKDQ WKDW RI VHOHFWLRQ VRUW $QRWKHU YHU\ LPSRUWDQW DQG ZLGHO\ XVHG VRUWLQJ DOJRULWKP LV QDPHG TXLFNVRUW ,QYHQWHG E\ LQIOXHQWLDO %ULWLVK FRPSXWHU VFLHQWLVW & $ 5 +RDUH TXLFNVRUW LV DQ DOJRULWKP EDVHG RQ WKH LGHD RI UHSHDWHGO\ SDUWLWLRQLQJ DQ DUUD\ LQWR URXJK KDOYHV $ UHDO OLIH H[DPSOH RI SDUWLWLRQLQJ ZRXOG EH DQ LQVWUXFWRU ZKR KDV D ODUJH SLOH RI H[DP SDSHUV WR VRUW VR VKH FKRRVHV VRPH PLGSRLQW RI WKH DOSKDEHW DQG SODFHV DOO WKH VWXGHQWV ZLWK $ WKURXJK / QDPHV LQWR RQH SLOH DQG 0 WKURXJK = QDPHV LQWR DQRWKHU SLOH ,I WKHVH SLOHV DUH VWLOO WRR ELJ WR VRUW SHUKDSV WKH LQVWUXFWRU IXUWKHU SDUWLWLRQV WKH WZR SLOHV LQWR WZR VPDOOHU RQHV $ WKURXJK ) * WKURXJK / 0 WKURXJK 5 DQG 6 WKURXJK = $W VRPH SRLQW WKH SLOHV EHFRPH PDQDJHDEOH LQ VL]H DQG WKH WHDFKHU DQG RU DVVLVWDQWV FDQ VRUW HDFK SLOH WKHQ FRPELQH WKH SLOHV 4XLFNVRUW SDUWLWLRQV DQ DUUD\ E\ VHOHFWLQJ DQ HOHPHQW IURP WKH DUUD\ WR XVH DV LWV PLGSRLQW WKLV HOHPHQW LV RIWHQ FDOOHG WKH SLYRW 7KH RWKHU HOHPHQWV RI WKH DUUD\ DUH VSOLW LQWR WZR JURXSV RQH FRQWDLQLQJ DOO YDOXHV VPDOOHU WKDQ WKH SLYRW DQG WKH RWKHU FRQWDLQLQJ DOO HOHPHQWV JUHDWHU WKDQ WKH SLYRW 2QFH SDUWLWLRQLQJ LV FRPSOHWHG WKH OHIW DQG ULJKW KDOYHV DUH UHFXUVLYHO\ TXLFNVRUWHG $ EDVH FDVH FDQ XVH D VLPSOH VRUWLQJ DOJRULWKP OLNH LQVHUWLRQ VRUW WR VRUW D SLOH RI HOHPHQWV WKDW V VPDOOHU WKDQ VRPH WKUHVKROG VXFK DV PD\EH HOHPHQWV (YHQWXDOO\ WKH HQWLUH DUUD\ RI GDWD IDOOV LQWR VRUWHG RUGHU 4XLFNVRUW V DYHUDJH UXQWLPH LV 2 1 ORJ 1 OLNH PHUJH VRUW EXW LW LV IDVWHU RYHUDOO %HFDXVH RI WKLV TXLFNVRUW LV RIWHQ XVHG LQVWHDG RI PHUJH VRUW IRU WRS VSHHG 4XLFNVRUW DOVR GRHVQ W UHTXLUH D WHPSRUDU\ DUUD\ WR EH FUHDWHG ZKLFK FDQ VDYH D ORW RI PHPRU\ IRU ODUJH DUUD\V %XW TXLFNVRUW GRHV KDYH D IHZ GUDZEDFNV WKDW SUHYHQW LW IURP EHLQJ WKH FOHDU EHVW VRUWLQJ DOJRULWKP 2QH LV WKDW LI WKH SDUWLWLRQV DUH YHU\ XQHYHQ LQ VL]H LI WKH SLYRW LV RQH RI WKH VPDOOHVW RU ODUJHVW HOHPHQWV DQG WKHUHIRUH GRHVQ W GLYLGH WKH DUUD\ YHU\ HYHQO\ WKH VRUW UXQV OHVV HIILFLHQWO\ KDYLQJ D ZRUVW FDVH SHUIRUPDQFH RI 2 1 LQ YHU\ UDUH FDVHV $OVR TXLFNVRUW LV D FRPSOH[ DOJRULWKP WR ZULWH VR LW FDQ EH GLIILFXOW WR SURGXFH D ZRUNLQJ TXLFNVRUW SURJUDP ZLWKRXW DQ\ EXJV RU PLVWDNHV 7KH DXWKRUV FKRVH WR FRYHU VHOHFWLRQ DQG PHUJH VRUWV LQ WKLV WH[WERRN EHFDXVH WKH\ RIIHU IDLUO\ SUHGLFWDEOH UXQWLPH SHUIRUPDQFH DQG UHODWLYHO\ XQGHUVWDQGDEOH DOJRULWKP FRGH &KDSWHU 6XPPDU\ 6HDUFKLQJ LV WKH WDVN RI DWWHPSWLQJ WR ILQG D SDUWLFXODU WDUJHW YDOXH LQ D FROOHFWLRQ RU DUUD\ 6RUWLQJ LV WKH WDVN RI DUUDQJLQJ WKH HOHPHQWV RI D OLVW RU DUUD\ LQWR D QDWXUDO RUGHULQJ -DYD V FODVV OLEUDULHV FRQWDLQV VHYHUDO PHWKRGV IRU VHDUFKLQJ DQG VRUWLQJ DUUD\V DQG OLVWV VXFK DV $UUD\V ELQDU\6HDUFK DQG &ROOHFWLRQV VRUW 684 $ &RPSDUDWRU REMHFW GHVFULEHV FXVWRPL]HG ZD\V WR FRPSDUH REMHFWV VR WKDW DUUD\V RU OLVWV RI WKHVH REMHFWV FDQ EH VHDUFKHG DQG VRUWHG LQ PDQ\ RUGHUV (PSLULFDO DQDO\VLV LV WKH WHFKQLTXH RI UXQQLQJ D SURJUDP RU DOJRULWKP WR GHWHUPLQH LWV UXQWLPH $OJRULWKP DQDO\VLV LV WKH WHFKQLTXH RI H[DPLQLQJ DQ DOJRULWKP V FRGH RU SVHXGRFRGH WR PDNH LQIHUUHQFHV DERXW LV HIILFLHQF\ $OJRULWKPV DUH JURXSHG LQWR FRPSOH[LW\ FODVVHV ZKLFK DUH RIWHQ GHVFULEHG XVLQJ ELJ RK QRWDWLRQ VXFK DV 2 1 IRU D OLQHDU DOJRULWKP 6HTXHQWLDO VHDUFK LV D OLQHDU 2 1 VHDUFKLQJ DOJRULWKP WKDW ORRNV DW HYHU\ HOHPHQW RI D OLVW XQWLO WKH WDUJHW YDOXH LV IRXQG %LQDU\ VHDUFK LV D ORJDULWKPLF 2 ORJ 1 VHDUFKLQJ DOJRULWKP WKDW RSHUDWHV RQ D VRUWHG GDWDVHW DQG VXFFHVVLYHO\ HOLPLQDWHV KDOI RI WKDW GDWD XQWLO WKH WDUJHW HOHPHQW LV IRXQG 6HOHFWLRQ VRUW LV D TXDGUDWLF 2 1 VRUWLQJ DOJRULWKP WKDW UHSHDWHGO\ ILQGV WKH VPDOOHVW XQSURFHVVHG HOHPHQW RI WKH DUUD\ DQG PRYHV LW WR WKH IURQWPRVW UHPDLQLQJ VORW RI WKH DUUD\ 0HUJH VRUW LV D 2 1 ORJ 1 VRUWLQJ DOJRULWKP RIWHQ LPSOHPHQWHG UHFXUVLYHO\ WKDW VXFFHVVLYHO\ GLYLGHV WKH LQSXW GDWDVHW LQWR WZR KDOYHV UHFXUVLYHO\ VRUWV WKH KDOYHV DQG WKHQ PHUJHV WKH VRUWHG KDOYHV LQWR D VRUWHG ZKROH 6HOI &KHFN 3UREOHPV 6HFWLRQ 6HDUFKLQJ DQG 6RUWLQJ LQ WKH -DYD &ODVV /LEUDULHV 'HVFULEH WZR ZD\V WR VHDUFK DQ XQVRUWHG DUUD\ RI 6WULQJ REMHFWV XVLQJ WKH -DYD FODVV OLEUDULHV 6KRXOG D VHTXHQWLDO RU ELQDU\ VHDUFK EH XVHG RQ DQ DUUD\ RI 3RLQW REMHFWV" 8QGHU ZKDW FLUFXPVWDQFHV FDQ WKH $UUD\V ELQDU\6HDUFK DQG &ROOHFWLRQV ELQDU\6HDUFK PHWKRGV EH XVHG VXFFHVVIXOO\" 6HFWLRQ 3URJUDP (IILFLHQF\ $SSUR[LPDWH WKH UXQWLPH RI WKH IROORZLQJ FRGH IUDJPHQWV LQ WHUPV RI Q int sum = 0; int j = 1; while (j <= n) { sum++; j *= 2; } int sum = 0; for (int i = 1; i <= n * 2; i = i++) { for (int j = 1; j <= n; j++) { sum++; } } for (int j = 1; j < 100; j++) { sum++; sum++; } 685 int sum = 0; for (int j = 1; j < n; j++) { sum++; if (j % 2 == 0) { sum++; } } 'HWHUPLQH WKH FRPSOH[LW\ FODVVHV RI DOJRULWKPV WR SHUIRUP WKH IROORZLQJ WDVNV )LQGLQJ WKH DYHUDJH RI WKH QXPEHUV LQ DQ DUUD\ RI LQWHJHUV )LQGLQJ WKH FORVHVW GLVWDQFH EHWZHHQ DQ\ SDLU RI SRLQWV LQ D 3RLQW DUUD\ )LQGLQJ WKH PD[LPXP YDOXH LQ DQ DUUD\ RI UHDO QXPEHUV &RXQWLQJ WKH PHGLDQ OHQJWK RI WKH 6WULQJV LQ DQ DUUD\ 5DLVLQJ DQ LQWHJHU WR D SRZHU VXFK DV $% ([DPLQLQJ DQ DUUD\ RI SRLQWV WR VHH KRZ PDQ\ WULRV RI SRLQWV DUH FROLQHDU WKDW LV KRZ PDQ\ JURXSV RI WKUHH SRLQWV FRXOG EH FRQQHFWHG E\ D VWUDLJKW OLQH &RXQWLQJ WKH QXPEHU RI OLQHV LQ D ILOH 'HWHUPLQLQJ ZKHWKHU D JLYHQ LQWHJHU UHSUHVHQWLQJ \HDU VWRUHV D OHDS \HDU \HDUV GLYLVLEOH E\ EXW QRW E\ XQOHVV GLYLVLEOH E\ 6HFWLRQ ,PSOHPHQWLQJ 6HDUFKLQJ $OJRULWKPV :KDW LV WKH UXQWLPH FRPSOH[LW\ FODVV RI D VHTXHQWLDO VHDUFK RQ DQ XQVRUWHG DUUD\" :KDW LV WKH UXQWLPH FRPSOH[LW\ FODVV RI WKH PRGLILHG VHTXHQWLDO VHDUFK RQ D VRUWHG DUUD\" :K\ GRHV ELQDU\ VHDUFK UHTXLUH WKDW WKH LQSXW LV VRUWHG" +RZ PDQ\ HOHPHQWV DW PRVW GRHV D ELQDU\ VHDUFK H[DPLQH LI WKH DUUD\ FRQWDLQV HOHPHQWV" :KDW LQGH[HV ZLOO EH H[DPLQHG DV WKH PLGGOH HOHPHQW E\ D ELQDU\ VHDUFK IRU WKH WDUJHW YDOXH ZKHQ UXQ RQ WKH IROORZLQJ LQSXW DUUD\V" :KDW YDOXH ZLOO WKH ELQDU\ VHDUFK DOJRULWKP UHWXUQ" int int int int numbers numbers numbers numbers = = = = {1, {1, {1, {8, 3, 2, 2, 9, 6, 7, 8, 10, 15, 20, 30}; 3, 4, 5, 7, 8, 9, 10}; 3, 4, 5, 6, 7, 8, 9}; 12, 14, 15, 17, 19, 25, 31}; :KDW LQGH[HV ZLOO EH H[DPLQHG DV WKH PLGGOH HOHPHQW E\ D ELQDU\ VHDUFK IRU WKH WDUJHW YDOXH ZKHQ UXQ RQ WKH IROORZLQJ LQSXW DUUD\" 1RWLFH WKDW WKH ODVW LQSXW DUUD\ LVQ W LQ VRUWHG RUGHU :KDW FDQ \RX VD\ DERXW WKH ELQDU\ VHDUFK DOJRULWKP UHVXOW" int numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; 686 6HFWLRQ ,PSOHPHQWLQJ 6RUWLQJ $OJRULWKPV :KDW PRGLILFDWLRQV ZRXOG KDYH WR EH PDGH WR WKH VHOHFWLRQ6RUW PHWKRG WR FDXVH LW WR VRUW DQ DUUD\ RI GRXEOH YDOXHV UDWKHU WKDQ RQH RI LQWHJHU YDOXHV" +RZ PDQ\ FDOOV RQ WKH PHUJH6RUW PHWKRG DUH JHQHUDWHG E\ D FDOO WR VRUW D OLVW RI OHQJWK " 7KH VRUWLQJ DOJRULWKPV LQ -DYD V FODVV OLEUDULHV XVH TXLFNVRUW IRU SULPLWLYH YDOXHV EXW PHUJH VRUW IRU REMHFWV :K\ LV WKLV" *LYHQ WKH IROORZLQJ DUUD\ RI LQW HOHPHQWV int numbers = {7, 2, 8, 4, 1, 11, 9, 5, 3, 10}; 6KRZ WKH VWDWH RI WKH HOHPHQWV DIWHU SDVVHV RI WKH RXWHUPRVW ORRS RI VHOHFWLRQ VRUW 6KRZ D WUDFH RI OHYHOV GHHS RI WKH PHUJH VRUW DOJRULWKP 6KRZ WKH VSOLWWLQJ RI WKH RYHUDOO DUUD\ SOXV RQH OHYHO GHHS RI WKH UHFXUVLYH FDOOV *LYHQ WKH IROORZLQJ DUUD\ RI LQW HOHPHQWV int numbers = {7, 1, 6, 12, -3, 8, 4, 21, 2, 30, -1, 9}; 6KRZ WKH VWDWH RI WKH HOHPHQWV DIWHU SDVVHV RI WKH RXWHUPRVW ORRS RI VHOHFWLRQ VRUW 6KRZ D WUDFH RI OHYHOV GHHS RI WKH PHUJH VRUW DOJRULWKP 6KRZ WKH VSOLWWLQJ RI WKH RYHUDOO DUUD\ SOXV RQH OHYHO GHHS RI WKH UHFXUVLYH FDOOV ([HUFLVHV :ULWH FRGH WR UHDG D GLFWLRQDU\ IURP D ILOH WKHQ SURPSW WKH XVHU IRU WZR ZRUGV DQG WHOO WKH XVHU KRZ PDQ\ ZRUGV LQ WKH GLFWLRQDU\ IDOO EHWZHHQ WKRVH WZR ZRUGV 8VH ELQDU\ VHDUFK LQ \RXU VROXWLRQ )RU H[DPSOH Type two words: goodbye hello There are 4418 words between goodbye and hello :ULWH D &RPSDUDWRU WKDW FRPSDUHV 3RLQW REMHFWV E\ WKHLU GLVWDQFH IURP WKH RULJLQ RI 3RLQWV FORVHU WR WKH RULJLQ DUH FRQVLGHUHG WR FRPH EHIRUH WKRVH IXUWKHU IURP WKH RULJLQ :ULWH D &RPSDUDWRU WKDW FRPSDUHV 6WULQJ REMHFWV E\ WKHLU QXPEHU RI ZRUGV &RQVLGHU DQ\ QRQ ZKLWHVSDFH VWULQJ RI FKDUDFWHUV DV D ZRUG )RU H[DPSOH KHOOR FRPHV EHIRUH , VHH FRPHV EHIRUH <RX FDQ GR LW :ULWH D PRGLILHG YHUVLRQ RI WKH VHOHFWLRQ VRUW DOJRULWKP WKDW VHOHFWV WKH ODUJHVW HOHPHQW HDFK WLPH DQG PRYHV LW WR WKH HQG RI WKH DUUD\ UDWKHU WKDQ VHOHFWLQJ WKH VPDOOHVW DQG PRYLQJ LW WR WKH EHJLQQLQJ :LOO WKLV DOJRULWKP EH IDVWHU WKDQ WKH VWDQGDUG VHOHFWLRQ VRUW" :KDW ZLOO LWV FRPSOH[LW\ FODVV ELJ RK EH" 687 :ULWH D PRGLILHG GXDO YHUVLRQ RI WKH VHOHFWLRQ VRUW DOJRULWKP WKDW VHOHFWV ERWK WKH ODUJHVW DQG VPDOOHVW HOHPHQWV RQ HDFK SDVV DQG PRYHV HDFK RI WKHP WR WKH DSSURSULDWH HQG RI WKH DUUD\ :LOO WKLV DOJRULWKP EH IDVWHU WKDQ WKH VWDQGDUG VHOHFWLRQ VRUW" :KDW SUHGLFWLRQV GR \RX PDNH DERXW LWV SHUIRUPDQFH UHODWLYH WR WKH PHUJH VRUW DOJRULWKP" :KDW ZLOO LWV FRPSOH[LW\ FODVV ELJ RK EH" ,PSOHPHQW DQ DOJRULWKP WR VKXIIOH DQ DUUD\ RI QXPEHUV RU REMHFWV 7KH DOJRULWKP IRU VKXIIOLQJ VKRXOG EH WKH IROORZLQJ for each index i: choose a random index j greater than or equal to i. swap elements [i] and [j]. 7KH FRQVWUDLQW DERXW M EHLQJ JUHDWHU WKDQ RU HTXDO WR L LV DFWXDOO\ TXLWH LPSRUWDQW LI \RX ZDQW \RXU VKXIIOLQJ DOJRULWKP WR VKXIIOH IDLUO\ :K\" 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW UHDGV D VHULHV RI LQSXW OLQHV DQG VRUWV WKHP LQ DOSKDEHWLFDO RUGHU LJQRULQJ WKH FDVH RI ZRUGV 7KH SURJUDP VKRXOG XVH WKH PHUJH VRUW DOJRULWKP VR WKDW LW TXLFNO\ VRUWV HYHQ D ODUJH ILOH :ULWH D SURJUDP WKDW SURFHVVHV D GDWD ILOH RI VWXGHQWV FRXUVH JUDGH GDWD 7KH GDWD DUULYHV LQ UDQGRP RUGHU ZLWK HDFK OLQH VWRULQJ LQIRUPDWLRQ DERXW D VWXGHQW V ODVW QDPH ILUVW QDPH VWXGHQW ,' QXPEHU SHUFHQWDJH DQG OHWWHU JUDGH )RU H[DPSOH Smith Johnson Reges Smith Reges Kelly Gus Stu Marty Abe 438975 210498 098736 346282 298575 98.6 72.4 88.2 84.1 78.3 A C B B C <RXU SURJUDP VKRXOG EH DEOH WR VRUW WKH GDWD E\ DQ\ RI WKH FROXPQV 8VH &RPSDUDWRUV WR DFKLHYH WKH VRUW RUGHULQJV 0DNH WKH GDWD VRUWDEOH E\ ODVW QDPH VWXGHQW ,' DQG JUDGHV LQ DVFHQGLQJ DQG GHVFHQGLQJ RUGHU )RU H[DPSOH Student data, by last name: Johnson Gus 210498 72.4 Reges Stu 098736 88.2 Reges Abe 298575 78.3 Smith Kelly 438975 98.6 Smith Marty 346282 84.1 Student data, by student ID: Reges Stu 098736 88.2 Johnson Gus 210498 72.4 Reges Abe 298575 78.3 Smith Marty 346282 84.1 Smith Kelly 438975 98.6 C B C A B B C C B A :ULWH D SURJUDP WKDW GLVFRYHUV DOO DQDJUDPV RI DOO ZRUGV JLYHQ DQ LQSXW ILOH WKDW VWRUHV D ODUJH GLFWLRQDU\ $Q DQDJUDP RI D ZRUG LV D UHDUUDQJHPHQW RI LWV OHWWHUV LQWR D QHZ OHJDO ZRUG )RU H[DPSOH WKH DQDJUDPV RI VKDUH LQFOXGH VKHDU KHDUV DQG KDUHV $VVXPH WKDW 688 \RX KDYH D ILOH DYDLODEOH WR \RX ZKLFK OLVWV PDQ\ ZRUGV RQH SHU OLQH <RXU SURJUDP VKRXOG ILUVW UHDG LQ WKH GLFWLRQDU\ DQG VRUW LW ,W VKRXOGQ W VRUW LW LQ DOSKDEHWLFDO RUGHU WKRXJK ,QVWHDG LW VKRXOG VRUW DFFRUGLQJ WR HDFK ZRUG V FDQRQLFDO IRUP 7KH FDQRQLFDO IRUP RI D ZRUG LV GHILQHG WR EH D ZRUG ZLWK WKH VDPH OHWWHUV DV WKH RULJLQDO EXW DSSHDULQJ LQ VRUWHG RUGHU 7KXV WKH FDQRQLFDO IRUP RI FRPSXWHU LV FHPRSUWX DQG WKH FDQRQLFDO IRUP RI SURJUDP LV DJPRSUU 7KXV LQ VRUWLQJ WKH GLFWLRQDU\ WKH ZRUG SURJUDP ZRXOG EH SODFHG EHIRUH WKH ZRUG FRPSXWHU EHFDXVH LWV FDQRQLFDO IRUP LV DOSKDEHWLFDOO\ OHVV :ULWH FRGH WR UHWULHYH D ZRUG V FDQRQLFDO IRUP DQG D &RPSDUDWRU WKDW FRPSDUHV ZRUGV E\ FDQRQLFDO IRUPV 6WXDUW 5HJHV 0DUW\ 6WHSS 689 690 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online