You are on page 1of 104

K

Biên

www.hutech.edu.vn
*1.2021.CMP164*
-mail :
tailieuhoctap@hutech.edu.vn
I

...................................................................................................................I
.......................................................................................................... IV
............................................................................................................... 1
......................................................................................................... 1
.............................................................................................. 1
1.2.1 Khai báo ........................................................................................................ 1
.................................................................. 3
..................................................................... 4
...................................................................... 4
............................................................. 5
.......................................................... 7
.................................................... 7
1.3.1 Cách kha ......................................................................................... 8
....................................................................................... 8
............................................................................................. 9
............................................................................................... 9
1.4.1 Khai báo ........................................................................................................ 9
................................................................ 10
..................................................................... 11
..................................................................... 13
............................................................ 13
.................................................................................................... 15
................................................................................................ 17
.................................................... 17
2.2 KHAI BÁO VÀ ............................................................ 18
..................................................................................... 18
................................................................................ 18
....................................................................... 21
2.3.1 Phép gán...................................................................................................... 21
................................................................................... 22
................................................................................... 22
2.3.4 Phép so sánh ................................................................................................ 23
2 ......................... 23
........................................................................... 24
................................................................. 26
...................................................................................... 27
.............................................................................................. 27
........................................................................ 28
II
...................................................28
he ..................29
..........................................................................................30
........................................................................ 32
..........................................................................................32
2.6.2 Cách 1 ..........................................................................................................32
2.6.3 Cách 2 ..........................................................................................................34
2.7 ............................................................................... 36
.......................................................................................36
............................................................................36
........................................................37
...................................... 44
2.8.1 ..........................................................................................................44
..........................................................................................................44
.............................................................46
................................................................................................................ 48
.................................................................................................... 48
........................................................................................................... 52
....................................................................................................... 52
......................................................... 53
................................................................. 54
................................................................ 54
.................................................................................. 55
..........................................................................................55
............................................................................................55
............................................................................................56
............................................................................................57
.................................................................................................... 58
............................................................................................... 59
4. ....................................................................................................... 59
.......................................................................... 60
......................................................................................60
.....................................................................................................61
..................................................................................................62
..................................................................62
- Hàm rewind()......................................62
............................................................................ 63
.........................................................................63
..........................................................................64
.......................................................................... 66
- Hàm fwrite() ...................................................66
III
- Hàm fread() ................................................... 66
.................................................................................................... 71
BÀI 5: TÌM KI M ....................................................................................................... 72
5.1 GI I THI U V B I TO N T M KI M ................................................................ 72
5.2 T M KI M TUY N T NH..................................................................................... 73
........................................................................................ 74
.................................................................................................... 76
P ......................................................................................................... 77
6.1 GI I THI U V B I TO N S P X P .................................................................. 77
..................................................................................................... 77
6.1.2 ........................................................................................................... 77
PH C T P C A GI I THU T........................................................................ 78
........................................................... 78
................................................................ 79
................................................................................ 80
6. t.................................................................................... 80
............................................................................ 82
................................................................................ 83
................................................................................. 85
t ...................................................................................... 87
..................................................................................... 89
.................................................................................... 91
..................................................................................... 93
.................................................................................................... 95
............................................................................................ 96
IV

M C

C/C++. Ngoài ra,


h

- ,c

- Hi .

- .

- và nâng cao.

- B i 1:

- B i 2:

- B i 3:

- B i 4:

- B i 5:

- B i 6:

C t v logic v ki n th c to n h n.
V

L C, là môn h
gi p sinh viên l m quen v p l p tr
trên m y t nh gi p sinh viên c kh i ni m n v
c ch ti p c n v gi i quy t c c b i to , gi p sinh viên c kh p
c n v i c i l p tr nh, v l ti p c n v i c c h c ph n
quan tr ng c n l i c a ng nh Công ngh Thông tin. V v y, yêu c
u t i li c khi
n l p và g ch chân nh ng v không hi c t i li nl i.

1. trình (ch )

2. -
BÀI 1: 1

BÀI 1:

- c

1.1 KHÁI NI M

khi ta

1.2 M NG M T CHI U
1.2.1 Khai báo
Kha

Cú pháp:

Trong :

- <Tên
2 BÀI 1:

songuyen

int songuyen [10];

sothuc

float sothuc [15];

K daykytu

char daykytu [30];

Cú pháp:

Cách

Cú pháp:

float x[] = {12.1, 7.23, 5.0, 27.6, 87.9, 9.31};

sizeof()

nhapmang (int a[ ], int n);


BÀI 1: 3
1.2.2 Truy c p vào các ph n t c a m ng
M theo sau là

Ch

Ví d : :

float a [5];

a[0], a[1], a[2], a[3], a float

float t = 10.0;

int i = 1;

a[0] = 4.2;

a[2] = t;

a[i] = (a[0] + a[2]) / 2;

printf("\nGia tri: %f ", a[1]);

scanf("%f", &a[4]);

t = a[4];

a[2 * i + 1] = a[2 * i] + a [2 * i + 2];


4 BÀI 1:

float x[6] = {12.1, 7.23, 5.0, 27.6, 87.9, 9.31};

x[1]=7.23, x[2]=5.0, x[3]=27.6, x[4]=87.9, x[5]=9.31

1.2.3 Nh p d li u cho m ng m t chi u


K int a [100]

0 1 2 3 4 .... n-3 n-2 n-1


7 3 9 4 5 .... 8 12 2
a[0] a[1] a[2] a[3] a[4] .... a[n-3] a[n-2] a[n -1]

a[ 2 ] = 9 scanf (

...................................................

a[ n- -1 ]);

Ví : Hàm

void NhapMang (int a[], int n){

for (int i = 0 ; i < n ; i++){

printf ( :

scanf ( ]);

1.2.4 Xu t d li u cho m ng m t chi u

K uyên:
BÀI 1: 5
int a[100];

0 1 2 3 4 .... n-3 n-2 n-1


7 3 9 4 5 .... 8 12 2
a[0] a[1] a[2] a[3] a[4] .... a[n-3] a[n-2] a[n-1]

a [2] = 9 print

...................................................

a [n-1] = 2 -1]);

void XuatMang (int a[], int n){

for (int i =0 ; i<n ; i++){

]) ;

1.2.5 M t vài thu t toán trên m ng m t chi u

Bài toán 1:

phát 200 bytes


6 BÀI 1:

0 1 2 3 4 .... n-3 n-2 n-1


7 3 9 4 5 .... 8 12 2
a[0] a[1] a[2] a[3] a[4] .... a[n-3] a[n-2] a[n-1]

- 1]

ng

for (int i= 0 ; i<n ; i++)

S= S + a[i] ;

long TinhTong (int a[ ], int n) {

long s = 0;
for (int i = 0; i < n; i++)
s = s + a[i];

return s;
}

Bài toán 2

0 1 2 3 4 .... n-3 n-2 n-1


a[ ] 7 3 -9 4 5 .... 8 12 2

it

for (int i= 0 ; i<n ; i++)

