java:三种经典大排序汇总,冒泡,插入,选择
来源:互联网 发布:excel2013数据有效性 编辑:程序博客网 时间:2024/05/21 22:23
package arrays.myArray;
public class SortArr {
public static void main(String[] args) {
int[] arrInt = { 4, 7, 8, 5, 6, 3, 2, 3, 4 };
maoPaoSort(arrInt);
print("冒泡排序:", arrInt);
arrInt = new int[]{ 4, 7, 8, 5, 6, 3, 2, 3, 4 };
insertSort(arrInt);
print("插入排序:", arrInt);
arrInt = new int[]{ 4, 7, 8, 5, 6, 3, 2, 3, 4 };
checkSort(arrInt);
print("选择排序:", arrInt);
}
// 冒泡排序
private static void maoPaoSort(int[] arrInt) {
int temp = 0;
for (int i = 0; i < arrInt.length; i++) {
for (int j = 0; j < arrInt.length - 1; j++) {
if (arrInt[i] < arrInt[j]) {
temp = arrInt[i];
arrInt[i] = arrInt[j];
arrInt[j] = temp;
}
}
}
}
// 插入排序
private static void insertSort(int[] arrInt) {
//倒数第二个开始比较
for (int i = arrInt.length - 2; i >= 0; i--) {
int j = 0;
for (j = arrInt.length - 1; j > i; j--) {
if (arrInt[i] > arrInt[j]) {
break;
}
}
int temp = arrInt[i];
for (int k = i; k < j; k++) {
arrInt[k] = arrInt[k + 1];
}
arrInt[j] = temp;
}
//正数第二个开始比较
/*for (int i = 1; i < arrInt.length; i++) {
int j = 0;
for (j = 0; j < arrInt.length - 1; j++) {
if (arrInt[i] > arrInt[j]) {
break;
}
}
int temp = arrInt[j];
for (int k = arrInt.length - 1; k > 0; k--) {
arrInt[k - 1] = arrInt[k];
}
arrInt[i] = temp;
}*/
}
// 选择排序
private static void checkSort(int[] arrInt) {
int min = 0;
int temp = 0;
// 1、记录最小值
/*
* for (int i = 0; i < arrInt.length; i++) { int min = arrInt[i]; for
* (int j = i + 1; j < arrInt.length; j++) { if (minValue > arrInt[j]) {
* temp = min; min = arrInt[j]; arrInt[j] = temp; } } arrInt[i] = min; }
*/
// 2、记录最小下标
for (int i = 0; i < arrInt.length; i++) {
min = i;
for (int j = i + 1; j < arrInt.length; j++) {
if (arrInt[min] > arrInt[j]) {
temp = arrInt[min];
arrInt[min] = arrInt[j];
arrInt[j] = temp;
}
}
i = min;
}
}
// 遍历
public static void print(String str, int[] arrInt) {
System.out.println(str);
/*
* for (int i = 0; i < arrInt.length; i++) {
* System.out.println(arrInt[i]); }
*/
for (int i : arrInt) {
System.out.print(i + ",");
}
System.out.println();
}
}
- java:三种经典大排序汇总,冒泡,插入,选择
- java冒泡_选择_插入三大排序
- 三大基本排序:选择、冒泡、插入
- 三大基本排序冒泡、插入、选择
- 三大基本排序--选择、冒泡、插入
- 三大排序:选择排序,冒泡排序,插入排序
- Java-数组的三大排序方法(冒泡排序,选择排序,插入排序)
- Java三种常见的排序算法:冒泡,选择,插入
- 三种基本排序(冒泡,选择,插入)
- 三种排序方法:插入、冒泡、选择
- 冒泡、选择、插入三种排序
- 冒泡、插入、选择--三种排序
- 冒泡,选择,插入三大基本排序解析以及Demo
- 三大基本排序总结 (冒泡、选择、插入)
- 三大简单排序算法——插入、选择、冒泡
- JAVA_三大排序_选择冒泡插入
- Java中实现简单的三大排序——冒泡、选择、插入
- Java三大常用数组元素排序法,插入/选择/冒泡
- java:手写MyArrayLisy的常用方法,增删改查
- URl list
- java:手写MyLinkedList所有方法,增删改查
- 登陆说明
- CUDA第一个程序(只有一个线程)
- java:三种经典大排序汇总,冒泡,插入,选择
- java:四则混合运算,模拟微软的计算器
- 宝宝Oracle学习总结
- .NET在后置代码中输入JS提示语句(背景不会变白)
- 知识库没有充分利用可能的原因解析
- java:四种int【】数据排序,冒泡,插入,选择,快速排序
- java:ArrayList循环遍历的俩种方法使用
- 一组数字随机排序
- [Microsoft][ODBC 驱动程序管理器]驱动程序的SQLSetConnectAttr 失败的解决办法!