Fionn88 / ccClub-Practice

Python study group topic exercises.
https://www.ccclub.io/
0 stars 0 forks source link

Introduction (Concepts, Recursion, Algorithm Analysis) 學習筆記 #3

Open Fionn88 opened 9 months ago

Fionn88 commented 9 months ago
Fionn88 commented 9 months ago

Overview: System Life Cycle

搜索引擎

  1. 把所有網頁撈回來
  2. 分析網頁內容,去建關鍵字和 index
  3. 使用者下 query,把 document 去做 metric 並把資料抓出來顯示

    • Analysis

      • Bottom-up
      • Top-down

        當你設計一個系統,思考他的 input 跟 output 是什麼

    • Design

      • Data objects:abstract data types

        這些 Data objects 所實現的 function 是什麼

      • Operations:specification & design of algorithms

        Function:設計演算法來達到

Yahoo 新聞首頁

一個 Team 做出來的,並且來自各國,EX: 印度, 美國, etc.

  • 啟動備份新聞平台去測試小功能 => Testing
Fionn88 commented 9 months ago

Evaluative judgments about programs

Fionn88 commented 9 months ago

Data Abstraction

Struct student { char last_name;
int student_id;
char grade; }

integer: +, -, *, /, %, =, ==, atoi()

  • Representation: char 1 byte, int 4 bytes
Fionn88 commented 9 months ago

Algorithm Specification

解決問題的方法

program doesn’t have to be finite (e.g. OS scheduling)

Fionn88 commented 9 months ago

Example 1: Selection Sort

從小到大的排序,選最小的出來,放到第一個 有兩個 Set 的數字,一個是已排序,一個是 unsort.

void sort(int list[ ], int n)
{
for (i=0; i < n-1; i++) {
min = i;
for (j = i+1; j < n; j++) {
if (list[j] < list[min])
min = j; }
SWAP(list[i], list[min], temp);
}
}

如是 comparison base 的話是 Quick Sort 是最快的:O(nlogn),非 comparison base 的話則是 Radix Sort

Fionn88 commented 9 months ago

Example of Binary Search

前提數字是排好序的

input:你想找的數字 output:數字是否有在這個 List

Binary Search 跟 Binary Search Tree 是不同的東西

int compare(int x, int y)
/* return -1 for less than, 0 for equal */
int binsearch(int list[], int searchno, int left, int right)
{
while (left <= right) {
middle = (left + right) / 2;
switch ( COMPARE(list[middle], searchno) ) {
case -1: left = middle +1;
break;
case 0: return middle;
case 1: right = middle -1;
}
}
}
Fionn88 commented 9 months ago

Example 3: Selection Problem

Fionn88 commented 9 months ago

Recursive Algorithms

Recursive Multiplication

Fionn88 commented 9 months ago

不同的 Data structure 實作演算法(解決問題的方式)時會有不同的複雜度

Recursive Summation

空間複雜度為:O(N)。

For Loop 去解決加總演算法的話,空間複雜度為:O(1)。

時間複雜度:Recursive Summation > For Loop

Recursive binary search

int binsearch(int list[], int searchno, int left, int right)
{
if (left <= right) {
middle = (left + right)/2;
switch (COMPARE(list[middle], searchno) )
{
case -1: return binsearch(list, searchno, middle+1, right)
case 0: return middle;
case 1: return binsearch(list, searchno, left, middle-1);
}
}
return -1;
}

Recursive Permutations

void perm(char *list, int i, int n)
{
if ( i == n) {
for (j=0; j <=n; j++)
cout<<list[j];
}
else {
for (j = i; j <= n; j++) {
SWAP(list[i], list[j], temp);
perm(list, i+1, n);
SWAP(list[i], list[j], temp);
}
}
}
Fionn88 commented 9 months ago

Performance Evaluation

Performance Analysis

Fionn88 commented 9 months ago

Space Complexity

c: fixed space(instruction, simple variables, constant

存放變數的 storage,與怎麼實作演算法叫有關係

Sp(I): depends on characteristics of instance I

Sp(I):Input 的量的大小

Characteristics: number, size, values of I/O associated with I

if n is the only characteristic, Sp(I) ⇒ Sp(n)

Fionn88 commented 9 months ago

Time Complexity

c: compile time Tp(I): program execution time depends on characteristics of instance I

Characteristic: number, size, values of I/O associated with I

predict the growth in run time as the instance characteristics change

Fionn88 commented 9 months ago

Some Rules