0% found this document useful (0 votes)
13 views

C++ Week 9

The document discusses bubble sort, recursive functions, structures, and practice problems. It explains bubble sort and provides an example of how it works. For recursive functions, it gives examples of calculating factorials and sums recursively. It also describes how to define and initialize structures to organize related variables, using a person structure as an example. Finally, it presents some practice problems involving Collatz conjecture, sorting student data, and double hashing.

Uploaded by

黃楷庭
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C++ Week 9

The document discusses bubble sort, recursive functions, structures, and practice problems. It explains bubble sort and provides an example of how it works. For recursive functions, it gives examples of calculating factorials and sums recursively. It also describes how to define and initialize structures to organize related variables, using a person structure as an example. Finally, it presents some practice problems involving Collatz conjecture, sorting student data, and double hashing.

Uploaded by

黃楷庭
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

計算機程式與應用實習

Week 9
Bubble sort, Recursive and structure
Note

• recursive: 遞迴
• algorithm: 演算法
• convergence: 收斂
• complexity: 複雜
• factorial: 階層
• hash: 雜湊
Outline

• Bubble sort
• Recursive
• Structure
• Practice
Bubble Sort

• Bubble Sort is the simplest sorting algorithm that works by


repeatedly swapping the adjacent elements if they are in the
wrong order.
• This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
•65318724 •31567248
•56318724 •31562748
•53618724 •31562478
•53168724 •13562478
•53167824 •13526478
•53167284 •13524678
•53167248 •13254678
•53167248 •13245678
•35167248 •12345678
Bubble Sort
• Bubble sort starts with very first two elements, comparing them to check which
one is greater.
Recursive

• For some problems, it’s useful to have functions call


themselves.
• The recursion step executes while the original call to the
function is still “open,” i.e., it has not yet finished executing.
• The recursion step can result in many more such recursive
calls, as the function keeps dividing each new subproblem
with which the function is called into two conceptual pieces.
Recursive
10 + 9 + 8 + …. + 1 = ?

 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

• Structure are a way to group several related variables into


one place. Each variable in the structure is known as a
member of the structure.
Structure - person

• We define "person" as having the following properties:


• name(char[])
• height(int)
• weight(float)
• gender(char)
• Next, we need to combine the variables of the above complex basic
data type into the "person" variable type we designed in the program.

• struct struct_name{

data_type var_name1;
data_type var_name2;
• ...
};
Structure - person

Please declare it outside the main function.


Structure

• 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

• Collatz conjecture: Enter an integer, if it is even, divide it by


2, if it is odd, multiply it by 3 and add 1. After many
calculation, you will eventually get the value 1, Please output
how many calculations it takes to get 1.
Input:
6
Output:
8
Practice
• Input a Integer N to indicate the number of input data, and
then input the student name(Char array name[10]), mid-term
exam scores and final exam scores (Both int). After getting
the total score, sort each data from high to low, and then use \
t to format and output the name, mid-term score, final score,
and total score in four fields.
• Note: Struct can be used as a variable to sort.
Input:
3
AAA 30 50
BBB 10 20
CCC 80 75
Output:
Name Midterm Final Total
CCC 80 75 155
AAA 30 50 80
BBB 10 20 30
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

You might also like