冒泡法排序多语言版
呵呵,无聊,准备写点算法玩玩.先来排序的
冒泡法排序(C#)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class hooyes
{
public static void Main(string[] args)
{
int[] a ={ 1, 100, 3, 5, 2, 9, 0, 3, 43, 4, 2 };
Console.WriteLine("未排序前的数组");
printArray(a);
int j = a.Length;
while (j > 0)
{
for (int i = 0; i < j- 1; i++)
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
j--;
//Console.WriteLine("");
//printArray(a);
}
Console.WriteLine("");
Console.WriteLine("排序后的数组");
printArray(a);
Console.ReadLine();
}
public static void printArray(int[] Array)
{
for (int i = 0; i < Array.Length; i++)
{
Console.Write(Array[i].ToString()+" | ");
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class hooyes
{
public static void Main(string[] args)
{
int[] a ={ 1, 100, 3, 5, 2, 9, 0, 3, 43, 4, 2 };
Console.WriteLine("未排序前的数组");
printArray(a);
int j = a.Length;
while (j > 0)
{
for (int i = 0; i < j- 1; i++)
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
j--;
//Console.WriteLine("");
//printArray(a);
}
Console.WriteLine("");
Console.WriteLine("排序后的数组");
printArray(a);
Console.ReadLine();
}
public static void printArray(int[] Array)
{
for (int i = 0; i < Array.Length; i++)
{
Console.Write(Array[i].ToString()+" | ");
}
}
}
}
冒泡法排序(Java)
package com.hooyes.first;
public class What {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a ={ 1, 100, 3, 5, 2, 9, 0, 3, 43, 4, 2 };
System.out.println("排序前");
printArray(a);
int j=a.length;
while (j > 0)
{
for (int i = 0; i < j- 1; i++)
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
j--;
}
System.out.println("");
System.out.println("排序后");
printArray(a);
}
public static void printArray(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" | ");
}
}
}
public class What {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a ={ 1, 100, 3, 5, 2, 9, 0, 3, 43, 4, 2 };
System.out.println("排序前");
printArray(a);
int j=a.length;
while (j > 0)
{
for (int i = 0; i < j- 1; i++)
{
if (a[i] > a[i + 1])
{
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
j--;
}
System.out.println("");
System.out.println("排序后");
printArray(a);
}
public static void printArray(int[] a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" | ");
}
}
}
相关文章地址:https://hooyes.net/p/bubble-sort