Java中的冒泡排序与选择排序
冒泡排序
第一个跟第二个比较,大了就交换值,第二又跟第三个比较,依次进行下去,进行数组长度减一次。
排序前:[6, 5, 4, 3, 2, 1]
[5, 4, 3, 2, 1, 6]//第一轮
[4, 3, 2, 1, 5, 6]//第二轮
[3, 2, 1, 4, 5, 6]//第三轮
[2, 1, 3, 4, 5, 6]//第四轮
[1, 2, 3, 4, 5, 6]//第五轮
排序后:[1, 2, 3, 4, 5, 6]
代码如下:
import java.util.Arrays;
public class mppx {
public static void main(String[] args) {
int[] a = { 6, 5, 4, 3, 2, 1 };//也可以使用随机数random
int b;
System.out.println("排序前:" + Arrays.toString(a));
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {//减i只是为了美化代码,目的是让已经比较过的数不在参加比较
if (a[j] > a[j + 1]) {
b = a[j];//交换值
a[j] = a[j + 1];
a[j + 1] = b;
}
}
System.out.println(Arrays.toString(a));
}
System.out.println("排序后:" + Arrays.toString(a));
}
}
选择排序
第一个跟后面所有的数比较,大了就把后面中最小的放在第一个,再跟后面剩下的所有比较,依次进行下去,进行数组长度减一次。
排序前:[6, 5, 4, 3, 2, 1]
[1, 6, 5, 4, 3, 2]
[1, 2, 6, 5, 4, 3]
[1, 2, 3, 6, 5, 4]
[1, 2, 3, 4, 6, 5]
[1, 2, 3, 4, 5, 6]
排序后:[1, 2, 3, 4, 5, 6]
代码如下
import java.util.Arrays;
public class xzpx {
public static void main(String[] args) {
System.out.println("选择排序================================");
int a[] = {22, 45, 85, 1, 2, 0, 32};
int N = a.length;
System.out.println(Arrays.toString(a));
for (int i = 0; i < N - 1; i++) {
int min = i;
for (int j = i + 1; j < N; j++) {
//将a[i]和a[i+1...N-1]中的最小元素交换
if (a[j] < a[min]) {//升序排列
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
System.out.println("第"+(i+1)+"次排序:"+Arrays.toString(a));
}
}
System.out.println(Arrays.toString(a));
}
}
插入排序
将一个记录值在已排序的数组找到合适的位置进行插入。
import java.util.Arrays;
public class crpx {
public static void main(String[] args) {
int[] array = {55,22,45,12,2,6,8,96,74} ;
int i,j,temp;
System.out.println("原数组:"+Arrays.toString(array));
for(i=1;i<array.length;i++) {
temp=array[i];
for(j=i-1;j>=0;j--) {
if(temp>array[j]) {
break;
}else {
array[j+1]=array[j];
}
}
array[j+1]=temp;
System.out.println("第"+i+"次排序:"+Arrays.toString(array));
}
System.out.println("最终结果:"+Arrays.toString(array));
}
}
快速排序
import java.util.Arrays;
public class kspx {
public static void main(String[] args) {
System.out.println("快速排序================================");
int arr[] = {22, 45, 85, 1, 2, 0, 32};
System.out.println("排序前================");
System.out.println(Arrays.toString(arr));
sort(arr, 0, 6);
System.out.println("排序后================");
System.out.println(Arrays.toString(arr));
}
public static void sort(int arr[], int low, int high) {
int i, j, temp, t;
if (low > high) {
return;
}
i = low;
j = high;
//temp就是基准位
temp = arr[low];
while (i < j) {
//先看右边,依次往左递减
while (temp <= arr[j] && i < j) {
j--;
}
//再看左边,依次往右递增
while (temp >= arr[i] && i < j) {
i++;
}
//如果满足条件则交换
if (i < j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
//最后将基准为与i和j相等位置的数字交换
arr[low] = arr[i];
arr[i] = temp;
//递归调用左半数组
sort(arr, low, j - 1);
//递归调用右半数组
sort(arr, j + 1, high);
}
}
希尔排序
import java.util.Arrays;
public class xrpx {
public static void main(String[] args) {
System.out.println("希尔排序================================");
int arr[] = {22, 45, 85, 1, 2, 0, 32};
System.out.println("排序前================");
System.out.println(Arrays.toString(arr));
sort(arr, 0, 6);
System.out.println("排序后================");
System.out.println(Arrays.toString(arr));
}
public static void sort(int data[], int low, int high) {
int j = 0;
int temp = 0;
for (int l = data.length / 2; l > 0; l/= 2) {
for (int i = l; i < data.length; i++) {
temp = data[i];
for (j = i - l; j >= 0; j -= l) {
if (temp < data[j]) {
data[j + l] = data[j];
} else {
break;
}
}
data[j + l] = temp;
}
for (int i = 0; i < data.length; i++)
System.out.print(data[i] + " ");
System.out.println();
}
}
}
本文详细解析了Java中的冒泡排序和选择排序算法,通过实例展示了它们的工作原理和代码实现,对比两者的优缺点。

994

被折叠的 条评论
为什么被折叠?



