JAVA冒泡排序算法的几种实现方法
本文出自 “唐大老师” 博客,请务必保留此出处http://tscjsj.blog.51cto.com/412451/84561
public class Bubble {
// 冒泡排序函数1
public static void bubbleSort1(Comparable []data){
int position,scan;
Comparable temp;
for(position = data.length-1;position>=0;position--){
for(scan=0;scan<=position-1;scan++){
if(data[scan].compareTo(data[scan+1])<0){
temp = data[scan];
data[scan] = data[scan+1];
data[scan+1]=temp;
}
}
}
}
// 冒泡排序函数2
public static int[] bubbleSort2(int[] m){
int intLenth = m.length;
/*执行intLenth次*/
for (int i=0;i<intLenth;i++){
/*每执行一次,将最小的数排在后面*/
for (int j=0; j<intLenth-i-1;j++)
{
int a = m[j];
int b = m[j + 1];
if (a < b)
{
m[j] = b;
m[j + 1] = a;
}
}
}
return m;
}
public static void main(String []args){
// 冒泡排序1
Comparable []c={4,9,23,1,45,27,5,2};
bubbleSort1(c);
for(int i=0;i<c.length;i++)
System.out.println("冒泡排序1:"+c[i]);
System.out.println("*******************");
// 冒泡排序2
int []b = {4,9,23,1,45,27,5,2};
int []e = bubbleSort2(b);
for(int j=0;j<e.length;j++)
System.out.println("冒泡排序2:"+e[j]);
}
}
相关文档:
先来了解一下链表模式的原理:
首先写一个JavaBean,内容是要添加的元素和该元素的节点。
public class NodeBean implements Serializable
{
private Object data; //元素本身
private NodeBean next; //下一个节点
&n ......
今天工作的任务要写一些跟开源协议相关的约定说明,所以在网上搜索了一些资料以供参考,下面列出了几个比较常见的开源协议,如果想要了解其他的协议
和详细了解这些协议,我个人推荐这个网址:http://www.opensource.org/licenses/
Mozilla Public License
MPL License,允许免费重发布、免费修改,但要求修改后 ......
AWT是Java中支持图形化用户界面GUI设计的一个工具集。AWT的API是独立于平台的,但设计出来的界面在各种平台的风格不同,利用API中各种类在特定平台下的对等组件peers提供具体平台下的实现。
欲建立一个GUI首先确定所用的组件及其布局,然后实现其事件的响应。组件的类型有多种,如常用的Button、CheckBox等,均为Component ......
public static String byteToString(byte src)
{
String desc = null;
int i = 0; //取1个字节
i = src&0xFF;
desc = Integer.toHexString(i);
if (desc.length() == 1)
......