冒泡算法编码
来源:互联网 发布:单片机怎么连接电脑 编辑:程序博客网 时间:2024/06/06 02:00
其基本思想是,将待排序的数据依次进行相邻的比较,如不符合顺序的要求,两个数立即交换。
经过一趟比较后,最大的元素就被排到了最后,依次进行,就完成了整个的排序的过程。
循环分为内外二层,每次内层循环取出一个最大或者最小的值。
import java.io.*;
public class BubbleSort {/**
* @param args
*/
public static void main(String[] args)
{
int a[]=new int[20];
File fin=new File("423_in2.dat");
File fout=new File("423_out2.dat");
String score="";
String s[]=new String[20];
int length=(int)fin.length();
char[] b=new char[length];
try
int ch
FileInputStream filein=new FileInputStream(fin);
FileWriter fileOut=new FileWriter(fout);
int k=0;
while((ch = filein.read())!=-1 ){
b[k]=(char)ch;
k++;
}
score=new String(b);
s=score.split("\\D");
k=0;
for(int i=0;i<20;i++)
{
a[i]=Integer.parseInt(s[k]);
k++;
}
fileOut.write("排序前的数组是:");
for(int i=0;i<20;i++){
fileOut.write(s[i]+" ");
}
fileOut.write("\n"+"排序后的数组是:");
for(int i=0;i<20;i++)
{
for(int j=0,temp=0;j<20-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}}
for(int i=0;i<20;i++)
{
fileOut.write(((Integer)a[i]).toString()+" ");
}
fileOut.flush();
fileOut.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}}
0 0
- 冒泡算法编码
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法!!
- 冒泡算法
- 算法-冒泡
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 冒泡算法
- 从贝叶斯方法谈到贝叶斯网络
- org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type sette
- 关于Revit 二次开发问题请咨询Autodesk ADN
- 10个不错的编程等宽字体
- mysql 时间函数大全
- 冒泡算法编码
- 使用JNI_OnLoad()函数实现本地方法的注册
- 内核对象(VC_Win32)
- SQL 语句大全
- 数据的标准化
- SQL SERVER 正则匹配实例分享--【叶子】
- Android:处理调用系统相机照片被压缩问题
- PC值=当前程序执行位置+8
- vs2010无法连接到asp.net developer server