十行代码搞定 "冒泡排序"
2004-10-24 17:25
411 查看
Java Code:
class Class1
{
static void bubbleSort(int[] a)
{
for (int j = a.length - 1; j > 0; j--) // outer loop (backward)
for (int i = 0; i < j; i++) // inner loop (forward)
if (a[i] > a[i+1]) //Swap
{
a[i] = a[i] + a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] = a[i] - a[i+1];
}
}
public static void main(String[] args)
{
int[] a = new int[]{-1,-2,9,4,5,3,7,1,2,3,1,2,3,-3,2,-19};
//int[] a = new int[]{9,8,7,6,5,4,3,2,1,0,11};
bubbleSort(a);
for (int i=0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
}
C# Code:
class Class1
{
static void BubbleSort(int[] a)
{
for (int j = a.Length - 1; j > 0; j--) // outer loop (backward)
for (int i = 0; i < j; i++) // inner loop (forward)
if (a[i] > a[i+1])
{
//a[i] = a[i] + a[i+1];
//a[i+1] = a[i] - a[i+1];
//a[i] = a[i] - a[i+1];
Swap(ref a[i],ref a[i+1]);
}
}
static void Swap(ref int x,ref int y)
{
x = x + y;
y = x - y;
x = x - y;
}
static void Main(string[] args)
{
int[] a = new int[]{-1,-2,9,4,5,3,7,1,2,3,1,2,3,-3,2,-19};
//int[] a = new int[]{9,8,7,6,5,4,3,2,1,0,11};
BubbleSort(a);
for (int i=0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
System.Console.ReadLine();
}
}
class Class1
{
static void bubbleSort(int[] a)
{
for (int j = a.length - 1; j > 0; j--) // outer loop (backward)
for (int i = 0; i < j; i++) // inner loop (forward)
if (a[i] > a[i+1]) //Swap
{
a[i] = a[i] + a[i+1];
a[i+1] = a[i] - a[i+1];
a[i] = a[i] - a[i+1];
}
}
public static void main(String[] args)
{
int[] a = new int[]{-1,-2,9,4,5,3,7,1,2,3,1,2,3,-3,2,-19};
//int[] a = new int[]{9,8,7,6,5,4,3,2,1,0,11};
bubbleSort(a);
for (int i=0; i < a.length; i++)
{
System.out.println(a[i]);
}
}
}
C# Code:
class Class1
{
static void BubbleSort(int[] a)
{
for (int j = a.Length - 1; j > 0; j--) // outer loop (backward)
for (int i = 0; i < j; i++) // inner loop (forward)
if (a[i] > a[i+1])
{
//a[i] = a[i] + a[i+1];
//a[i+1] = a[i] - a[i+1];
//a[i] = a[i] - a[i+1];
Swap(ref a[i],ref a[i+1]);
}
}
static void Swap(ref int x,ref int y)
{
x = x + y;
y = x - y;
x = x - y;
}
static void Main(string[] args)
{
int[] a = new int[]{-1,-2,9,4,5,3,7,1,2,3,1,2,3,-3,2,-19};
//int[] a = new int[]{9,8,7,6,5,4,3,2,1,0,11};
BubbleSort(a);
for (int i=0; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
System.Console.ReadLine();
}
}
相关文章推荐
- 64位Win8添加Oracle ODBC驱动时提示"无法加载ORACLE In OraDb11g_Home1 ODBC驱动程序的安装例程,因为存在系统错误代码193"?
- VS2008无法执行添加/移除操作,因为代码元素"***"是只读的
- 编写T4模板进行代码生成无法避免的两个话题:"Assembly Locking"&"Debug"
- 哥发明的 一个ssh整合时解决dao层代码冗余问题的"一点儿不漏"型抽取法!!!!
- ubuntu 12.04(gcc降级)编译android代码遇到"_FORTIFY_SOURCE"的解决方法
- 定制Eclipse的Content assist(代码补全),取消 空格键 "=" 自动补全(上屏)
- 用Delphi7编写代码时出现"dcc70.dll"调用失败的处理方法
- 代码中“抛异常”与“记录log"的思考
- 控制台执行JAVA代码发生的错误(Exception in thread "main" java.lang.NoClassDefFoundError)
- 嵌入式裸机开发学习:MMU代码中的:"r" (ttb)
- 一段跨浏览器的"复制到剪贴板"javascript代码
- error PRJ0019: 某个工具从以下位置返回了错误代码: "Assembling...",
- VS2008 打开"视图设计器" 和 "查看代码"提示没有可用的编辑器问题
- 在Java代码中避免 "!=null " 判断
- Win8 下重装 Chrome 浏览器遇到"糟糕!安装失败。错误代码:0xa0430721。"错误的解决
- 解决VS生成时的错误: "LC.EXE 已退出,代码为-1"
- "lc.exe"已退出 代码为-1 的解决方法
- android Task Activity launchMode="singleTask" singleInstance 代码分析
- #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif
- 网页中实现"分享至微博、QQ空间等"代码