if (a[i] < 0)
BÀI 1: 7
.

return a[i];

int AmDau (int a[ ], int n) {

for (int i = 0; i < n; i++)


if (a[i] < 0)
return a[i];

return 1;
}

1.2.6 Truy n tham s m ng m t chi u cho hàm

long TinhTong (int a[ ], int n);

Hàm tính x

int TimX (int a[ ], int n, int x);

1.3 CHU I KÝ T (M NG M T CHI U CÁC KÝ T )


d

t .

Chú ý ng có
8 BÀI 1:

1.3.1 Cách khai báo chu i


Khai

char

char Hoten [20];

khai báo

V char [

char chuoi[50] NG

char name [] \

char ten[10] = \

khi

1.3.2 L i khi t o m t chu i


1. Chú ý: Không này sang

char a[4] =
char b[4];

b = a; /
BÀI 1: 9
2. Không ==, !=, <, > .

if(a==b) // Không so sánh

1.3.3 Nh p, xu t chu i
N

Cách 1:

char s[30];

Cách 2:

g phím space, tab, new line, Enter thì


.

Cách 1: g

Cách 2:

1.4 M NG HAI CHI U


1.4.1 Khai báo

Cú pháp:
10 BÀI 1:

- :

- dòng dòng
nhiêu.

-
nhiêu.

- g
là bao nhiêu (hay nói cách khác, .
.

Khai

int a [8][14];

Khai 10 hàng, 5
float

float b [10][5];

1.4.2 Truy c p vào các ph n t c a m ng


M theo sau là

a[0][0] là

ng có .

các thao tác


BÀI 1: 11
4 * , v

0 1 2 3 4 5 6 7
0 [0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6] [0][7]
1 [1][0] [1][1] [1][2] [1][3] [1][4] [1][5] [1][6] [1][7]
2 [2][0] [2][1] [2][2] [2][3] [2][4] [2][5] [2][6] [2][7]
3 [3][0] [3][1] [3][2] [3][3] [3][4] [3][5] [3][6] [3][7]

1.4.3 Nh p d li u cho m ng hai chi u


V uyên,

#define d 30 // d

#define c 30 // c là

int a [d ][c] ;

d dòng c
m d hàng :

0 1 2 3 4 5 c-1
0 1 2 3 4 5 6 7
1 10 11 12 13 14 15 17

d-1 20 21 22 23 24 25 27

- :

a [ 0 ][ 0 ] = 1 scanf ( a[0 ][0]);

a [ 0 ][ 1 ] = 2 scanf ( [0][1]);

a [ 0 ][ 2 ] = 3 scanf ( [0][2]);

...................................................

a [ 0 ][ c-1 ] = 2 scanf ( a[0][c-1 ]);


12 BÀI 1:

p d

for (int j = 0; j < c; j++)

scanf ( ;

- :

a [ 1 ][ 0 ] = 1 scanf ( [0]);

a [ 1 ][ 1 ] = 2 scanf (

a [ 1 ][ 2 ] = 3 scanf (

...................................................

a [ 1 ][ c-1 ] = 2 scanf ( -1 ]);

for (int j = 0; j < c; j++)

scanf (

..................................................................................

- :

a [ d-1 ][ 0 ] = 1 scanf ( -1 ][0]);

a [ d-1 ][ 1 ] = 2 scanf ( -1 ][1]);

a [ d-1 ][ 2 ] = 3 scanf ( -1 ][2]);

...................................................

a [ d-1 ][ c-1 ] = 2 scanf ( -1][c-1 ]);

dòng d:

for (int j = 0; j < c; j++)

scanf ( -1][j]) ;
BÀI 1: 13
:

for (int i = 0; i<d; i++)


for (int j=0; j< c; j++){
printf ( hap a[
a[ i ][ j ]);
}

:
void NhapMatran (int a[][10], int d, int c){
for (int i =0; i<d; i++)
for (int j=0; j< c; j++){

j ]);
}
}

1.4.4 Xu t d li u cho m ng hai chi u


