2 Recursion Jarray Jpointer
2 Recursion Jarray Jpointer
(Week2)
[email protected]
LMS Mail
010-2329-8055
Attendance Check
Syllabus
1. Time
2. Space
3. Network
4. Power
5. CPU Registers
Algorithm Analysis
Time Complexity
Algorithm Swap(a,b)
temp = a; 1
a = b; 1
b = temp; 1
}
F(n) = 3
O(1) : Order of 1
Algorithm Analysis
Time Complexity
Algorithm sum(A, n)
S = 0; 1
S = S + A[i]; n
return s; 1
} F(n) = 2n + 3
O(n)
Algorithm Analysis
} F(n) = 2n2 + 2n + 1
} O(n2)
Algorithm Analysis
Go to YouTube
“binary search Big O notation”
Algorithm Analysis
Big O Notation
Run time
number of inputs
Why Data Structure and Algorithm?
01 Array
02 Structure
03 Applications of Array and Structure
04 Pointer
05 Recursion
ADT(Abstract Data Type)
ADT
Data Structure
Circular Binary
Character Stack
Directed
Undirected
Basics of Data structure and Algorithm
17
Array
Array
Array Element
Array Index
Array
- Operation
· create(size) ::= Create an array that can hold “size” elements
· get(A,i) ::= Returns the ith element of array A
· set(A,I,v) ::= store value v at ith position in array A
Element
index
ADT
2 4 0 5
Int A[4] = {2,4,0,5};
A
A[0] A[1] A[2] A[3] A[0] A[1] A[2] A[3]
A 2 4 0 5
A.append(6) : Insert 6 at the very end
A.pop() : Clear the top value and return
A[2] = A[2] +1;
A.pop(1) : Remove A[1] and return
A.insert(1,10) : Insert 10 into A[1]
※ A[2] address
A.remove(value) : Remove “value” from A
: A[0] address + 2*4Bytes
A.index(value), A.count(value), …
□ 1-Dimensional Array1차원배열
int list[3][5];
Row 0
Row 1
Row 2
Structure
Structure
Structure
Array Structure
Field
Structure
operations:
struct studentTag s1;
strcpy(s1.name, “Soares");
s1.age = 20;
s1.gpa = 4.3;
Structure
typedef
typedef studentTag {
char name[10];
int age;
double gpa;
} student;
student s;
#include <stdio.h>
int main(void)
{
student a = { “Soares", 20, 4.3 };
student b = { “Nazarova", 21, 4.2 };
return 0;
}
Applications of Arrays and Structures
Applications of Array and Structure : polynomial
General form of polynomials "p of x equals a sub n times x to the power of n, plus a sub
n minus 1 times x to the power of n minus 1, plus ……..,
plus a sub 1 times x, plus a sub 0."
To express polynomials #1
10 x 5 + 0 x 4 + 0 x 3 + 0 x 2 + 6 x1 + 3 x 0
1
0 1 2 3 4 5 6 7 8 9
0
coef 10 0 0 0 6 3
Applications of Array and Structure : polynomial
To express polynomials #1
To express polynomials #1
void print_poly(polynomial p)
{
for (int i = p.degree; i>0; i--)
printf("%3.1fx^%d + ", p.coef[p.degree - i], i);
printf("%3.1f \n", p.coef[p.degree]);
}
// Main function
int main(void)
{
polynomial a = { 5,{ 3, 6, 0, 0, 0, 10 } };
polynomial b = { 4,{ 7, 0, 5, 0, 1 } };
polynomial c;
print_poly(a);
print_poly(b);
c = poly_add1(a, b);
printf(“-----------------------------------------------------------------------------\n”);
print_poly(c);
return 0;
}
Applications of Array and Structure : polynomial
Execution result
# Main function
if __name__ == "__main__":
# Define two polynomials a and b
a = Polynomial(5, [3, 6, 0, 0, 0, 10])
b = Polynomial(4, [7, 0, 5, 0, 1])
To express polynomials #2
Example
int avail=6;
To express polynomials #2
To express polynomials #2
// C = A + B
poly_add2(int As, int Ae, int Bs, int Be, int *Cs, int *Ce)
{
float tempcoef;
*Cs = avail;
while( As <= Ae && Bs <= Be )
switch(compare(terms[As].expon,terms[Bs].expon)){
case '>': // Degree of A > Degree of B
attach(terms[As].coef, terms[As].expon);
As++; break;
case '=': // Degree of A == Degree of B
tempcoef = terms[As].coef + terms[Bs].coef;
if( tempcoef )
attach(tempcoef,terms[As].expon);
As++; Bs++; break;
case '<': // Degree of A < Degree of B
attach(terms[Bs].coef, terms[Bs].expon);
Bs++; break;
}
Applications of Array and Structure : polynomial
To express polynomials #2
// A
for (; As <= Ae; As++)
attach(terms[As].coef, terms[As].expon);
// B
for (; Bs <= Be; Bs++)
attach(terms[Bs].coef, terms[Bs].expon);
*Ce = avail - 1;
}
To express polynomials #2
//
int main(void)
{
int As = 0, Ae = 2, Bs = 3, Be = 5, Cs, Ce; // As: A start, Ae: A end
poly_add2(As, Ae, Bs, Be, &Cs, &Ce);
print_poly(As, Ae);
print_poly(Bs, Be);
printf(“-----------------------------------------------------------------------------\n”);
print_poly(Cs, Ce);
return 0;
}
Pointer
※ The concept of pointer in C Language (1/4)
※ The concept of pointer in C Language (2/4)
char a='A';
char *p;
p = &a;
Address
26
26 ‘A’
Pointer p Variable a
Pointer
*p= 'B';
Address
26
26 ‘B’
Pointer p Variable a
Pointer
pointer-related operators
Address
26
26 ‘B’
Pointer p Variable a
*p &a
Pointer
Array vs Pointer
== &A[0]
Pointer
main()
{
int *pi;
pi = (int *)malloc(sizeof(int));
// allocate dynamic memory
...
Heap ... // use
...
free(pi); // return
}
Pointer
Execution Result
0123456789
Pointer
(*ps).i X
ps->I O
Pointer
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
student *p;
p = (student *)malloc(sizeof(student));
if (p == NULL) {
fprintf(stderr, “not enough memory.\n");
exit(1);
}
Pointer
Example
strcpy(p->name, "Park");
p->age = 20;
free(s);
return 0;
}
Recursion
Recursion반복, 되풀이
□ Recursion ?
“GNU"
GNU는 "GNU's Not Unix!“ (GNU는 유닉스가 아닙니다!)의 약자
GNU is a free software operating system that was
launched by Richard Stallman in 1983.
재귀함수 (再歸函數)
Recursion
A B C A B C
A B C A B C
A B C A B C
A B C A B C
Recursion
General Approach ??
n-1 disks
1 disk
Using C as a temporary buffer,
A B C n-1 disks stacked in A are
transferred to B.
A B C
Move the largest disk from A to C.
A B C
Using A as a temporary buffer,
n-1 disks stacked in B are
transferred to C.
A B C
Recursion
General Approach ??
Now, how do we move n-1 disks from A to B and from B to C?
// Move the n disks stacked on the rod from to the rod to using the rod tmp.
void hanoi_tower(int n, char from, char tmp, char to)
{
if (n==1){
Move the disk from from to to.
}
else{
hanoi_tower(n-1, from, to, tmp);
move one disk from from to to.
hanoi_tower(n-1, tmp, from, to);
}
}
Recursion
Tower of Hanoi Program
#include <stdio.h>
void hanoi_tower(int n, char from, char tmp, char to)
{
if( n==1 ) printf(“move disk 1 from %c to %c.\n”, from,to)
else {
hanoi_tower(n-1, from, to, tmp);
printf(“move disk %d from %c to %c,\n”,n,from,to);
hanoi_tower(n-1, tmp, from, to);
}
}
int main(void)
{
hanoi_tower(4, 'A', 'B', 'C');
retrun 0;
}
Tower of Hanoi
For reference,
Assuming that the time is 1 second to move one time,
1. Netscape
2. LOL(League of Legend)
3. Minecraft (Java edition)
4. Dungeon Fighter
are all developed under complex requirements, continuous
updates, and changing environments, the potential for code
complexity is very high.
Richard Matthew Stallman (RMS)
- TIOBE Index
#10 Go
- A programming language officially announced by Google
Package main
Import “fmt”
func main() {
fmt.Println(“Hello world”)
}
05. Top 10 of Programming Language (2023.03)
#9 PHP
- Dirty old php
* Adult language in the programming world (adult site)
→ Developed ad hoc
→ The most popular server-side web programming language
* Wikipedia, wordprocess, facebook, … run on PHP
<?php
echo “Hello World”;
?>
05. Top 10 of Programming Language (2023.03)
#8 SQL
- Structured Query Language, abbreviated as SQL (S-Q-L),
sometimes /ˈsiːkwəl/ "sequel" for historical reasons is a
domain-specific language used in programming and
designed for managing data held in a relational database
management system (RDBMS), or for stream processing in a
relational data stream management system (RDSMS). It is
particularly useful in handling structured data, i.e. data
incorporating relations among entities and variables.
#7 JavaScript
- JavaScript (/ˈdʒɑːvəskrɪpt/), often abbreviated as JS, is a programming
language that is one of the core technologies of the World Wide Web,
alongside HTML and CSS. As of 2022, 98% of websites use JavaScript
on the client side for webpage behavior, often incorporating third-
party libraries. All major web browsers have a dedicated JavaScript
engine to execute the code on users' devices.
consol.log(“Hello World”);
05. Top 10 of Programming Language (2023.03)
#6 Visual Basic
- Not like Basic in 60’s
* MicroSoft invented
* OOP embedded for Windows system
05. Top 10 of Programming Language (2023.03)
#5 C#
- Developed in 2000 (from MS)
- MicroSoft.net platform
05. Top 10 of Programming Language (2023.03)
#4 C++
- Extension of C
· Objected Oriented Program paradigm added to C
#include <iostream>
using namespce std;
Int main(void) {
cout<<“Hello World”<<endl;
return 0
}
05. Top 10 of Programming Language (2023.03)
#3 Java
- Developed in 1995 (SUN → Oracle), also C type language
- JVM (bytecode), Hybrid type (Compile + Interpreter)
* Machine independent, compatibility, scalability
- Basically Object Oriented Programming
#2 C
- Spirit : “Trust the programmers”
- Weakness
* Security
* Programmer’s mistake
#include <stdio.h>
main() (
printf (“Hello World\n”);
}
# Spirit of the C language
#1 Python
- Developed in 1991.
- Can be used for Data Science, AI, ML, and Web app development
- Web site
** Indentation
05. Top 10 of Programming Language (2023.03)
# Others
R -
-
Statistic oriented (developed in NZ)
There are a lot of package, chart, picture,
- Big data, ML
- Much more powerful than SAS, …
Thank You
엔디컷 대학 / AI-빅데이터학과
E-Mail: [email protected]
전 화: 010-2329-8055