Professional Documents
Culture Documents
Kỹ Thuật Sort Cơ Bản
Kỹ Thuật Sort Cơ Bản
Lý thuyết sort:
SORT1.INP SORT1.OUT
5 10 20 30 40 50
50 40 30 20 10
HD CODE:
int n, a[maxn];
void nhap()
cin>>n;
void xuat()
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
int t=a[i];
a[i]=a[j];
a[j]=t;
int main()
nhap();
sort_b(a,n);
xuat();
return 0;
Phân tích:
2. QUICK SORT
SORT1.INP SORT1.OUT
5 10 20 30 40 50
50 40 30 20 10
HD CODE
#include <bits/stdc++.h>
int n, a[maxn];
void nhap()
cin>>n;
void xuat()
if (l <= r)
int x= a[(l+r)/2];
int i = l;
int j = r;
while (i <= j)
if (i <= j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
i++;j--;
sort(a,a+n);
int main()
nhap();
//QuickSort(a,0,n-1);
sort_stl(a,n);
xuat();
return 0;
}
3.Sắp xếp phân phối (Distribution Counting)
HD Code 1:
#include <bits/stdc++.h>
int n;
mang dem;
vector<int>a;
void nhap()
cin>>n;
for(int i=0;i<n;i++)
int x; cin>>x;
a.push_back(x);
void xuat()
void phanphoi2(vector<int>& a)
dem[a[i]]++;
int t = 0;
while(dem[j]-- > 0)
a[t++] = j;
int main()
nhap();
//countSort(a);
phanphoi2(a);
xuat();
return 0;
HD CODE 2:
#include <bits/stdc++.h>
int n;
mang dem;
vector<int>a;
void nhap()
cin>>n;
for(int i=0;i<n;i++)
{
int x; cin>>x;
a.push_back(x);
void xuat()
void countSort(vector<int>& a)
dem[a[i]]++;
output[dem[a[i]] - 1] = a[i];
dem[a[i]]--;
}
for (int i = 0; i <(int) a.size(); i++)
a[i] = output[i];
int main()
nhap();
//countSort(a);
xuat();
return 0;
VIDU1.INP VIDU1.OUT
53 4
57134
HD:
#include <bits/stdc++.h>
void nhap()
cin>>n;
void xuat()
if (l <= r)
int x= a[(l+r)/2];
int i = l;
int j = r;
while (i <= j)
{
if (i <= j)
int t=a[i];
a[i]=a[j];
a[j]=t;
i++;j--;
void xl()
cin>>k;
QuickSort(a,0,n-1);
cout<<a[k-1];
int main()
{
nhap();
xl();
return 0;
VIDU2.INP VIDU2.OUT
5 3 2
12345
HD
#include <bits/stdc++.h>
using namespace std;
int k,n,a[maxn];
void nhap()
cin>>n>>k;
int g,d=0,c=n-1;
while(d<=c)
g=(d+c)/2;
if(a[g]==k) return g;
else d=g+1;
return -1;
int main()
nhap();
cout<<np(a,n,k);
VIDU3.INP VIDU3.OUT
8 5
6714668 3
HD:
#include <bits/stdc++.h>
int n, a[maxn];
void nhap()
cin>>n;
int dem=1;
for(int i=1;i<n;i++)
if(a[i-1]!=a[i]) dem++;
return dem;
int kq=1;
int dem=1;
for(int i=1;i<n;i++)
if(a[i-1]!=a[i]) dem=1;
else dem++;
if(dem>kq) kq=dem;
return kq;
if (l <= r)
int x= a[(l+r)/2];
int i = l;
int j = r;
while (i <= j)
if (i <= j)
int t=a[i];
a[i]=a[j];
a[j]=t;
i++;j--;
int main()
nhap();
QuickSort(a,0,n-1);
cout<<gtkhac(a,n)<<endl;
cout<<solanlapmax(a,n);
return 0;
VIDU4.INP VIDU4.OUT
66 8
HD:
#include <bits/stdc++.h>
int p,n,k;
long int c[maxvalue];
int main()
cin>>n>>k;
int f_1=1,f_2=1;
memset(c,0,sizeof(c));
c[f_1]=c[f_1]+1; c[f_2]=c[f_2]+1;
for(int i=3;i<=n;i++)
c[f_2]=c[f_2]+1;
c[f_2]=c[f_1];
f_1=fi;
int cv=0;
for(int v=0;v<=maxvalue;v++)
cv=cv+v;
if(cv>=k)
p=v; break;
}
cout<<p;
HD:
int n,x,c[maxn];
void xl()
memset(c,0,sizeof(c));
cin>>n;
for(int i=1;i<n;i++)
{
cin>>x;
if(x<=n) c[x]++;
for(int i=0;i<=n;i++)
if(c[i]==0)
x=i;
break;
cout<<x;
int main()
xl();
return 0;
}
HD:
#include <bits/stdc++.h>
string s;
void xl()
memset(c,0,sizeof(c));
c[0]=1;
sum=0;
dem=0;
for(int i=0;i<(int)s.size();i++)
dem=dem+c[sum];
c[sum]++;
cout<<dem;
int main()
cin>>s;
xl();
return 0;
TK.INP
5 10 0
10 20 30 40 50
for(int i=0;i<n;i++)
if(a[i]==k) return i;
return -1;
NP1.INP NP1.OUT
5 10 0
10 50 100 120 130
10 21 5
2 4 5 6 9 21 22 30 31 33
10 25 -1
2 4 5 6 9 21 22 30 31 33
#include <bits/stdc++.h>
using namespace std;
int k,n,a[maxn];
void nhap()
cin>>n>>k;
int g,d=0,c=n-1;
while(d<=c)
g=(d+c)/2;
if(a[g]==k) return g;
else d=g+1;
return -1;
int main()
nhap();
cout<<np(a,n,k);
NP2.INP NP2.OUT
10 23 22
2 4 5 6 9 21 22 30 31 33
Code:HD
int np1(int x)
int d=1,c=n,g,kq=0;
while(d<=c)
g=(d+c)/2;
if(a[g]<=x)
kq=g;
d=g+1;
else c=g-1;
return kq;
NP3.INP NP3.OUT
10 28 32
2 4 5 6 9 21 22 32 31 33
HD CODE
int np2(int x)
int d=1,c=n,g,kq=0;
while(d<=c)
{
g=(d+c)/2;
if(a[g]>=x)
kq=g; c=g-1;
else d=g+1;
return kq;
VÍ DỤ MINH HỌA
BÀI TẬP ÁP DỤNG
http://ntucoder.net/Problem/Details/5574
http://ntucoder.net/Problem/Details/5571
http://ntucoder.net/Problem/Details/5506
http://ntucoder.net/Problem/Details/4485
http://ntucoder.net/Problem/Details/4408
http://ntucoder.net/Problem/Details/1177
http://ntucoder.net/Problem/Details/4408
http://vnoi.info/problems/list/?tag=56&page=1