T d dòng :
void XuatMatran (int a[][10], int d, int c){
for (int i = 0; i < d; i++) {
for (int j = 0; j < c; j++)
printf (
printf ( \
}
}

1.4.5 Truy n tham s m ng 2 chi u cho hàm


T

#define MAX_SIZE 10

typedef MATRAN[MAX_SIZE][MAX_SIZE];
14 BÀI 1:

Tên_hàm(MATRAN

#define MAX_SIZE 10
typedef int MATRAN[MAX_SIZE][MAX_SIZE];
long TongMatran (MATRAN a, int d, int c){
long s = 0;
for (int i = 0; i < d; i++)
for (int j = 0; j < c; j++)
s = s + a[i][j];
return s;
}

#define MAX_SIZE 10
typedef int MATRAN[MAX_SIZE][MAX_SIZE];
long TongHang (MATRAN a, int c, int k){
long s = 0;
for (int j = 0; j < c; j++)
s = s + a[k][j];
return s;
}
BÀI 1: 15

Câu 1:

a.

b.

c.

d.

e.

f.

g.

h.

i.

j.

k.

l.

Câu 2:

a.

b.

c.

d.
16 BÀI 1:

e.

f.

g. .

Câu 3:

Câu 4: các hàm sau:

a. d dòng và c d, c

b.

c.

d.

e.

f.

g. )

h.
BÀI 2: 17

BÀI 2:

2.1 KHÁI NI M V A CH Ô NH VÀ CON TR


Các ã bi d ng tr c ây u là bi c và k
d nh. Ng i ta g i các bi k ày là bi áo b
m t l ng ô nh n này s c mà b trong quá
trình th c thi ch ng trình d ng h ng ô nh hay không.
b d ng này s t t i gian t c thi ch ng
b mà ch trình s d ng 1 l ib .

: int x=4;

X 1024 4

M t s h ch h g ph khi d ng c

1. phát ô nh d , gây ra lãng phí ô nh .

2. phát ô nh ch ng trình t c thi b l i.


18 BÀI 2:

tránh nh ng h n ch g p cho ta m t lo
i là bi ng v i các sau:

1. sinh tr

2.

3.

Tuy nhiên các ng không có h h nên ta không th


chúng g p cho ta m t lo a kh
ph ó là bi r inter) i các m:

1. n c

2.

2.2 KHAI BÁO VÀ S D NG BI N CON TR


2.2.1 Khai báo bi n con tr

Cú pháp: <K T

Ý ngh a: Khai báo m a ch i


có ki Ki

Ví : Kh b có ki i r

int a, b, *pa, *pb;

Ví f ki u

float f, *pf;

2.2.2 Các thao tác trên con tr

2.2.2.1 Gán a ch c a bi n cho bi n con tr

Toán & dùng nh con tr m


BÀI 2: 19
Cú pháp: =

Gi :T a ch Tên bi .

Ví ;

Gán i r &x

Lúc c mô t

1024: 4
x
1024:
xp
2.2.2.2 L y giá tr c a bi n con tr ch t i

i dung c a ô nh m h i, ta s d ng cú pháp:

*<Tên b n con tr >

V i cách truy p này thì *<Tên bi r > có t m u c


h áo bi r .

ây cho phép khai báo, gán h ng nh y n i dung vùng nh a


bi n con tr :

int x = 100, *ptr;

ptr = &x;

int y = *ptr;

: Khi gán h am t m r ,m is i trên i


dung ô nh r ch is thay i theo (t c ch n i dung ô
nh i h t).

Ví d o n ch rình sau th i này:

#include <stdio.h>

#include <conio.h>
20 BÀI 2:

int main() {

int a, b, *pa, *pb;

a = 2;

b = 3;

printf("\nGia tri cua bien a = %d\nGia tri cua bien b = %d", a, b);

pa = &a;

pb = &b;

printf("\nNoi dung cua o nho con tro pa tro toi = %d", *pa);

printf("\nNoi dung cua o nho con tro pb tro toi = %d ", *pb);

*pa = 20; // Thay i giá t c a *pa

*pb = 20; // Thay i giá tr c a *pb

printf("\nGia tri moi cua bien a = %d // a=20

\nGia tri moi cua bien b = %d", b); // b=20

return 0;

u t h

int x, y, *px , *py;

x = 95; // Truy nh

px = &x;

py = px; // B
BÀI 2: 21
y = *px; // Gán y=x

*py =17; //

1.

2.

2.3 CÁC PHÉP TOÁN TRÊN CON TR

1. Phép gán

2.

3.

4. Phép so sánh

2.3.1 Phép gán

int x=1, *pi, *qi;

pi = &x;

qi = pi;

int x;

char *pc;

pc = (char*
22 BÀI 2:

2.3.2 a ch
T c - v i1s guyên N nào ó; k u v
con tr . ch vùng nh vùng nh a con h ph n t .

float x[30], *px;

px = &x[10];

Cho b

x[10+i]

px- x[10-i] .

Phép tr r cùng k s v nguyên (int).

o ng cách (s t ) gi a 2 con tr ó.

C ng h n

trong ví d trên pc-pa=4.

NULL: là con tr a h Ta có th r ULL cho


1 con tr có ki b k .

L : Ta không th ng 2 con tr i nhau.

2.3.3 Phép truy nh p b nh

N float tru 4 int 2


char 1 byte.

float *pf;

int *pi;

char *pc;
BÀI 2: 23
:

Chú ý

2.3.4 Phép so sánh


1. C

2.

3.

4.

2.4 S D NG CON TR C P PHÁT VÀ THU H I


B NH NG

1. malloc

2. calloc

3. realoc

4. new

: khi
malloc, calloc, realoc free.

N new
delete
24 BÀI 2:

2.4.1 Các hàm c p phát vùng nh

2.4.1.1 Hàm malloc


Cú pháp: void *malloc(size_t n);

n size_t.

: dùng hàm malloc

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <process.h>

int main( ) {

char *str;

/* allocate memory for string */

str = (char *) malloc(10);

if(str==NULL){

printf("Not enough memory to allocate buffer\n");

exit(1); /* terminate program if out of memory */

strcpy(str, "Hello"); /* copy "Hello" into string */

printf("String is %s\n", str); /* display string */

free(str); /* free memory */

return 0;

}
BÀI 2: 25
2.4.1.2 Hàm calloc

Cú pháp: void *calloc(size_t nItems, size_t size);

nItems*size

dùng hàm calloc

#include <stdio.h>

#include <alloc.h>

#include <string.h>

int main() {

char *str = NULL;

/* allocate memory for string */

str = (char *) calloc(10, sizeof(char));

strcpy(str, "Hello"); /* copy "Hello" into string */

printf("String is %s\n", str); /* display string */

free(str); /* free memory */

return 0;

1.

2.

2.4.1.3 Hàm realloc

Cú pháp: void* realloc(void *ptr, unsigned size);

Trong

- ptr
26 BÀI 2:

- size

Ví d : int a, *pa;

pa = (int*) malloc (10) ; h c (10 x 2 ) byte*/

pa l h c 16 x2 byte*/

2.4.2 Toán t new và delete (trong C++)

M .

, ta new

Cú pháp:

các

new

double *p = NULL;

p = new double;

double *p = NULL;

if (!(p = new double)) {

exit(1);
BÀI 2: 27
}

m nào

delete trong

Cú pháp:

2.4.3 Thu h i b nh ng

1. K

2. free delete.

2.4.4 Toán t sizeof:

u d int, float,
typedef, enum, struct, union,

Khai báo:

typedef float KieuThuc;

struct DiemThi {

char masv[8];

char mamh[5];

int lanthi;

float diem;
28 BÀI 2:

} x;

sizeof (int)

sizeof (KieuThuc)

sizeof (x)

2.5 CON TR VÀ M NG M T CHI U


Gi a m r m t s ch h h ph c a m ng có
th c xác ng ch s m ó chúng ng có th c xác
i r .

2.5.1 Truy c p các ph n t m ng theo d ng con tr


Ta

&<Tên ng>[0] t ng ng v i <Tên m ng>

&<Tên trí>] ng ng v i <Tên m ng> +

<Tên m ng>[ ng ng i *(<Tên m ng> +

Ví d : Cho 1 m i s có n ph c c t
theo ki u m r .

#include <stdio.h>

#include <conio.h>

/* Cách 1: Nh p m ng bình th ng*/

void NhapMang (int a[], int n)

for( int i=0; i<n; i++)

printf("Phan tu thu %d: ", i);

scanf("%d", &a[i]);

}
BÀI 2: 29
}

/*Cách 2: Nh p m ng theo d ng con tr */

void NhapContro (int *a, int n)

for(i=0; i<n; i++)

printf("Phan tu thu %d: ", i);

scanf("%d", a+i);

2.5.2 Truy c p t ng ph n t c qu n lý b i con tr


theo d ng m ng
<Tên bi trí>] v *(<Tên

&<Tên b v (<Tên bi Trong ó

<Tên bi r ,

là 1 bi h c s

Ví d s khai báo:

#include <stdio.h>

#include <alloc.h>

#include <conio.h>

int main()

int *a;

a = (int*)malloc(10);

for(i=0; i<10; i++)


30 BÀI 2:

a[i] = 2*i;

printf("Truy cap theo kieu mang: ");

for(i=0; i<10; i++)

printf("%d ",a[i]);

printf("\nTruy cap theo kieu con tro: ");

for(i=0; i<10; i++)

printf("%d ",*(a+i));

return 0;

u ch

2.5.3 Bài toán minh ho


D yêu c sau:

1. N .

2. .

3. .

int *p;

p = (int *) malloc (n); //

= new int[n];

P
1024
BÀI 2: 31
P P +1 P +2 P +3 P +4 P +5 P +6 P +7 P+n-1
1 2 3 4 5 6 7 8 n
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[n-1]

Cài t:

1. H :

void Nhapmang (int *a, int n)

{
for (int i=0; i<n; i++)

{ hap a[%d]:", i );

", (a+ i));

}
}
2. H :

void Xuatmang ( int *a, int n)

{
for (int i=0; i<n; i++)

printf ("%4d", *(a+i));

3. H :

long Tongmang ( int *a, int n)

long s = 0;

for (int i=0; i<n; i++)

s = s + *(a+i));

return s;

4. Hàm
32 BÀI 2:

int main()

int *p;

p = (int*)malloc (100);

// hoac p = new int[100];

return 0;

2.6 CON TR VÀ M NG HAI CHI U


2.6.1 Bài toán minh ho

D :

1. N
n<=100).

2. Xu .

3.

2.6.2 Cách 1

int *a;

*a

1024 0 1 2 ... n-1


0 1 2 3 4 5
1 6 7 8 9 10
... 11 12 13 14 15
m-1 16 17 18 19 20

a = (int *) malloc (m*n))

: a = new int[m][n];
BÀI 2: 33

a a +1 a +2 a +3 a +4 a +5 a +6 a+mxn-1
1 2 3 4 5 6 7 mxn
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[0][6] a[m-1][n-1]

*(a + i*n + j);

C :

1. H :

void NhapMang (int * a, int m, int n)

{
for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
{
hap a[%d][%d]:", i , j);
%d ", (a+ i*n + j));
}
}
2. H :

void XuatMang (int * a, int m, int n)

for (int i=0; i<m; i++)

for (int j=0; j<n; j++)

printf ("%4d", *(a+i *n +j));

printf("\n");

}
34 BÀI 2:

3. H :

long TinhTong (int * a, int m, int n)

for (int i=0; i<m; i++)

for (int j=0; j<n; j++)

s = s+ *(a+i *n +j);

2.6.3 Cách 2

**a 0 1 2 ... n-1

a 1 2 3 4 5

a+1 6 7 8 9 10

a+i 11 12 13 14 15

a+m-1 16 17 18 19 20

1. : a[0][0], a[0][1], a[0][2],....a[0][n -1]

*a a[0][0] a[0][1] ... a[0][n-1]

a[1][1],a[1][2],....a[1][n-1]

*(a+1) a[1][0] a[1][1] ... a[1][n-1]

-1]

*(a+i) a[i][0] a[i][1] ... a[i][n-1]

- -1: a[m -1][0], a[m-1][1], a[m-


1][2],....a[m-1][n-1]

*(a+m-1) a[m-1][0] a[m-1][1] ... a[m -1][n-1]

: *a, *(a+1), *(a+2)...,


*(a+m-1).
BÀI 2: 35
** a *a * (a+1) * (a+2) ... *(a+m-1)

1. H :

void NhapMang (int** a, int m, int n) {


for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
{
printf( hap a[%d][%d]:", i , j);
scanf ( ", (*(a+i) + j));
}
}
2. H :

void XuatMang (int ** a, int m , int n) {


for (int i=0; i<m; i++) {
for (int j=0; j<n; j++)
printf ("%4d", *( *( a+i ) +j));
printf("\n");
}}
3. H :

long TinhTong (int ** a, int m, int n) {


long s=0;
for (int i=0; i<m; i++)

for (int j=0; j<n; j++)


s = s + *(*( a+i )+j) ;

return s;
}

4. Hà :

void FreeArray (int **a, int m)

{
for (int i=0; i<m; i++)
36 BÀI 2:

free (a[i]) ;
free (a);

2.7 CON TR VÀ CHU I KÝ T


, c

\
.

2.7.1 Cách khai báo chu i


K

Cách 1:

char Hoten [20];

Cách 2:

char *

char *Hoten;

Hoten = (char *) malloc (20*sizeof(char));

2.7.2 Cách nh p/ xu t chu i ký t

Cách 1: <stdio.h>

scanf("format string", argument_list);


BÀI 2: 37
hoten);

phím space, tab, new line, e


dùng .

printf("format string", argument_list);

V : printf oten);

Cách 2:

gets (string);

p e
gets: fflush(stdin) hay flushall()

gets(hoten);

puts (string);

Hàm này x .

puts(hoten);

2.7.3 M t s hàm x lý chu i (trong <string.h>)

1. Hàm strcat:

Cú pháp:

char * strcat(char *s1, char *s2);


38 BÀI 2:

#include "stdio.h"
#include "string.h"
int main() {
char s1[50], s2[50];
printf("\nNhap chuoi 1: ");
gets(s1);
printf("Nhap chuoi 2: ");
gets(s2);
strcat(s1,s2);
printf("Xuat chuoi 1: %s",s1);
printf("\nXuat chuoi 2: %s",s2);
return 0;
}
2. Hàm strchr:

Cú pháp:

char * strchr (char *ch, int kt);

NULL.

#include "stdio.h"
#include "string.h"
int main() {
char s[50], ch, *p;
printf("\nNhap chuoi: ");
gets(s);
printf("Nhap ky tu: ");
ch=getche();
BÀI 2: 39
p=strchr(s,ch);
if (p!=NULL)
printf("\nchi so cua ky tu: %d",(int)(p-s));
else
printf("\nKhong tim thay!");
return 0;
}
3. Hàm strcmp:

Cú pháp:

int strcmp (char *s1, char *s2);

4. Hàm stricmp(): So sánh

Cú pháp:

int stricmp (const char *s1, const char *s2);

5. Hàm strcpy:

Cú pháp:

char * strcpy (char *Des, const char *Source);

#include "stdio.h"
#include "string.h"
int main() {
40 BÀI 2:

char s[50];
strcpy(s,"Truong Dai hoc Ky thuat");
printf("\nXuat chuoi: %s",s);
return 0;
}
6. Hàm strncpy (): Sa

Cú pháp:

char * strncpy(char *Des, const char *Source, size_t n);

7. Hàm strchr():

Cú pháp:

char *strchr (const char *str, int c);

8. Hàm strstr()

Cú pháp:

char * strstr(const char *s1, const char *s2);

hoc

#include<conio.h>
BÀI 2: 41
#include<stdio.h>
#include<string.h>
int main() {
char Chuoi[255], *s ;
printf("Nhap chuoi: ");gets(Chuoi);

return 0;
}

9. Hàm strlen():

Cú pháp:

int strlen (const char *s);

#include <stdio.h>
#include <string.h>
int main() {
char string [ ] = "Borland International";
printf("%d\n", strlen(string));
return 0; }

Ta g

#include<conio.h>
#include<stdio.h>
#include<string.h>
int main() {
char newstr [35];

for(int i = 0; i<strlen(str); i++)


newstr[i] = str[i];
42 BÀI 2:

\
return 0;
}

10. - Hàm toupper()

Hàm toupper()

Cú pháp:char toupper (char c)

11. - Hàm strupr()

Cú pháp:char *strupr(char *s)

#include<conio.h>

#include<stdio.h>

#include<string.h>
int main() {
char Chuoi[255],*s;
printf("Nhap chuoi: "); gets(Chuoi);
s=strupr(Chuoi) ;

return 0;
}
12. - Hàm strlwr()

Cú pháp: char *strlwr (char *s)

13.
BÀI 2: 43

Cú pháp:

int atoi(const char *s)

long atol(const char *s):

atof(const char *s):

14. char* strtok (char *s1, const char *s2)

#include <string.h>
int main() {
char s[80], *p ;
gets(s);
p = strtok(s," ");

while(p) {
p = strtok(NULL," ");
if (p) printf
}
return 0;
}

15.
44 BÀI 2:

2.8 CON TR V I KI U D LI U CÓ C U TRÚC


(STRUCT)
2.8.1 Ví d 1

typedef struct DIEM

char masv[8];

char mamh[5];

int lanthi;

float diem;

};

DIEM *p, x;

P = &x;

hai cách sau:

Cách 1:

Cú pháp -

: printf("%f ", p->diem);

Cách 2:

Cú pháp

printf("%f ", (*p).diem);

2.8.2 Ví d 2

#include "stdio.h"

#include "conio.h"
BÀI 2: 45
struct DIEM {

char masv[8];

char mamh[5];

int lanthi;

float diem;

};

int main() {

struct DIEM x,*p;

float tam;

p=&x;

printf("\nNhap ma so sinh vien :");

gets(p->masv);

printf("Nhap ma mon hoc : ");

gets(p->mamh);

printf("Lan thi :");

scanf("%d ", &p->lanthi);

printf("diem :");

scanf("%f ", &tam);

p->diem=tam;

printf("\nKet qua:");

printf("\nMa so sinh vien: %s", (*p).masv);

printf("\nMa mon hoc: %s", (*p).mamh);

printf("\nLan thi: %d", (*p).lanthi);

printf("\ndiem: %.2f ", (*p).diem);

return 0;

}
46 BÀI 2:

2.8.3 Truy n tham s ki u c u trúc cho hàm


V :

#include "stdio.h"

#include "conio.h"

typedef struct DIEMTHI

char masv[8];

char mamh[5];

int lanthi;

float diem;

};

void nhap(DIEMTHI *px);

void xuat(DIEMTHI x);

int main()

{
DIEMTHI x;
printf("\nNhap diem thi");
nhap(&x);
printf("\nXuat diem thi");
xuat(x);
getch();
}
void xuat( DIEMTHI x)
{
printf("\nMa sinh vien :%s", x.masv);
printf("\nMa mon hoc:%s", x.mamh);
printf("\nLan thi:%d", x.lanthi);
BÀI 2: 47
printf("\nDiem thi:%.2f", x.diem);
}
void nhap(DIEMTHI *px)
{
float tam;
printf("\nMa sinh vien :");
gets(px->masv);
printf("Ma mon hoc:");
gets(px->mamh);
printf("Lan thi:");
scanf("%d%*c", &px->lanthi);
printf("Diem thi:");
scanf("%f%*c", &tam);
px->diem=tam;
}
48 BÀI 2:

- Khái ni v

- Cách khai báo i

- m

- m

Câu 1: thao tác trên :

a.

b.

c.

d.

e.

f.

g.

h. Tìm

i.

j.

k.
BÀI 2: 49
l.

m.

n. n

o.

p.

q.

r.

s.

t.

u.

Câu 2: :

a. (0< m, n
<100)

b.

c.

d.

e. T

f.

g.

h. c

i.

j. Tính

k.

l.

m.
50 BÀI 2:

n. T

o.

p.

q.

r.

s.

t. Tín

u.

v.

Câu 3: các hàm sau:

a.

b. X

c. X

d. T

e. T

Câu 4: r

a.

b.

c.

d.

e.

f.

Câu 5: t
BÀI 2: 51
a.

b. ên.

c. h viên.

d. .

e.
phím.

f.

g.

h. sinh viên theo tên

i.

j.
52 BÀI 3:

BÀI 3:
S

- quy;

3.1 KHÁI NI M

Ta có:

S(n) = 1 + 2 + 3 + 4 +.. + (n-1)+ n = S(n-1) + n;

Cách 1:

long TinhTong (int n)


{
long s = 0;
for(int i = 1; i <= n; i++)
s = s + i;
return s;
}
BÀI 3: 53
Cách 2: quy:

long TinhTong (int n) {

if (n == 0)

return 0;

return TinhTong (n 1) + n;

3.2 K THU T GI I BÀI TOÁN B QUY


1. Tham

2.

3. Tìm t

- Tham [ ], int n

Sum(a,n) = a[0] + a[1] +... + a[n-3] +a[n-2] + a[n-1]

Sum(a,n-1)

Sum(a,n) = 0 ,n= 0

a[n 1] + Sum (a, n-1), n>0

C :

long TongMang (int a[ ], int n) {


if (n==0) return 0;
return a[n-1] + TongMang (a, n-1);
}
54 BÀI 3:

3.3 M T S NH N XÉT V QUY


1.

2.

3.

4.

5.
không- -quy.

6.

3.4 SO SÁNH C U TRÚC L QUY


1. l

2.

3.

4. g

a.

b. .

c.
.

d.
n.

5.

a.
BÀI 3: 55
b. á
.

c.

3.5 PHÂN LO QUY

(1)

(2)

(3)

(4)

3.5.1 quy tuy n tính


T

a ,n=1
Un = r + Un-1 ,n>1

double U (int n, double a, double r)

if (n==1)

return a;

return r + U(n-1,a,r);

3.5.2 quy nh phân

T
56 BÀI 3:

: 1 2 3 5 8 13 ...

1 , n=1, 2
Un =
Un-2 + Un-1 , n>21

u:

long Fibo (int n)

{
if (n<=2)

return 1;
return Fibo(n-2) + Fibo(n-1);
}

3.5.3 quy phi tuy n


Thân hàm

Un = n , n ,n <6
Un =
Un-5 + Un-4 + Un-3 + Un-2 + Un-1 ,n >6

nh sau:

long U (int n)

if (n<6)

return n;

long S= 0;

for (int i = 5; i>0; i--)

S += U(n-i);

return S;

}
BÀI 3: 57
3.5.4 quy h
2

Un = n , n<5
Un =
Un-1 + Gn-2 , n>=5

Gn = n-3 , n<8
Gn =
Un-1 + Gn-2 , n>8

long G(int n);

long U (int n)

if (n<5)

return n;

return U(n-1) + G(n-2);

long G(int n)

if (n<8)

return n-3;

return U(n-1) + G(n-2);

}
58 BÀI 3:

