排序
直接插入排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void insert_sort(int n){
int i,j;
for(i=1;i<n;i++){
if(a[i]<a[i-1]){
int temp=a[i];
for(j=i-1;j>=0&&a[j]>temp;j--){
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
}
int main(){
int n,k,j,i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
insert_sort(n);
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
折半插入排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void half_insert_sort(int n){
int mid,left,right,j,i;
for(i=1;i<n;i++){
if(a[i]<a[i-1]){
int temp=a[i];
right=0,left=i-1;
while(right<=left){
mid=(right+left)/2;
if(a[mid]<=temp&&a[mid+1]>=temp){
break;
}
else if(a[mid]<temp){
right=mid+1;
}else{
left=mid-1;
}
}
for(j=i;j>mid;j--){
a[j]=a[j-1];
}
a[mid+1]=temp;
}
}
}
int main(){
int n,k,mid,left,right,j,i;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
half_insert_sort(n);
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
希尔排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void shell_sort(int n) {
shell=n/2;
while(shell) {
for(i=0; i<shell; i++) {
for(j=i+shell; j<n; j+=shell) {
if(a[j]<a[j-shell]) {
temp=a[j];
for(k=j-shell; k>=0&&a[k]>temp; k-=shell) {
a[k+shell]=a[k];
}
a[k+shell]= temp;
}
}
}
shell/=2;
}
}
int main() {
int n,k,shell,temp,j,i;
scanf("%d",&n);
for(i=0; i<n; i++) {
scanf("%d",&a[i]);
}
shell_sort(n);
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
}
冒泡排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void bubbo_sort(int n){
int i,j,temp;
for(i=0;i<n;i++){
int flag=0;
for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag++;
}
}
if(flag==0)
break;
}
}
int main(){
int n,k,j,i,temp;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
bubbo_sort(n);
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
选择排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void select_sort(int n){
for(i=0;i<n;i++){
smallest=i;
for(j=i+1;j<n;j++){
if(a[j]<a[smallest]){
smallest=j;
}
}
temp=a[smallest];
a[smallest]=a[i];
a[i]=temp;
}
}
int main(){
int n,k,j,i,temp,smallest;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
select_sort(n);
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
快速排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void quick_sort(int left,int right) {
if(left>=right) {
return;
}
int i=left,j=right;
int key=a[left];
while(i<j) {
while(i<j&&key<=a[j]) {
j--;
}
a[i]=a[j];
while(i<j&&key>=a[i]) {
i++;
}
a[j]=a[i];
}
a[i]=key;
quick_sort(left,i-1);
quick_sort(i+1,right);
}
int main() {
int n,k,j,i,temp,smallest;
scanf("%d",&n);
for(i=0; i<n; i++) {
scanf("%d",&a[i]);
}
quick_sort(0,n-1);
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
}
堆排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
void heap_sort(int start,int end) {
int dad=start;
int son=dad*2+1;
while(son<=end) {
if(son+1<=end &&a[son]<a[son+1]) {
son++;
}
if(a[dad]>a[son])
return;
else {
int temp=a[dad];
a[dad]=a[son];
a[son]=temp;
dad=son;
son=dad*2+1;
}
}
}
int main() {
int n,k,j,i,temp,smallest;
scanf("%d",&n);
for (i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for (i = n / 2 - 1; i >= 0; i--)
heap_sort(i, n - 1);
for (i = n - 1; i > 0; i--) {
int temp=a[0];
a[0]=a[i];
a[i]=temp;
heap_sort(0, i - 1);
}
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
}
归并排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
int temp[1000];
void merge_sort(int start,int end) {
if(start>=end) {
return;
}
memset(temp,0,sizeof temp);
merge_sort(start,(start+end)/2);
merge_sort((start+end)/2+1,end);
int i=start;
int j=(start+end)/2+1;
int k=start;
while(i<=(start+end)/2&&j<=end) {
if(a[i]<a[j]) {
temp[k++]=a[i];
i++;
} else {
temp[k++]=a[j];
j++;
}
}
while(i<=(start+end)){
temp[k++]=a[i++];
}
while(j<=end){
temp[k++]=a[j++];
}
for(i=start;i<=end;i++){
a[i]=temp[i];
}
}
int main() {
int n,k,j,i,temp,smallest;
scanf("%d",&n);
for (i=0; i<n; i++) {
scanf("%d",&a[i]);
}
merge_sort(0, n - 1);
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
}
基数排序
#include <cstdio>
#include <iostream>
#include <malloc.h>
#include <cstring>
using namespace std;
int a[1000];
int get_max(int n){
int i,max;
max=a[0];
for(i=1;i<n;i++){
if(a[i]>max)
max=a[i];
}
return max;
}
void count_sort(int len,int exp){
int temp[1000];
int i,buckets[10]={0};
for(i=0;i<len;i++){
buckets[(a[i]/exp)%10]++;
}
for(i=1;i<10;i++){
buckets[i]+=buckets[i-1];
}
for(i=len-1;i>=0;i--){
temp[--buckets[(a[i]/exp)%10]]=a[i];
}
for(i=0;i<len;i++){
a[i]=temp[i];
}
}
void radix_sort(int len) {
int exp;
int max=get_max(len);
for(exp=1;max/exp>0;exp *= 10){
count_sort(len,exp);
}
}
int main() {
int n,k,j,i,temp,smallest;
scanf("%d",&n);
for (i=0; i<n; i++) {
scanf("%d",&a[i]);
}
radix_sort(n);
for(i=0; i<n; i++) {
printf("%d ",a[i]);
}
}
查找
折半查找
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a[1000];
int main(){
int n,k,mid;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
while(scanf("%d",&k)){
int flag=-1;
int right=0,left=n-1;
while(right<=left){
mid=(right+left)/2;
if(a[mid]==k){
flag=mid+1;
break;
}
else if(a[mid]<k){
right=mid+1;
}else{
left=mid-1;
}
}
printf("flag=%d\n",flag);
}
return 0;
}
图算法
BFS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
const int N=22;
bool flag=false;
int stepArr[4][2]={0,-1,0,1,1,0,-1,0};
bool visit[N][N];
int mazeArr[N][N];
struct Node{
int x,y,step;
Node(int x1,int y1,int step1):x(x1),y(y1),step(step1){}
};
int BFS(int n){
Node node(0,0,0);
queue<Node> q;
while(!q.empty())q.pop();
q.push(node);
while(!q.empty()){
node=q.front();
q.pop();
if(node.x==n-1&&node.y==n-1){
return node.step;
}
memset(visit,0,sizeof visit);
for(int i=0;i<4;i++){
int x=node.x+stepArr[i][0];
int y=node.y+stepArr[i][1];
if(x>=0&&y>=0&&x<n&&y<n&&visit[x][y]==0&&mazeArr[x][y]==0)
{
Node next(x,y,node.step+1);
visit[x][y]==1;
q.push(next);
}
}
}
return -1;
}
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
memset(mazeArr,0,sizeof mazeArr);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&mazeArr[i][j]);
}
}
printf("%d",BFS(n));
}
return 0;
}
DFS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <cmath>
#include <vector>
using namespace std;
bool flag=false;
bool check(int row,int cod){
if(a[row][cod]==0){
return true;
}
return false;
}
void DFS(int row,int cod){
if(flag==true||row==endrow&&cod==endcod)
{
flag=true;
return;
}
if(check(row+1,cod)&&flag==false){
dfs(row+1,cod)
}if(check(row,cod+1)&&flag==false){
dfs(row,cod+1)
}if(check(row-1,cod)&&flag==false){
dfs(row+1,cod)
}if(check(row,cod-1)&&flag==false){
dfs(row,cod+1)
}
return;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d%d%d",&a[i][j]);
}
}
DFS(0,0);
printf("")
}
return 0;
}
PRIM
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
#define N 1010
#define pi acos(-1.0)
#define inf 100000000
#define eps 1e-9
typedef long long ll;
typedef unsigned long long ull;
struct point{
int x,y,v;
}p[N];
double a[N][N],maxx[N][N],dis[N];
int n,pre[N];
bool vis[N];
void prim(){
double ans=0;
memset(vis,false,sizeof(vis));
memset(maxx,0,sizeof(maxx));
for(int i=1;i<=n;i++){
dis[i]=a[1][i];
pre[i]=1;
}
dis[1]=0;
vis[1]=true;
for(int i=1;i<n;i++){
int k=0;
double tmp=inf;
for(int j=1;j<=n;j++){
if(!vis[j]&&dis[j]<tmp){
k=j;
tmp=dis[j];
}
}
vis[k]=true;
ans+=tmp;
for(int j=1;j<=n;j++){
if(!vis[j]&&dis[j]>a[k][j]){
dis[j]=a[k][j];
pre[j]=k;
}
}
for(int j=1;j<=n;j++){
if(vis[j]&&j!=k){
maxx[j][k]=max(maxx[j][pre[k]],tmp);
maxx[k][j]=maxx[j][k];
}
}
}
double an=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j){
an=max(an,double(p[i].v+p[j].v)/(ans-maxx[i][j]));
}
}
}
printf("%.2f\n",an);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].v);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
a[i][j]=sqrt(1.0*(p[i].x-p[j].x)*(p[i].x-p[j].x)+1.0*(p[i].y-p[j].y)*(p[i].y-p[j].y));
}
}
prim();
}
return 0;
}