数组的排序&冒泡排序&选择排序
思维导图:
排序算法介绍
常见的算法有选择排序、冒泡排序、快速排序、归并排序、希尔排序等,我们需要根据实际情况,择优选择合适的算法
排序算法的稳定性
排序算法有稳定排序也有不稳定排序
**稳定性定义:**排序前后两个相等的数相对位置不变,则算法稳定。
各排序算法的稳定性:
1.冒泡排序、归并排序等是稳定的排序算法;
2.快速排序、希尔排序等不是稳定的排序算法;
排序算法实现:
选择排序:
思想:
在一堆数据中,先用第一个数据与之后的所有数据进行比较,比较结果如果第一个数据大于后面的值,则进行交换;
若这样做,第一位就是全部数据中的最小值;
然后再用第二位上的数据重复上述操作步骤,把第二位上的数据变成次小数;
依次重复至倒数第二位;
选择排序代码:
package array;
import java.util.Arrays;
public class SelectSort {
/**
* 选择排序思想 18, 34, 79,62, 59, 15, 39, 75, 62, 10
*
* 10, 34, 79,62, 59, 18, 39, 75, 62, 15 确定 0 索引为最小数
*
* 10, 15, 79,62, 59, 34, 39, 75, 62, 18 确定 1 索引为最小数
*
* 10, 15, 18, 79,62, 59, 39, 75, 62, 34 确定 2 索引为最小数
*
* 依次进行到length-2索引
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 18, 34, 79, 62, 59, 15, 39, 75, 62, 10 };
TestArray.infoElement(array); // 随机赋值
System.out.println(Arrays.toString(array));
sort(array);
System.out.println(Arrays.toString(array));
}
/**
* 选择排序
*
* @param array
*/
private static void sort(int[] array) {
// TODO Auto-generated method stub
int sum = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
System.out.println(array[i] + " -> " + array[j]);
sum = array[i];
array[i] = array[j];
array[j] = sum;
}
}
System.out.println(Arrays.toString(array));
}
}
}
选择排序升级版(减少交换次数):
package array;
import java.util.Arrays;
public class SelectSort {
/**
* 选择排序思想 18, 34, 79,62, 59, 15, 39, 75, 62, 10
*
* 10, 34, 79,62, 59, 18, 39, 75, 62, 15 确定 0 索引为最小数
*
* 10, 15, 79,62, 59, 34, 39, 75, 62, 18 确定 1 索引为最小数
*
* 10, 15, 18, 79,62, 59, 39, 75, 62, 34 确定 2 索引为最小数
*
* 依次进行到length-2索引
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 18, 34, 79, 62, 59, 15, 39, 75, 62, 10 };
TestArray.infoElement(array); // 随机赋值
System.out.println(Arrays.toString(array));
sort(array);
System.out.println(Arrays.toString(array));
System.out.println("\n\n==============\n\n");
TestArray.infoElement(array); // 随机赋值
sortPlus(array); //目的:减少交换次数
}
/**
* 减少交换次数
* @param array
*/
private static void sortPlus(int[] array) {
// TODO Auto-generated method stub
int count = 0;
int con = 0;
int sum = 0;
for(int i = 0;i < array.length - 1;i++){
int index = i;//最小索引
for(int j = i + 1;j < array.length;j++){
count++;//比较次数
if(array[index] > array[j]){
index = j; //始终指向最小的元素
}
}
if(index != i){
con++; //交换次数
sum = array[i];
array[i] = array[index];
array[index] = sum;
}
System.out.println(Arrays.toString(array));
}
System.out.println("比较次数为:" + count);
System.out.println("交换次数为:" + con);
}
/**
* 选择排序
*
* @param array
*/
private static void sort(int[] array) {
// TODO Auto-generated method stub
int count = 0;
int sum = 0;
int con = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
count++;
if (array[i] > array[j]) {
con++;
System.out.println(array[i] + " -> " + array[j]);
sum = array[i];
array[i] = array[j];
array[j] = sum;
}
}
System.out.println(Arrays.toString(array));
}
System.out.println("比较次数为:" + count);
System.out.println("交换次数为:" + con);
}
}
冒泡排序
思想:
在一堆数据中,先拿第一个数据和第二个数据进行比较,如果前面的数据值大则进行交换,这是相邻元素进行比较;
再拿第二个数据和第三个数据进行比较,如果前面的数据值大则进行交换,这是相邻元素进行比较;
直到倒数第二位和倒数第一位进行比较,最后以为是所有数据汇总最大数;
按照相同逻辑,吧倒数第二位变成次大值;
多次执行上述步骤,知道所有数据都有序;
代码示例1:
package array;
import java.util.Arrays;
public class BubbleSort {
/**
* 冒泡排序
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = { 18, 34, 79, 62, 59, 15, 39, 75, 62, 10 };
TestArray.infoElement(array); // 随机赋值
System.out.println(Arrays.toString(array));
sortPlus(array);
System.out.println(Arrays.toString(array));
}
/**
* 冒泡排序升级版
* @param array
*/
private static void sortPlus(int[] array) {
// TODO Auto-generated method stub
int sum = 0;
for(int i = 0;i < array.length-1;i++){
boolean b = true; //标记
for(int j = 0; j < array.length - 1 - i; j++){
if(array[j] > array[j + 1]){
b = false;
sum = array[j];
array[j] = array[j+1];
array[j+1] = sum;
}
}
System.out.println(Arrays.toString(array));
if(b)
break;
}
}
/**
* 冒泡排序普通版
* @param array
*/
private static void sort(int[] array) {
// TODO Auto-generated method stub
int sum = 0;
for(int i = 0;i < array.length-1;i++)
for(int j = 0; j < array.length - 1 - i; j++){
if(array[j] > array[j + 1]){
sum = array[j];
array[j] = array[j+1];
array[j+1] = sum;
}
}
}
/**
* 吧最后一个索引上的数变成最大数
*/
public static void sort8(int[] its){
for(int j = 0; j < its.length - 1 - 1; j++){
int sum = 0;
if(its[j] > its[j + 1]){
sum = its[j];
its[j] = its[j + 1];
its[j+1] = sum;
}
}
}
/**
* 吧最后一个索引上的数变成最大数
*/
public static void sort9(int[] its){
for(int j = 0; j < its.length - 1; j++){
int sum = 0;
if(its[j] > its[j + 1]){
sum = its[j];
its[j] = its[j + 1];
its[j+1] = sum;
}
}
}
}