算法
冒泡排序 bubble sorting
int t;
int[] a ={21,56,64,94,97,123};
for(int j =a.Length-1;j>0;j--)
{ for(int i =0;i<j;i++)
{ if(a[i]>a[i+1])
{ t =a[i];
a[i]=a[i+1];
a[i+1]=t;
} } }
for(int u =0;u<a.Length;u++)
Console.WriteLine(a[u]); 结果:21,56,64,94,97,123
同时找最大最小
int temp;
int[] a={56,66,5,1230,87,95};
for(int i =0;i<(a.Length+1)/2;i++)
{ if(a[i]>a[a.Length-1-i])
{ temp =a[i];
a[i] =a[a.Length-1-i];
a[a.Length-1-i] =temp;
}
}
int max =a[a.Length-1],min =a[0];
for(int i=0;i<(a.Length+1)/2;i++)
{ if(min>a[i])
min =a[i];
}
for(int i =(a.Length+1)/2;i<a.Length;i++)
{ if(max<a[i])
max =a[i];
}
Console.WriteLine("{0},{1}",min,max); 结果:5,1230
基数排序
int[] a={1,5,9,7};
int[] b=new int[10];
for(int i=0;i<a.Length;i++)
b[a[i]]=1;
for(int j=0;j<b.Length;j++)
if(b[j]==1)
Console.WriteLine(j); 结果:1,5,7,9
插入排序
int[] r={12,2,6,65,42};
for(int i=1;i<r.Length;i++)
{ int t;
评论0