你相信什么,就会成为什么。
——CL
1.冒泡排序
冒泡排序(Bubble Sort)是一种简单的排序算法。
它通过反复比较相邻的元素并交换它们的位置,将最大的元素逐步“冒泡”到数组的末尾。具体步骤如下:
1. 比较相邻的元素,如果第一个比第二个大,就交换它们两个。
2. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数。
3. 针对所有的元素重复以上的步骤,除了最后一个已经排好序的元素。
4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对元素需要比较。
用java代码实现
import java.util.Arrays;
import java.util.Random;
public class Bubbling {
public static void main(String[] args) throws InterruptedException {
System.out.println(bubble(100000));
}
static long bubble(int n) throws InterruptedException {
long starttime = System.currentTimeMillis();
if (n < 2) { //必须大于两个数
return 0;
}
//创建一个指定大小的空数组
int[] borad = new int[n];
//创建随机数,填满数数组
Random random = new Random();
for (int i = 0; i < n; i++) {
borad[i] = random.nextInt(n*2) + 1; //1-n*2
}
//System.out.println("原始的数据:" + Arrays.toString(borad));
//排序了
for (int i = 0; i < borad.length - 1; i++) {
for (int j = 0; j < borad.length - 1 - i; j++) {
//排
if (borad[j] > borad[j + 1]) { //从小到大
int a = borad[j];
borad[j] = borad[j + 1];
borad[j + 1] = a;
}
}
}
long endtime = System.currentTimeMillis();
return endtime-starttime;
}
}
2.选择排序
选择排序(Selection Sort)也是一种简单的排序算法。
其基本思想是:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
java代码实现:
import java.util.Random;
public class Select {
//选择排序算法代码
static long Optorder(int n) throws InterruptedException { //n为排序多少个元素
//开始时间
long starttime = System.currentTimeMillis();
if (n < 2) { //必须大于两个数
return 0;
}
//创建一个指定大小的数组
int[] borad = new int[n];
//创建随机数,填满数数组
Random random = new Random();
for (int i = 0; i < n; i++) {
borad[i] = random.nextInt(n * 2) + 1; //1-n*2
}
//System.out.println("原始数据:" + Arrays.toString(borad));
//找到最小滴,把最小的移到最左边
for (int i = 0; i < borad.length - 1; i++) {
//最小数索引,开始先假设第一个为最小数
int minindex = i;
for (int j = i + 1; j < borad.length; j++) {
if (borad[j] < borad[minindex]) { //如果第二个数小于第一个数,就得到最小数索引
minindex = j;
}
}
//找到最小数,然后换位置
int temp = borad[i];
borad[i] = borad[minindex];
borad[minindex] = temp;
}
//结束时间
long endtime = System.currentTimeMillis();
return endtime-starttime;
}
}
3.插入排序
插入排序(Insertion Sort)的基本思想是先把一组数据分为有序和无序,在无序的元素中拿一个出来插入到有序中(拿出来的元素与有序元素依次进行对比,比前面数大后面数小就插入到这两个数中间),直到无序元素全部插入。
java代码实现:
import java.util.Arrays;
import java.util.Random;
public class Inset {
//插入排序算法代码
static long insert(int n) throws InterruptedException {
//开始时间
long starttime = System.currentTimeMillis();
if (n < 2) { //必须大于两个数
return 0;
}
//创建一个指定大小的空数组,用于存放随机数
int[] borad = new int[n];
//创建随机数,填满数数组
Random random = new Random();
for (int i = 0; i < n; i++) {
borad[i] = random.nextInt(n * 2) + 1; //1-50
}
//打印原始数组
//System.out.println("未排序数据:" + Arrays.toString(borad));
// 1 3 4 2 5
//插入排序,默认第一个数为有序
for (int i = 1; i < borad.length; i++) {
//定义一个元素需要向左移动的步数step
int step = 0;
for (int j = 0; j < i; j++) {
if (borad[i] < borad[i - j - 1]) { //如果右边小于左边,则这个数需要移位
step++;
}
}
//移位,定义一个老的数组,绘制图的时候用
int[] oldborld = Arrays.copyOf(borad, borad.length);
int temp = borad[i]; //拿到要移动的数,把它赋值给其他的元素
for (int j = 0; j < step; j++) {
//画图,要移动元素标红i,并且移到最下面,并且被比较的标绿jj
//左边赋值给右边
borad[i - j] = borad[i - j - 1];
//最后在把移动出来的数放在合适位置
if (j == step - 1) {
borad[i - j - 1] = temp;
}
}
//重置步数
step = 0;
}
//排序结束
long endtime = System.currentTimeMillis();
return endtime - starttime;
}
}
上面的是java代码实现的排序算法,对于这三种排序,只能处理小量数据,现实中用的较少,但是也不能少了对他们的研究。
由于文件太大,动态展示我就放在下面的包里面了,感兴趣的可以去看看呀。