一、冒泡排序代码实现:

public class TastDome {
public void maopao(){
int[] arr={2,1,4,3};
for(int i=0;i
for(int j=0;j
if (arr[j]
int temp;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i
System.out.println(i);
}
}
public static void main(String[] arry){
TastDome td=new TastDome();
td.maopao();
}

二、选择排序代码实现:

public class TastDome1 {
public void xze(){
int[] arr=new int[]{7,3,6,2,1};
for(int i=0;i
int k=i;
for(int j=i+1;j
if(arr[j]
k=j;
}
}
if (i !=k){
int temp =arr[i];
arr[i]=arr[k];
arr[k]=temp;
}
}
for (int num:arr){
System.out.println(num);
}
}
public static void main(String[] ags){
TastDome1 tastDome1=new TastDome1();
tastDome1.xze();
}
}

非常简单就不一句一句的解释了

三、快速排序

思想:通过一趟排序将待排序的列表分为两个部分,其中记录的一部分关键字均要比另一部分的关键字要小,则分别对这两部分继续进行排序,直到整个序列有序。

代码实现:

public int getMiddle(Integer[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] > tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] < tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}

递归形式的分治排序算法:

public void _quickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
public void quick(Integer[] str) {
if (str.length > 0) { //查看数组是否为空
_quickSort(str, 0, str.length - 1);
}
}

编写测试方法:

public class TestMain {
public static void main(String[] args) {
Integer[] list={34,3,53,2,23,7,14,10};
QuicSort qs=new QuicSort();
qs.quick(list);
for(int i=0;i
System.out.print(list[i]+" ");
}
}
}