Câu 1:
(0<n<100)

Câu 2:

Câu 3:

Câu 4:

Câu 5:

Câu 6: Tính

Câu 7:

Câu 8:

Câu 9:

Câu 10: g

Câu 11:

Câu 12:

Câu 13: g
BÀI 4: 59

BÀI 4:

4.1 KHÁI NI M

l
ngoài.

ngoài).

1.
SCII
ý
\ -
mã ASCII là 13) và LF (Line Feed - ASCII
ASCII là
Ctrl + Z).
60 BÀI 4:

2.
c byte

3. (Untyped File)

ên

(End Of File).

4.2 CÁC THAO TÁC TRÊN T P TIN

1.

2. ng hàm fopen().

3.

4.

stdio.h.

4.2.1 Khai báo bi n t p tin


Cú pháp:FILE <Danh sách

: FILE *f1, *f2;


BÀI 4: TIN (FILE) 61
4.2.2 M t p tin
Cú pháp: FILE *fopen(char *Path, const char *Mode)

- Path

- Type á Mode:

r
w
a
rb
wb
ab i
r+
w+
a+
r+b
w+b
a+b hân

Hàm fopen

:\\

FILE *f;
:\\
if (f != NULL) {
/* Các câu l nh thao tác v i t p tin*/
/* óng t p tin*/
}
62 BÀI 4:

