1.冒泡排序
package learnday06排序;
//动态录入往数组里录入n个数字,并用冒泡排序
import java.util.Arrays;
import java.util.Scanner;
public class Maopaopaixu {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入n个整数");
int num = input.nextInt();
int[] array1 = new int[num];
System.out.println("n的值为:"+num);
for (int i = 0; i < array1.length; i++) {
System.out.println("请输入第" + (1 + i) + "个数字");
int number = input.nextInt();
array1[i] = number;
}
int[] array2 = maoPao(array1);
System.out.println(Arrays.toString(array2));
input.close();
}
public static int[] maoPao(int[] array) {
int c = 0;
for (int i = 0; i < array.length - 1; i++) {
boolean falg = false;//通过falg标识减少没有意义的比较
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
c = array[j];
array[j] = array[j + 1];
array[j + 1] = c;
falg = true;
}
}
if (falg = false) {
break;
}
}
return array;
}
}
2.选择排序
package learnday06排序;
import java.util.Arrays;
/*选择排序:是对冒泡排序的改进,它的比较次数与冒泡相同
但交换的次数小于冒泡,数据量较大时,效率会有很大提升
原理:从第一个元素开始往后找最小的元素然后交换位置,同理第二个
第三个直到排完序
*/
public class XuanzePaixu {
public static void main(String[] args) {
int[] array = {1, 11, 22, 34, 6, 8};
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;//索引
}
}
if (i != minIndex) {//加个判断自己不和自己交换,加快效率
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
System.out.println(Arrays.toString(array));
}
}
/*
---------------------选择排序------------
int[] array = {1,11,22,34,6,8};
for (int i = 0; i < array.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if (i != minIndex) {
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
System.out.println(Arrays.toString(array));
*/
/*
--------------冒泡排序-----------------------
int[] array = {1,11,22,34,6,8};
for (int i = 0; i < array.length - 1; i++) {
boolean falg = false;
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
falg = true;
}
}
if (falg = false) {
break;
}
}
System.out.println(Arrays.toString(array));
*/
/*
------------------插入排序-------------
int[] array = {1, 11, 22, 34, 6, 8};
for (int i = 0; i < array.length; i++) {
int temp = array[i];
int leftIndex = i - 1;
while (leftIndex >= 0 && array[leftIndex] > temp) {
array[leftIndex + 1] = array[leftIndex];
leftIndex--;
}
if (i != (leftIndex + 1)) {
array[leftIndex + 1] = temp;
}
}
System.out.println(Arrays.toString(array));
*/
3.插入排序
package learnday06排序;
import java.util.Arrays;
/*
插入排序:原理将指针指向某个(一般是第二个开始)元素,假设该元素左侧全部有序
,将该元素抽取出来,按照从右往左的顺序依次比较,遇到比他大的,将大的元素右移
直到找到比他小的元素或找到最左边发现其左侧的元素都比他大停止,此时会出现一个空位
将他放入这个空位,此时他的左侧元素都比他小,右侧都比他大,然后指针往后移动一位,
重复上述过程,没操作一次,左侧有序元素增加一个,右侧无序元素减少一个
*/
public class CharuPaixu {
public static void main(String[] args) {
int[] array = {1, 11, 22, 34, 6, 8};
for (int i = 0; i < array.length; i++) {
int temp = array[i];
int leftIndex = i - 1;
while (leftIndex >= 0 && array[leftIndex] > temp) {
array[leftIndex + 1] = array[leftIndex];
leftIndex--;
}
if (i != (leftIndex + 1)) {
array[leftIndex + 1] = temp;
}
}
System.out.println(Arrays.toString(array));
}
}
package learnday06排序;标签:minIndex,java,temp,int,插入排序,----,++,leftIndex,array From: https://www.cnblogs.com/wang1999an/p/16771757.html
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] array = {22, 12, 3, 6, 1, 46};
//----------------冒泡排序------------------------
for (int i = 0; i < array.length - 1; i++) {
boolean falg = false;
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
falg = true;
}
}
if (falg = false) {
break;
}
}
//----------------选择排序----------------------
// for (int i = 0; i < array.length - 1; i++) {
// int minIndex = i;
// for (int j = i + 1; j < array.length; j++) {
// if (array[minIndex] > array[j]) {
// minIndex = j;
// }
// }
// if (i != minIndex) {
// int temp = array[i];
// array[i] = array[minIndex];
// array[minIndex] = temp;
// }
// }
//---------------------插入排序---------------
// for (int i = 1; i < array.length; i++) {
// int temp = array[i];
// int leftIndex = i - 1;
// while (leftIndex >= 0 && array[leftIndex] > temp) {
// array[leftIndex + 1] = array[leftIndex];
// leftIndex--;
// }
// if (i != leftIndex) {
// array[leftIndex + 1] = temp;
// }
//
// }
System.out.println(Arrays.toString(array));
}
}