C++ Week 9
C++ Week 9
Week 9
Bubble sort, Recursive and structure
Note
• recursive: 遞迴
• algorithm: 演算法
• convergence: 收斂
• complexity: 複雜
• factorial: 階層
• hash: 雜湊
Outline
• Bubble sort
• Recursive
• Structure
• Practice
Bubble Sort
The function used for recursion must have a set of if...else, one side is
used to judge whether the critical value is reached, the other side will
continue to call itself, and at the same time to achieve "convergence
parameters".
Recursive
5+4+3+2+1=?
main
recursion_sum(5)
5 + 10
recursion_sum(5)
4+6 recursion_sum(value-1)
recursion_sum(4)
3+3 recursion_sum(value-1)
recursion_sum(3)
2 +1 recursion_sum(value-1)
recursion_sum(2)
recursion_sum(value-1)
1
recursion_sum(1)
Ex: Recursive - Factorial
5!= 5*4*…*1=?
Structure
• struct struct_name{
data_type var_name1;
data_type var_name2;
• ...
};
Structure - person
• Struct declaration :
struct struct_name variable_name;
• Initialization:
To access the variables in the structure, you need to use the
"." operator.
• Declaration and Initialization:
struct struct_name variable_name = {data1, data2….};
These initial values are filled in the member in the order in
which the member variables are declared.
Structure – person initialization
Practice
• Please use Double Hashing to put the following key values into
hash table of size 13 (see Table 1).
• {37, 30, 65, 24, 48, 18, 27, 53}
•)
• (i=0, 1, 2, …, 12)
• First calculation: Primer = h1(k)
If the Primers of the first point conflict, then: Primer = h1(k) +
1* h2(k)
• If the Primer of the second point is still in conflict, then: Primer =
h1(k) + 2*h2(k)
• 0And so1 on2until 3there4are no
5 more6 conflicts.
7 8 9 10 11 12
65 27 0 0 30 18 24 0 53 48 0 37 0