có thành công hay không?

c w i

4.2.3 t p tin

Hàm fclose() ù

Cú pháp: int fclose (FILE *f)

Cú pháp: int fcloseall()

4.2.4 Ki n cu i t

Cú pháp: int feof (FILE *f)

4.2.5 Di chuy n con tr t p tin v u t p tin - Hàm rewind()

K
rewind().

Cú pháp: void rewind (FILE *f)


BÀI 4: 63
4.3 TRUY C P T N
4.3.1 Ghi d li u lên t p tin v n b n

Hàm putc()

Cú pháp: int putc (int c, FILE *f)

Hàm fputs()

Cú pháp: int puts (const char *buffer, FILE *f)

ghi vào. Hàm

Hàm fprintf()

Cú pháp: fprintf (FILE *f, const char *format, varexpr)

- format các hàm printf()).

- varexpr

%d

%o phân
%x
%c
%s
%e %E %g
64 BÀI 4:

: \\Baihat.txt

#include<stdio.h>
#include<conio.h>
int main () {
FILE *f;
f = fopen ("D:\\Baihat.txt", "r+");
if (f != NULL) {
fputs("Em oi Ha Noi pho.\n", f);
fputs("Ta con em, mui hoang lan; ta con em, mui hoa sua.", f);
fclose(f);
}
return 0;
}
o

