冒泡排序:
public class BubbleSort
{
public int[] bubble(int str[])
{
int temp;
int size = str.length;
for(int i=size-1; i>=1; i--)
{
for(int j=i; j<i; j++)
{
if(str[j]>str[j+1])
{
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
return str;
}
public static void main(String[] args)
{
int array[] = {12,343,5,3432,0,44,54,75};
BubbleSort bubbleSort = new BubbleSort();
int[] result = bubbleSort.bubble(array);
System.out.println("排序后:");
for(int i=0; i<result.length; i++)
{
System.out.print(result[i]+",");
}
}
}
快速排序:
public class QuickSort {
public static void quickSort(int obj[],int low,int high)
{
int i=low;
int j=high;
int keyValue=obj[i];
while(i<j)
{
int temp=0;
while(i<j&&obj[j]>=keyValue)
{
j=j-1;
}
obj[i]=obj[j];
while(i<j&&obj[i]<=keyValue)
{
i=i+1;
}
obj[j]=obj[i];
}
obj[i]=keyValue;
if(low<i-1)
{
quickSort(obj,low,i-1);
}
if(high>i+1)
{
quickSort(obj,i+1,high);
}
}
public static void main(String[] args)
{
int[] arr = {23,45,42,16,43,83,3,72,47,4,9,23,1,45,27,5,2};
quickSort(arr, 0, arr.length-1);
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+",");
}
}
}