第一章(CHAPTER 1)

Introduction

In this chapter,we discuss the aims and goals of this text and briefly review programing concepts and discrete mathematics.We will

  • See that how a program performs for reasonably large input is just as important as its performance on moderate amounts of input.
  • Summarize the basic mathematical background needed for the rest of the book.
  • Briefly review recursion
  • Summarize some important features of Java that are used throughout the text

簡介

在這章中筛欢,我們討論這本書的目標(biāo)和目的纵揍,然后簡單回顧一下編程概念以及離散數(shù)學(xué)宇植。我們將

  • 看到一個程序在大量合法輸入的運(yùn)行情況和適量合法輸入的運(yùn)行情況是一樣重要的
  • 為這本書的剩余部分總結(jié)基本的數(shù)學(xué)背景需求
  • 簡要介紹一下遞歸
  • 總結(jié)這本書中通篇使用到的一些重要的Java特性

1.1 What's the Book About?

Suppose you have a group of N numbers and would like to determine the kth largest.This is known as the selection problem.Most students who have had a programming course or two would have no difficulty writing a program to solve this problem.There are quite a few "obvious" solutions.

假設(shè)你有一組N個數(shù)字工猜,然后要找出第k大的值日裙。這被稱為選擇選擇排序問題弦疮。對大多數(shù)已經(jīng)有一兩門編程課程的學(xué)生來說阻肩,寫一個程序去解決這個問題沒有一點(diǎn)困難匀伏。有不少明顯的解決方案洒忧。

One way to solve this problem would be to read the N numbers into an array,sort the array in decreasing order by some simple algorithm such as bubblesort,and then return the element in position k.

一種解決方法就是將這N個數(shù)字讀進(jìn)一個數(shù)組中,然后使用一些簡單的算法對數(shù)組進(jìn)行降序排序够颠,比如冒泡排序算法熙侍,然后返回數(shù)組中第k個值。

package com.lin.data.structure;