4.3.2 cd li u t t n
Hàm getc()

Cú pháp: int getc (FILE *f)

ASCII
ic

Hàm fgets()

Cú pháp: char *fgets (char *buffer, int n, FILE *f)

\
c
BÀI 4: 65

- buffer

- n

- f

- \

Hàm fscanf()

Cú pháp: fscanf (FILE *f, const char *format, varlist)

format:

varlist: hau

: trình chép tin D:\Baihat.txt trên sang tin


D:\Baica.txt.

#include<stdio.h>
#include<conio.h>
int main() {
FILE *f1,*f2 ;
f1=fopen("D:\\Baihat.txt", "rt");
f2=fopen("D:\\Baica.txt", "wt");
if (f1 != NULL && f2 != NULL) {
int ch=fgetc (f1);
while (! feof (f1)) {
fputc(ch,f2);
ch=fgetc(f1);
}
66 BÀI 4:

fcloseall();
}
return 0;
}

4.4 TRUY C P T P TIN NH PHÂN


4.4.1 Ghi d li u lên t p tin nh phân - Hàm fwrite()
Cú pháp: size_t fwrite(const void *ptr, size_t size, size_t n, FILE*f)

- ptr

- n:

- size

- f

ày

4.4.2 cd li u t t p tin nh phân - Hàm fread()

Cú pháp: size_t fread (const void *ptr, size_t size, size_t n, FILE *f)

- ptr

- n

- size

- f

