第一章(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
簡介
在這章中,我們討論這本書的目標和目的,然後簡單回顧一下程式設計概念以及離散數學。我們將
- 看到一個程式在大量合法輸入的執行情況和適量合法輸入的執行情況是一樣重要的
- 為這本書的剩餘部分總結基本的數學背景需求
- 簡要介紹一下遞迴
- 總結這本書中通篇使用到的一些重要的Java特性
1.1 What's the Book About?
Suppose you have a group of N numbers and would like to determine the k th 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.
假設你有一組N個數字,然後要找出第k大的值。這被稱為選擇 選擇排序問題 。對大多數已經有一兩門程式設計課程的學生來說,寫一個程式去解決這個問題沒有一點困難。有不少明顯的解決方案。
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個數字讀進一個數組中,然後使用一些簡單的演算法對陣列進行降序排序,比如氣泡排序演算法,然後返回陣列中第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("排序前陣列為:"); for (int num : arr) { System.out.print(num + " "); } for (int i = 0; i < arr.length - 1; i++) {//外層迴圈控制排序趟數 for (int j = 0; j < arr.length - 1 - i; j++) {//內層迴圈控制每一趟排序多少次 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("排序後的陣列為:"); 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 k th 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 k th position is returned as the answer.
一種多少更好的演算法可能是首先讀k個元素到一個數組中並對其進行降序排序。然後,一個一個讀剩下的元素。當一個新的元素到達是,判斷如果它小於陣列中的第k個元素,就忽略它。否則這個元素就放到陣列中正確的位置。擠出陣列中原來的一個元素。當演算法結束,陣列中第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("排序前陣列為:"); 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++) {//外層迴圈控制排序趟數 for (int j = 0; j < tempArr.length - 1 - i; j++) {//內層迴圈控制每一趟排序多少次 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.
兩種演算法都很容易編碼,並且鼓勵你去實現。很自然的問題是,哪一個演算法更好,更重要一些?是否有一種演算法足夠好?使用了一個包含3000萬元素的檔案,指定k等於1500萬的模擬表明沒有一種算符在一個合理的時間內結束,每種演算法都要耗費幾天的時間去計算(即使最終都能獲得一個正確的結果)。另一個供選擇的將在第7章討論的方法,給出的解決方法大概需要1秒鐘。因此,我們我們提議的演算法,都不能被認為是一個好的演算法,因為他們對上面的輸入來說,完全不切實際,並且第三種演算法可以在一個合理的時間內處理這種情況。

猜字謎遊戲
A second problem is to solve a popular world puzzle.