标签:string temp ++ main number ber 算法 sort ati
冒泡排序:
public class BubbleSort {
public static void main(String [] args)
{
int [] array={1,2,3,5,6,4};
sort(array);
}
public static void sort(int [] array)
{
for(int i=1;i<array.length;i++)
{
for(int j=0;j<array.length-1;j++)
{
if(array[j]>array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
show(array);
}
public static void show(int [] array)
{
for(int i:array)
{
System.out.println(i);
}
}
}
直接选择法:
public class SelectSort {
public static void main(String [] args)
{
int [] array = {6,5,4,3,2,1};
sort(array);
}
public static void sort(int [] array)
{
int number;
for(int i =1;i<array.length;i++)
{
number=0;
for(int j=1;j<=array.length-i;j++)
{
if(array[j]>array[number])
{
number=j;
}
}
int temp = array[array.length-i];
array[array.length-i]=array[number];
array[number] = temp;
}
show(array);
}
public static void show(int [] array)
{
for(int i:array)
{
System.out.println(i);
}
}
}
反转排序:
public class ReserveSort {
public static void mian(String [] args)
{
int [] array = {1,2,3,4,5,6};
sort(array);
}
public static void sort(int [] array)
{
int temp;
int len=array.length;
for(int i=0;i<len/2;i++)
{
temp=array[i];
array[i] = array[len-1-i];
array[len-1-i]=temp;
}
show(array);
}
public static void show(int [] array)
{
for(int i:array)
{
System.out.println(i);
}
}
}
标签:string temp ++ main number ber 算法 sort ati
原文地址:http://www.cnblogs.com/HMY666/p/7637749.html