tin
BÀI 4: (FILE) 67
#include<stdio.h>
#include<conio.h>
int main() {
FILE *f;
f=fopen ("D:\\CacSo.txt", "wb";
if (f != NULL) {
double d=3.14;
int i=101;
long l=54321;
fwrite (&d, sizeof(double), 1, f);
fwrite(&i, sizeof(int), 1, f);
fwrite(&l, sizeof(long), 1, f);
/* Doc tu tap tin*/
rewind(f);
fread (&d, sizeof(double), 1, f);
fread(&i, sizeof(int), 1, f);
fread(&l, sizeof(long), 1, f);
printf("Cac ket qua la: %f %d %ld", d, i, l);
fclose(f);
}
return 0;
}

c
sách lên màn hình, tìm

t
68 BÀI 4:

#include<stdio.h>

#include<conio.h>

#include<string.h>

typedef struct {

char Ma[10];

char HoTen[40];

} SinhVien;

//----------- -------------------------- -------------------------------------------

void WriteFile (char *FileName) {

FILE *f;

int n,i;

SinhVien sv;

f=fopen(FileName,"ab");

printf("Nhap bao nhieu sinh vien? ");

scanf("%d",&n);

fflush(stdin);

for(i=1;i<=n;i++) {

printf("Sinh vien thu %i\n",i);

printf(" - MSSV: ");

gets(sv.Ma);

printf(" - Ho ten: ");

gets(sv.HoTen);

fwrite(&sv,sizeof(sv),1,f);

fflush(stdin);

fclose(f);

printf("Bam phim bat ky de tiep tuc");

getch();

}
BÀI 4: 69
//----------- ---------------------------------------------------------------------

void ReadFile(char *FileName) {

FILE *f;

SinhVien sv;

f=fopen(FileName,"rb");

printf(" MSSV | Ho va ten\n");

fread (&sv,sizeof(sv),1,f);

while (!feof(f)) {

printf(" %s | %s\n",sv.Ma,sv.HoTen);

fread(&sv,sizeof(sv),1,f);

fclose(f);

printf("Bam phim bat ky de tiep tuc!!!");

getch();

//----------- -------------------------- -------------------------------------------

void Search(char *FileName) {

char MSSV[10] ;

FILE *f ;

int Found=0;

SinhVien sv;

fflush(stdin);

printf("Ma so sinh vien can tim: ");

gets(MSSV);

f=fopen(FileName,"rb");

while (!feof(f) && Found==0) {

fread(&sv,sizeof(sv),1,f);

if (strcmp(sv.Ma,MSSV)==0)

Found=1;
70 BÀI 4:

fclose(f);

if (Found == 1)

printf("Tim thay SV co ma %s. Ho ten la: %s",sv.Ma,sv.HoTen);

else

printf("Tim khong thay sinh vien co ma %s",MSSV);

printf("\nBam phim bat ky de tiep tuc!!!");

getch();

//----------- -------------------------- -------------------------------------------

int main() {

int c;

for (;;) {

printf("1. Nhap DSSV\n");

printf("2. In DSSV\n");

printf("3. Tim kiem\n");

printf("4. Thoat\n");

printf("Ban chon 1, 2, 3, 4: ");

scanf("%d",&c);

if(c==1)

WriteFile("d:\\SinhVien.Dat");

else if (c==2)

ReadFile("d:\\SinhVien.Dat");

else if (c==3) Search("d:\\SinhVien.Dat");

else break;

return 0 ;

}
BÀI 4: 71

CÂU
Câu 1: V

a.

b.

c. và cho phép

Câu 2:

a. ê

b.

c.

Câu 3:

a.

b.

c.
72 BÀI 5: TÌM K I M

BÀI 5: TÌM KI M

5.1 GI I THI U V B I TO N T M KI M
n d

g này là khóa (key)

V d x trong danh s ch sinh viên, v i c c thông tin {M


SV, h a ch kh a t m ki m l h tên. K t qu tr v l : thông tin
c ho c s th t c trong danh s ch.

i to n:

a0, a1, ..., an - 1


BÀI 5: TÌM K I M 73
- Tìm

5.2 M TUY N T NH

D
h.

B1: i = 0;

a
a[i]
B3: i = i + 1 m
n
n

-
int Search(int a[], int n, int key)
{
int i =0;
while (i<n && key != a[i])
i++;
if (i < n)
return i;
return -1;
}

.
74 BÀI 5: TÌM K I M

5.3 TÌM KI M NH PHÂN


P k[2] ...
k[n].

..., x;

=x

<x x

Tìm x

>x x

Tìm x ight-1]

> right

B1: left =0, right = n-1

B2: mid = (left + right)/2

a[mid] = x

a[mid] < x: p trong dãy a[mid+1]...a[right]

left = mid +1;

a[mid] > x: -1]

right = mid -1;


BÀI 5: TÌM K I M 75
B3:

int BinarySearch(int a[], int n, int x){


int left = 0, right = n-1, mid;
while (left <= right){
mid = (left + right)/ 2;
if (a[mid] == x)
return mid;
if (a[mid] < x)
left = mid+1;
else
right = mid-1;
}
return -1;
}

Th t
76 BÀI 5: TÌM K I M

Câu 1: Sinh mn (-100, 100)

a.

b.

Câu 2: char[10], Tên sách: char[40], Giá: long)

a. n

b. Tìm x x
phím).

c. x x
phím).

d.

t.
BÀI 6: P 77

BÀI 6: S P X P
Sau

6.1 GI I THI U V B I TO N S P X P
6.1.1 Gi i thi u

trúc

- T n t

6.1.2 Mô t
S

c
78 BÀI 6: P

n.

Mô t bài toán:

n tin

tin, c
tin : So sánh

6.2 C T P C A GI I THU T
6.2.1 Khái ni m v ph c t p c a gi i thu t

T
y tính.

p trong

vào, ký

0.

T(n) = C*n
BÀI 6: P 79

ào có cùng kích

ãy

nh

0
N0

Nói cách khác

- (T(n)=C)

- O(log2

- O(

- O(nlog2

- O(n2), O(n3 O(n

- O(n!), O(nn

t p

6.2.2 ph c t p c a gi i thu t
Q là:

O(max(f(n),g(n)))

:
80 BÀI 6: P

O(f(n).g(n))

n b b a
1 n 1 1 1 b a 1
i 1 i a i 1 i 1

n n
n( n 1) n( n 1) (a 1) a
i i
i 1 2 i a 2

n2
n 2 ( n 2 1)
n
2 n( n 1)(2n 1) n
3 n2 ( n 1) 2
i i i
i 1 2 i 1 6 i 1 4

6.3 CÁC GI I THU T S P X P

Insertion Sort, Interchange Sort, Shell


Sort, Quick Sort, Merge Sort và Radix Sort

6.3.1 Gi i thu t Bubble Sort

pp

B1: i=0;
B2: j=n-1;

a[j] < a[j- a[j] và a[j-1]


j = j -1;
B3: i = i+1;
BÀI 6: P 81
i > n-

A[]

25 55 45 40 10 90 85 35

tiên
:

25 55 45 40 10 90 85 35
j
7 25 55 45 40 10 90 35 85
6 25 55 45 40 10 35 90 85
5 25 55 45 40 10 35 90 85
4 25 55 45 10 40 35 90 85
3 25 55 10 45 40 35 90 85
2 25 10 55 45 40 35 90 85
1 10 25 55 45 40 35 90 85
Sa (10)

- 2,

- -

y.

D
25 55 45 40 10 90 85 35
0 10 25 55 45 40 35 90 85
1 10 25 35 55 45 40 85 90
2 10 25 35 40 55 45 85 90
3 10 25 35 40 45 55 85 90
4 10 25 35 40 45 55 85 90
5 10 25 35 40 45 55 85 90
6 10 25 35 40 45 55 85 90
10 25 35 40 45 55 85 90
82 BÀI 6: P

void BubbleSort(int a[], int n)


{
int i, j;
for(i =0; i < n-1; i++)
for(j=n-1; j >i; j--)
if (a[j-1] >a[j])
Swap(a[j], a[j-1]); a[j] và a[j-1]
}

void Swap(int &x, int &y) {


int tam=x;
x=y;
y=tam;
}

6.3.2 Gi i thu t Interchange Sort


M

mà không tho ,t

B1: i = 0;
B2: j = i +1; /

a[j] < a a[i] và a[j];


j = j +1;
B4: i = i +1;
-
Ngoài ra

D terchange

25 55 45 40 10 90 85 35
BÀI 6: P 83
, i=0:

u
25 55 45 40 10 90 85 35
j
1 25 55 45 40 10 90 85 35
2 25 55 45 40 10 90 85 35
3 25 55 45 40 10 90 85 35
4 10 55 45 40 25 90 85 35
5 10 55 45 40 25 90 85 35
6 10 55 45 40 25 90 85 35
7 10 55 45 40 25 90 85 35
q

25 55 45 40 10 90 85 35
0 10 55 45 40 25 90 85 35
1 10 25 55 45 40 90 85 35
2 10 25 35 55 45 90 85 40
3 10 25 35 40 55 90 85 45
4 10 25 35 40 45 90 85 55
5 10 25 35 40 45 55 85 90
6 10 25 35 40 45 55 85 90

void InchangeSort(int a[],int n){

for(int i=0; i<n-1; i++)

for(int j=i+1; j<n; j++)

if (a[i]>a[j]) Swap(a[i], a[j]);

6.3.3 Gi i thu t Selection Sort

M :

y n-

- - 0.

- 0
84 BÀI 6: P

- Dò tìm trong - 1.

- 1

- - i.

- i .

...

h -

- - - n-
2.

- n-2 -2.

a[mi a a[n-1]

a[i] và a[min]

-2 thì i = i+1

25 55 45 40 10 90 85 35

Ban 25 55 45 40 10 90 85 35
0 10 55 45 40 25 90 85 35
1 10 25 45 40 55 90 85 35
2 10 25 35 40 55 90 85 45
3 10 25 35 40 55 90 85 45
4 10 25 35 40 45 90 85 55
5 10 25 35 40 45 55 85 90
6 10 25 35 40 45 55 85 90
BÀI 6: P 85
lection Sort:

void SelectionSort(int a[], int n){

for(int i = 0; i < n-1; i++) {


int min = a[i], vtmin = i;
for(int j = i+1; j < n; j++)

if (a[j] < min)


{ min = a[j];

vtmin = j;
}

Swap(a[vtmin], a[i]); a
}

void SelectionSort(int a[], int n)


{
for(int i = 0; i < n-1; i++){ -

int vtmin = i;
for(int j = i+1; j < n; j++)

if (a[j] < a[vtmin]) vtmin = j;


Swap(a[vtmin], a[i]); a

}
}

6.3.4 Gi i thu t Insertion Sort

- Xem danh s c a[0].


86 BÀI 6: P

- a
a[0] và a[1].

- a
a[0], a[1] và a[2].

- hèn n-1: chèn a[n-


a[0], a[1], a[n-1].

B1: i = 1; a

B2: x= a[i];

B3 a[pos] a[i- a[i].

B4: a[pos] = x; a[0]...a

B5: i = i +1;

D sách:

25 55 45 40 10 90 85 35

Nút chèn vào


Danh sách sau khi chèn
chèn chèn danh sách a[i]
0 25 55 25, 55
1 25,55 45 25, 45, 55
2 25, 45, 55 40 25, 40, 45, 55
3 25, 40, 45, 55 10 10, 25, 40, 45, 55
4 10, 25, 40, 45, 55 90 10, 25, 40, 45, 55, 90
5 10, 25, 40, 45, 55, 90 85 10, 25, 40, 45, 55, 85, 90
6 10, 25, 40, 45, 55, 85, 90 35 10, 25, 35, 40, 45, 55, 85, 90
BÀI 6: P 87

void InsertionSort(int a[], int n){


int pos, x; a[i]
for(int i=1; i < n; i++){
x = a[i]; pos = i-1;
a[pos] > x) {

a[pos+1] = a[pos];
pos--;
}
a[pos+1] = x; //chè
}
}

6.3.5 Gi i thu t Shell Sort


M

a[0]...a[n-1], cho h n), chia dãy thành h dãy

Dãy con 1: a[0], a[0+h], a[0+2h]...

Dãy con 2: a[1], a[1+h], a[1+2h]...

Dãy con 3: a[2], a[2+h], a[2+2h]...

...

Dãy con h: a[h], a[2h], a[3h]...

g
88 BÀI 6: P

k 0;

dãy con

B3: i = i+1

Shell Sort , n = 8, h = {5, 3, 1}.

10 3 7 6 2 5 4 16

10 3 7 6 2 5 4 16
Dãy
1 5 10
2 3 4
3 7 16
4 6
5 2
5 3 7 6 2 10 4 16
-

Ban
5 3 7 6 2 10 4 16
Dãy
1 4 5 6
2 2 3 16
3 7 10
4 2 7 5 3 10 6 16
-

4 2 7 5 3 10 6 16

2 3 4 5 6 7 10 16.

void ShellSort(int a[], int n, int h[], int k){


BÀI 6: P 89
int step, i, pos, x, len;
for(step = 0; step < k; step++) {
len = h[step];
for(i = len; i < n; i++) { y con
x = a[i];
pos = i len; a[i] trong cùng dãy con

a[pos+len] = a[pos];
pos = pos len; // rong dãy con
}
a[pos+len] = x;
}// end for i
}// end for step

6.3.6 Gi i thu t Quick Sort

T . ,d
quick .

-
l .

-
-
, các ph -
.

- -1 và
-

B1: k right,
Cho x = a[k], i = left, j = right.

B2-
90 BÀI 6: P

B2- ào a[j] > x thì j--;


B2- thì Swap(a[i], a[j])

B3:

j:

Quick S

25 55 45 40 10 90 85 35

m 25 55 45 40 10 90 85 35
40 25 35 10 40 45 90 85 55
35 25 10 35 40
25 10 25
90 45 55 85 90
10 25 35 40 45 55 85 90

void QuickSort(int a[], int left, int right) {


int i, j, x;
x = a[(left+right)/2];
i = left; j = right;
do {
while (a[i] < x) i++; khi a[i] >= x
while (a[j] > x) j--;
if (i <= j) {
Swap(a[i], a[j]);
i++;
j--;
}
} while (i<j);
if (left < j) /* */ QuickSort(a, left, j);
if (right > i) /* */ QuickSort(a, i, right);
}
BÀI 6: P 91
6.3.7 Gi i thu t Merge Sort
M :

sau:

- húng

- 2

void MergeSort(int a[], int n){


int i, j, k, low1, up1, low2, up2, size;
int dstam[MAXLIST];
size = 1;
while (size<n){
low1=0;
k=0;
92 BÀI 6: P

while(low1+size<n){
low2=low1+size;
up1=low2-1;
up2=(low2+size-1<n)? (low2+size-1):(n-1);
for(i=low1,j=low2;i<=up1 && j<=up2;k++)
if(a[i]<=a[j]) dstam[k]=a[i++];
else dstam[k]=a[j++];
for(;i<=up1;k++) dstam[k]=a[i++];
for(;j<=up2;k++) dstam[k]=a[j++];
low1=up2+1;
}
for(i=low1;k<n;i++) dstam[k++ ]=a[i];
for(i=0;i<n;i++) a[i]=dstam[i];
size *=2;
}
}

Merge S

25 55 45 40 10 90 85 35

Hình 6.1:
BÀI 6: P 93
6.3.8 Gi i thu t Radix Sort
K thâ

- a[i] trong dãy a[0] ... a[n-1

- ...

-
0 9.

- n

B1: k = 0; //

B2: ... B[9]

...

B3:

For i=1 to n do

B4:
k = k +1

Dùng Radix S 493 812 715 710 195 437 582 340 385
94 BÀI 6: P

a. b. Phân lô dã
Dãy con Dãy con
0 710 340 0
1 1 710 812 715
2 812 582 2
3 493 3 437
4 4 340
5 715 195 385 5
6 6
7 437 7
8 8 582 385
9 9 493 195

710 340 812 582 493 715 195 710 812 715 437 340 582 385
385 437 493 195
ng t
Dãy con
0
1 195
2
3 340 385
4 437 493
5 582
6
7 710 715
8 812
9

195 340 385 437 493 582 710 715 812

void RadixSort(long a[], int n){


int i, j, d, digit, num;
int h = 10;

long B[10][MAX];
int Len[10]; // kích

for(d = 0; d < MAXDIGIT; d++) { / t hàng


for( i = 0; i < 10; i++)
BÀI 6: P 95
Len[i] = 0;

for(i = 0; i < n; i++) {


digit = (a[i] % h) / (h / 10);

B[digit][Len[digit]++] = a[i];
}
num = 0;

for(i = 0; i < 10; i++)


for(j =0; j < Len[i]; j++)

a[num++] = B[i][j];
h *= 10;
}
} //end RadixSort

Câu 1:

a.

b.

c. Xem danh sách sau khi

d.

Câu 2:

a.
.

b. dan
.
96

1. - - 2003.

2. - 1997.

3. Hoà
- 1999.

4.

5.
2000.

6.

7.

8. Brain W. Kernighan & Dennis Ritchie, The C Programming Language, Prentice Hall
Publisher, 1988.

You might also like