笔试编程2.编一个冒泡排序程序
2012-05-18 13:37
169 查看
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
string[] strTmp = {
"5", "6", "8",
"9", "2",
"3", "4" };
int len = strTmp.Length;
int[] a = new
int[len];
Console.Write("原-数ºy:
");
for (int i = 0; i < len; i++)
{
x = Convert.ToInt32(strTmp[i]);
a[i] = x;
Console.Write(a[i] +
" ");
}
Console.Write("\n");
int b = 0;
//冒泡排序的核心代码
for (int i = (len - 1); i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
b = a[j + 1];
a[j + 1] = a[j];
a[j] = b;
}
}
}
Console.Write("排序后的数: ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] +
" ");
}
Console.Write("\n");
Console.ReadKey(true);
}
}
}
JAVA:
public
class Sort {
public
static
void maopaosort(int
array[])
{
int
mark=0;
for(int
i=0;i<array.length;i++)
{
System.out.print("第"+(i+1)+"次排序:");
for(int j=0;j<array.length;j++)
{
if(array[i]<=array[j])
{
mark=array[i];
array[i]=array[j];
array[j]=mark;
}
System.out.print(array[j]);
}
System.out.println();
}
}
public
static
void main(String [] args)
{
int array[]={4,7,2,6,8,1,5,9,3};
maopaosort(array);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x = 0;
string[] strTmp = {
"5", "6", "8",
"9", "2",
"3", "4" };
int len = strTmp.Length;
int[] a = new
int[len];
Console.Write("原-数ºy:
");
for (int i = 0; i < len; i++)
{
x = Convert.ToInt32(strTmp[i]);
a[i] = x;
Console.Write(a[i] +
" ");
}
Console.Write("\n");
int b = 0;
//冒泡排序的核心代码
for (int i = (len - 1); i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (a[j] > a[j + 1])
{
b = a[j + 1];
a[j + 1] = a[j];
a[j] = b;
}
}
}
Console.Write("排序后的数: ");
for (int i = 0; i < a.Length; i++)
{
Console.Write(a[i] +
" ");
}
Console.Write("\n");
Console.ReadKey(true);
}
}
}
JAVA:
public
class Sort {
public
static
void maopaosort(int
array[])
{
int
mark=0;
for(int
i=0;i<array.length;i++)
{
System.out.print("第"+(i+1)+"次排序:");
for(int j=0;j<array.length;j++)
{
if(array[i]<=array[j])
{
mark=array[i];
array[i]=array[j];
array[j]=mark;
}
System.out.print(array[j]);
}
System.out.println();
}
}
public
static
void main(String [] args)
{
int array[]={4,7,2,6,8,1,5,9,3};
maopaosort(array);
}
}
相关文章推荐
- 10个方法提高编程生产力--一个优秀程序
- 【Java编程】编写一个计算N个整数平均值的程序
- 【C语言编程】编写一个程序解决选择问题,令k=n/2
- java笔试题---程序产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
- 使用面向对象的编程思想写一个简单的控制台计算器程序,放松一下
- Linux Socket 编程实例(一个Echo程序)
- 去年这时候辞退了一个开发人员,不知道他现在是否还在写程序了,可以对比一下编程水平
- OpenGL编程学习之《一个简单的OpenGL程序》
- 去年这时候辞退了一个开发人员,不知道他现在是否还在写程序了,可以对比一下编程水平
- 编程新手:看懂很多示例,却依然写不好一个程序
- Linux Socket编程实例(一个Hello World程序) 选择自 shaohui 的 Blog
- 简单编程(三)编写一个程序,要求用户输入两个整数,然后计算并输出他们的和、差、积、商。
- Linux Socket编程实例(一个Hello World程序)
- 用Java Swing作一个日历控制程序-Java基础-Java-编程开发
- 一个简单的SWT程序实例及详解-Java基础-Java-编程开发
- ROS Learning-015 learning_tf(编程) 编写一个监听器程序 (Python版)
- socket编程的一个简单的hello world程序
- 一个有意思的笔试题:如何破解一个简单密码登陆程序
- 每天一个java小程序之冒泡排序
- Android编程实现在一个程序中启动另一个程序的方法