数组的操作处理与数组元素的冒泡排序
2003-02-24 09:07
337 查看
//(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 ();
}
---------------------------------------
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 ();
}
---------------------------------------
相关文章推荐
- 第十四周实验--任务1--建立专门的数组类处理有关数组的操作
- (34)数组的相关处理函数(上)(35)常用的数组相关操作函数(下)
- 建立专门的数组类处理有关数组的操作+扩展1
- jQuery事件处理,动画,类数组操作
- 编写程序计算数组元素之和。要求编写函数三次,每次以不同的方法处理数组边界。
- 第十五周项目一 —— 建立专门的数组类处理有关数组的操作
- jq 数组操作、事件处理和相关处理函数的使用
- (13)数组操作:遍历、输出最大值、冒泡排序、选择排序,java已有的排序方法、折半查找
- Python和JavaScript中对超过数组长度的操作处理的不同
- Javascript的对象操作 - 数组元素操作 【javascript、数组、插入、删除、替换】 ( 咋个办呢 -zgbn)
- java数组操作 数组去除重复 移除数组元素
- C++第十四周【任务1】 建立专门的数组类处理有关数组的操作
- 编写程序计算数组元素之和。要求编写函数三次,每次以不同的方法处理数组边界。
- 第十五周上机任务项目1-2建立专门的数组类处理有关数组的操作
- js 数组赋值操作,修改数组元素的值
- 数组的常见操作:冒泡排序 选择排序与取最值
- 数组的常见操作:冒泡排序 选择排序与取最值
- 尚硅谷第四课0722班 java-特殊流程控制 -数组元素的默认初始化-数组操作常见问题-Java内存的结构
- JAVA操作数组----二分查找法,冒泡排序