数组(随机生成,三种排序,二分查找)
【摘要】
namespace keshanglianxi6.28_2{ class Program { static void Main(string[] args) { Random ra= new Random(); int[] arr= new int[10]; for (int i= 0; i < 10; i++) { arr[i] = ra.Next(100,999...
namespace keshanglianxi6.28_2
{
class Program
{
static void Main(string[] args)
{
Random ra= new Random();
int[] arr= new int[10];
for (int i= 0; i < 10; i++)
{
arr[i] = ra.Next(100,999);
}
for (int i= 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "");
}
Console.WriteLine();
sort1(arr);
for (int i= 0; i < arr.Length; i++)
{
Console.Write(arr[i] + "");
}
//int[]arr = { 1, 22, 45, 65, 75, 84, 95, 100, 124 };
Console.WriteLine();
int a =f(arr,222);
if (a ==-1)
{
Console.Write("没有您要查找的数:");
}
else
{
Console.Write("您要查找的数是在第"+(a+1)+"位");
}
Console.ReadLine();
}
//查找,,,
static int f(int[] a, int n)
{
int low,mid, high;
low =0;
high =a.Length - 1;
while (low<= high)
{
mid = (low + high) / 2;
int midVal = a[mid];
if (midVal < n)
{
low = mid + 1;
}
else if (midVal > n)
{
high = mid - 1;
}
else
{
return mid;
}
}
return-1;
}
//排序,,,
static void sort(int []a)
{
int i,j,temp;
for (i =0; i < a.Length; i++) {
for (j = 0; j < a.Length -i-1; j++) {
if (a[j] > a[j + 1])
{
temp =a[j];
a[j] = a[j+ 1];
a[j + 1] =temp;
}
}
}
}
static void sort1(int []a)
{
int i, j,temp;
for (i =1; i < a.Length; i++) {
j = i - 1;
temp = a[i];
while (j >= 0 &&temp < a[j])
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
static void sort2(int[]arr)
{
for (int x= 0; x < arr.Length - 1; x++)
{
for (int y = x + 1; y <arr.Length; y++)
{
if (arr[x] > arr[y])
{
int temp =arr[x];
arr[x] =arr[y];
arr[y] =temp;
}
}
}
}
}
}
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/76091898
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者


评论(0)