-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsapxep.cpp
85 lines (73 loc) · 1.3 KB
/
sapxep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
void Nhapmang(int a[100],int n);
void Xuatmang(int a[100],int n);
void QuickSort(int a[100], int l, int r);
void InsertionSoft (int a[100], int N);
int main(){
int a[100],n;
int l, r, N;
int pos, i;
int x;
printf("Nhap so phan tu cua mang n= ");
scanf("%d",&n);
Nhapmang(a,n);
QuickSort(a,l,r);
Xuatmang(a,n);
InsertionSoft(a,N);
Xuatmang(a,n);
}
void Nhapmang(int a[100], int n)
{
for(int i=0;i<n;i++)
{
printf("Nhap so phan tu cua mang la a[%d]= ",i);
scanf("%d",&a[i]);
}
}
void Xuatmang(int a[100], int n)
{
printf("\n Cac phan tu cua mang n = ");
for(int j=0;j<n;j++)
printf("%d ; ",a[j]);
}
void Hoanvi(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
void QuickSort(int a[100], int l, int r)
{
int i,j;
int x;
x = a[(l+r)/2];
i =l; j = r;
do {
while(a[i] > x) i++;
while(a[j] < x) j--;
if(i <= j)
{
Hoanvi(a[i],a[j]);
i++;j--;
}
}while (i < j);
if(l < j)
QuickSort(a,l,r);
if(i < r)
QuickSort(a,i,r);
}
void InsertionSoft (int a[100], int N)
{
int pos, i;
int x;
for(int i=1;i<N;i++)
{
x=a[i]; pos = i-1;
while((pos >= 0)&&(a[pos] > x))
{
a[pos+1] = a[pos];
pos--;
}
a[pos+1] = x;
}
}