Lecture 1 Introduction
Programming Basic
building blocks in a C++ program:
- constant, (e.g., 3, "x*x + x = ")
- variables, (e.g., x) for storing values
- operations (e.g., +, =, <<) for executing operations
- expressions (e.g., x*x+x)
- statement (e.g., x=3;)
- functions (e.g., main)
the execution starts at the main method

- compile a program
g++ -o test.exe test.cpp - run a program
test.exe
Array (C++)
array: for storing multiple variables of the same type
- create an array A with n integers by
int A[n]; // for small array
int* A = new int[n]; // for large array
A[i]denotes the variables at the positioniof arrayA- in C++, the range of
iis from0ton-1
examples:
int A[7]; // create an array
x = A[2]; // copy value A[2] to variable x
A[3] = 5; // store value 5 to A[3]

Control Flow (C++)
sequence (step by step)
- a sequence of statement implies the execution ordering

- each statement is a 'step', e.g.,
c = a*b; cout << "hello" << endl;
selection (choose a branch)
if statement, switch statement

#include <iostream>
using namespace std;
int main ()
{
// 局部变量声明
char grade = 'D';
switch(grade)
{
case 'A' :
cout << "很棒!" << endl;
break;
case 'B' :
case 'C' :
cout << "做得好" << endl;
break;
case 'D' :
cout << "您通过了" << endl;
break;
case 'F' :
cout << "最好再试一下" << endl;
break;
default :
cout << "无效的成绩" << endl;
}
cout << "您的成绩是 " << grade << endl;
return 0;
}
selection (choose a branch)
repeat the execution of the loop body (a sequence of statement) for multiple times
example: print "hello" 5 times
while loop:
int i=5;
while (i>0) {
cout << "hello" << endl;
i--;
}
- evaluate <cond> before the loop body;
- the loop stops when <cond> is false
for loop:
- Execute <init> once before the loop starts
- Evaluate <cond> before the loop body; the loop stops when <cond> is false
- Execute <update> after the loop body
for (<init>; <cond>; <update>) {
// body
}
// example
for (int i=0; i<5; i++) {
cout << "hello" << endl;
}
how to solve a problem? by an algorithm?
a problem for human (in the real world)
sort poker cards on hand
- input: a list of cards
- procedures:
- put all cards on the left hand
- pick the smallest card from the left hand
- move that card to the right hand
- repeat step 2-3 until the left hand is empty
computational problem
- example: the sorting problem
- input: a sequence
Aof n integers - output: a sequence
A'of n integers such that:A'is an increasing sequenceAandA'contain the same integers
- input: a sequence
algorithms
Algorithm: a well defined sequence of steps for solving a computational problem
- It always produces the correct output
- It uses well-defined steps or operations
- It finishes in finite time
- It is written for human readers
- Like programming, we can use variables and data structures, control flow, call statement
- We can also use natural language provided that it is clear. If it is unclear, we need to define it first
An algorithms can be implemented as a program in any language (e.g., C, C++, Java, Python)
An algorithms is more readable than a program.
- algorithm
Selection-Sort (Array A[0..n–1])
1. for integer i ← 0 to n–2
2. k ← i
3. for integer j ← i+1 to n–1
4. if A[k] > A[j] then
5. k ← j
6. swap A[i] and A[k]
- program
void SelectionSort(int* A, int n) {
int i, j, k, temp;
for (i = 0; i < n–1; i++) {
k = i;
for (j = i + 1; j < n; j++) {
if (A[k] > A[j])
k = j;
}
int temp = A[i];
A[i] = A[k];
A[k] = temp;
}
}
int main() {
// read input
SelectionSort(myarray, length);
// write output
// ...
}
Algorithm design paradigms
Incremental approach
Build a solution into a larger solution
E.g., we have a sorted subarray A[0..i–1], then append an item to obtain a sorted subarray A[0..i]
Recursive approach (or divide-and-conquer)
- Reduce the problem into smaller subproblems
E.g., find the smallest item in subarrayA[i..n–1], then sort the subarrayA[i+1..n–1] - recursion: a function that calls itself
- base case: a simple case that does not call the function
Recursive Approach: Simple Example
int F (int n) {
if (n == 1) // base case
return 1;
else
return n * F(n–1); // recursive case
}

Selection Sort
Example: recursive selection sort
Input: an subarray A[i..n–1] 5| 2| 4| 9| 7
Output : an subarray A[i..n–1] in the ascending order
Recur-Selection-Sort (A, i, n)
1. if i < n-1 then
2. k ← i
3. for integer j ← i+1 to n–1
4. if A[k] > A[j] then
5. k ← j
6. swap A[i] and A[k]
7. Recur-Selection-Sort (A, i+1, n) // a recursive call
Data structure
Let S be a set of items, and x be a search key
- A key is a number, e.g., product id
Useful operations on a set S - Search(S, x): search whether x appears in S
- Insert(S, x): insert item x into S
- Delete(S, x): remove item x from S
Data structure: - A way of organizing data objects for efficient usage
- Building blocks for designing algorithms
| Foo | Bar |