public class BubbleSortTest {
    public static void main(String[] args) {
        int[] arr = {6, 3, 8, 2, 9, 1};
        int k = 3;
        System.out.println("排序前數(shù)組為:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        for (int i = 0; i < arr.length - 1; i++) {//外層循環(huán)控制排序趟數(shù)
            for (int j = 0; j < arr.length - 1 - i; j++) {//內(nèi)層循環(huán)控制每一趟排序多少次
                if (arr[j] < arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println();

        System.out.println("排序后的數(shù)組為:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
        System.out.println("第"+k+"大的元素為:"+arr[k-1]);
    }
}

A somewhat better algorithm might be to read the first k elements into an array and sort them(in decreasing order).Next,each remaining element is read one by one.As a new element arrives,it is ignored if it is smaller than the kth element in the array.Otherwise ,it is placed in its correct spot in the array,bumping one element out of the array.When the algorithm ends,the element in the kth position is returned as the answer.

一種多少更好的算法可能是首先讀k個元素到一個數(shù)組中并對其進(jìn)行降序排序履磨。然后蛉抓,一個一個讀剩下的元素。當(dāng)一個新的元素到達(dá)是剃诅,判斷如果它小于數(shù)組中的第k個元素巷送,就忽略它。否則這個元素就放到數(shù)組中正確的位置矛辕。擠出數(shù)組中原來的一個元素笑跛。當(dāng)算法結(jié)束付魔,數(shù)組中第k個元素將作為答案返回。

package com.lin.data.structure;

public class PumpingTest {
    public static void main(String[] args) {
        int[] arr = {6, 3, 8, 2, 9, 1};
        int k = 3;
        System.out.println("排序前數(shù)組為:");
        for (int num : arr) {
            System.out.print(num + " ");
        }


        int[] tempArr = new int[k];
        for (int i=0;i<k;i++){
            tempArr[i] = arr[i];
        }


        bubbleSort(tempArr);

        for (int i=k;i<arr.length;i++){
            if (arr[i] < tempArr[k-1]){
                continue;
            }else {
                tempArr[k-1] = arr[i];
                bubbleSort(tempArr);
            }
        }

        System.out.println();
        System.out.println("第k大的元素"+tempArr[k-1]);
    }

    public static void bubbleSort(int[] tempArr){
        for (int i = 0; i < tempArr.length - 1; i++) {//外層循環(huán)控制排序趟數(shù)
            for (int j = 0; j < tempArr.length - 1 - i; j++) {//內(nèi)層循環(huán)控制每一趟排序多少次
                if (tempArr[j] < tempArr[j + 1]) {
                    int temp = tempArr[j];
                    tempArr[j] = tempArr[j + 1];
                    tempArr[j + 1] = temp;
                }
            }
        }

    }
}

Both algorithms are simple to code,and you are encouraged to do so.The natural questions,then,are whick algorithm is better and ,more important,is either algorithm good enough?A simulation using a random file of 30 million elements and k = 15,000,000 will show that neither algorithm finishes in a reasonable amount of time;each requires several days of computer processing to terminate(albeit eventually which a correct answer).An alternative method,discussed in Chapter 7,gives a solution in about a second.Thus although our proposed algorithms work,they cannot be considered good algorithms,because they are entirely impractical for input sizes that a third algorithm can handle in a reasonable amount of time.

兩種算法都很容易編碼飞蹂,并且鼓勵你去實現(xiàn)几苍。很自然的問題是,哪一個算法更好陈哑,更重要一些妻坝?是否有一種算法足夠好?使用了一個包含3000萬元素的文件芥颈,指定k等于1500萬的模擬表明沒有一種算符在一個合理的時間內(nèi)結(jié)束惠勒,每種算法都要耗費(fèi)幾天的時間去計算(即使最終都能獲得一個正確的結(jié)果)。另一個供選擇的將在第7章討論的方法爬坑,給出的解決方法大概需要1秒鐘纠屋。因此,我們我們提議的算法盾计,都不能被認(rèn)為是一個好的算法售担,因為他們對上面的輸入來說,完全不切實際署辉,并且第三種算法可以在一個合理的時間內(nèi)處理這種情況族铆。

猜字謎游戲

A second problem is to solve a popular world puzzle. The input consists of a two-dimensional array of letters and a list of words.The object is to find the words in the puzzle.These words may be horizontal,vertical,or diagonal in any direction.As an example ,the puzzle shown in Figure 1.1 contains these words this,two,fat,and that.The word this begins at row 1,column 1,or (1,1),and extends to (1,4);two goes from (1,1) to (3,1);fat goes from (4,1) to (2,3);and that goes from (4,4) to (1,1)

第二個問題是解決流行的猜字謎的問題。輸入是由字母組成的二維數(shù)組和一個單詞列表哭尝。目的是找出迷宮中的單詞哥攘。這些單詞可能是橫向、縱向或者斜向任何一個方向材鹦。舉一個例子逝淹,圖標(biāo)1.1顯示的謎題包含 this,two,fat,that這些單詞。單詞this從行1桶唐,列1或者叫(1,1)延伸至(1,4)栅葡。two 從(1,1)出發(fā)到(3,1);fat 從(4,1)出發(fā)到(2,3);that 從(4,4)出發(fā)到(1,1)。

Again ,there are at least two straightforward algorithms that solve the problem. For each word in the word list, we check each ordered triple(row,column,orientation) for the presence of the word. The amounts to lots of nested for loops but is basically straightforward.

再次尤泽,有兩種簡單的算法來解決這個問題欣簇。對列表中的每個單詞,我們檢查有序的三元組(橫坯约,豎熊咽,斜)來找出存在的單詞。這相當(dāng)于很多的嵌套的for循環(huán)闹丐,但基本上很簡單网棍。

Alternatively, for each ordered quadruple(row,column,orientation,number of characters) that doesn't run off an end of the puzzle,we can test whether the word indicated in the word list. Again ,this amounts to lots of the nested for loops.It is possible to save more time if maximum number of characters in any word is known.

另外,對每個沒有跑出迷宮的有序四元組(行妇智,列滥玷,方向,字符個數(shù))巍棱,我們可以測試這個單詞是否在單詞列表中惑畴。再次,這相當(dāng)于很多嵌套的for循環(huán)航徙。如果每個單詞的最大字符數(shù)已經(jīng)知道了的話如贷,可能會節(jié)省很多時間

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市到踏,隨后出現(xiàn)的幾起案子杠袱,更是在濱河造成了極大的恐慌,老刑警劉巖窝稿,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件楣富,死亡現(xiàn)場離奇詭異,居然都是意外死亡伴榔,警方通過查閱死者的電腦和手機(jī)纹蝴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來踪少,“玉大人塘安,你說我怎么就攤上這事≡荩” “怎么了兼犯?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長集漾。 經(jīng)常有香客問我切黔,道長,這世上最難降的妖魔是什么帆竹? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任绕娘,我火速辦了婚禮,結(jié)果婚禮上栽连,老公的妹妹穿的比我還像新娘险领。我一直安慰自己,他們只是感情好秒紧,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布绢陌。 她就那樣靜靜地躺著,像睡著了一般熔恢。 火紅的嫁衣襯著肌膚如雪脐湾。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天叙淌,我揣著相機(jī)與錄音秤掌,去河邊找鬼愁铺。 笑死,一個胖子當(dāng)著我的面吹牛闻鉴,可吹牛的內(nèi)容都是我干的茵乱。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼孟岛,長吁一口氣:“原來是場噩夢啊……” “哼瓶竭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起渠羞,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤斤贰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后次询,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體荧恍,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年渗蟹,在試婚紗的時候發(fā)現(xiàn)自己被綠了块饺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡雌芽,死狀恐怖授艰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情世落,我是刑警寧澤淮腾,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站屉佳,受9級特大地震影響谷朝,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜武花,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一圆凰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧体箕,春花似錦专钉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至娃兽,卻和暖如春菇民,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工第练, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留阔馋,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓复旬,卻偏偏與公主長得像垦缅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子驹碍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容