首 页文章中心下载中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:开源盛世-源代码下载网 -> 文章中心 -> 网站开发类 -> NET 技术 -> .NET基础 -> 文章内容 退出登录 用户管理
栏目导航
· .NET基础 · Asp.Net
· 综合应用 · 数据库应用
· XML 应用 · 控件开发和使用
热门文章
· Tab Control控件使用...
· 学生档案管理系统
· [图文] 排列组合公式
· UTF-8与GB2312之间的...
· DirectShow下载安装...
· Virtual PC 在PAE模...
· Windows2000终端服务...
· MapInfo上的GIS系统...
· Mapbasic参考手册索...
· MapX应用开发中文讲...
相关文章
数组的操作处理与数组元素的冒泡排序
作者:佚名  来源:vscodes.com整理  发布时间:2005-12-16 13:06:13  发布人:Polaris

减小字体 增大字体


//(1)查找数组元素

static void Main(string[] args)
{
   // TODO: 查找数组元素
   int[] a= new int[100];
   Console.WriteLine("输入数字");
   string s=Console.ReadLine();

   int x=Int32.Parse(s);
   Console.WriteLine("\n 输入int数组元素 \n");

   for(int i=0;i<x;i++)
  {
     string s1=Console.ReadLine();
     a[i]=Int32.Parse(s1);
  }

  Console.WriteLine("搜查元素\n");
  string s3=Console.ReadLine();
  int x2=Int32.Parse(s3);
  //循环部分数组
  for(int i=0;i<x;i++)
  {
    if(a[i]==x2)
    {
 Console.WriteLine("Search successful");
 Console.WriteLine("Element {0} found at location {1}\n",x2,i+1);
        Console.ReadLine ();
 return;
    }
  }
}
---------------------------------------
//(2)查找数组元素中最大和最小的元素


static void Main(string[] args)
{
   // TODO: 找出最大和最小的元素
   int n;
   float large,small;
   int[] a = new int[50];
   Console.WriteLine("输入int数组大小");
   string s= Console.ReadLine();
   n=Int32.Parse(s);
   Console.WriteLine("输入数组元素");
   for(int i=0;i<n;i++)
   {
 string s1=Console.ReadLine();
 a[i]=Int32.Parse(s1);
   }
   large =a[0];
   small= a[0];
   for(int i=1;i<n;i++)
   {
 if(a[i]>large)
 large=a[i]; //更新large变量
 else if(a[i]<small)
 small=a[i]; //更新small变量
    }
   Console.WriteLine("Largest element in the array is {0}",large);
   Console.WriteLine("Smallest element in the array is {0}",small);
   Console.ReadLine ();//暂停
}


----------------------------------------------
//数组元素的冒泡排序

/*
第一遍使最轻的记录上升到数组的最顶端,
第二遍使剩下的最小的上升到第二位置,
第二遍扫描时不必再比较最顶端的记录
*/

static void Main(string[] args)
{
   int[] a= new int[100];
   Console.WriteLine("输入int数组里的元素数目");
   string s=Console.ReadLine();
   int x=Int32.Parse(s);

   Console.WriteLine("输入元素");
   for(int j=0;j<x;j++)
   {
 string s1=Console.ReadLine();
 a[j]=Int32.Parse(s1);
   }

   int limit= x-1;
   for(int pass=0;pass<x-1;pass++)
   {
 for(int j=0;j<limit-pass;j++)
 {
   if(a[j]>a[j+1])
   {
      int k=a[j]; //数组元素交换
      a[j]=a[j+1]; //数组元素交换
      a[j+1]=k; //数组元素交换
   }
 }
   }

  Console.WriteLine("Sorted elements of an array are(冒泡排序)");

  for (int j=0;j<x;j++)
  {
   Console.WriteLine(a[j]);
  }
   Console.ReadLine ();
}

---------------------------------------


End of《数组的操作处理与数组元素的冒泡排序》

[] [返回上一页] [打 印] [收 藏]
 
∷相关“数组的操作处理与数组元素的冒泡排序”文章评论∷
(评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 网站目录 鄂ICP备06007162
开源盛世 版权所有Copyright © 2003-2005 VSCodes.Com. All Rights Reserved.