目录
一、冒泡排序
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/*
冒泡排序时间复杂度 o(n^2)
*/
public class bubbleSoft {
public static void main(String[] args) {
int[] arr = {4,3,2,1,-9};
bubbleSoft(arr);
//测试排序速度
/** int[] arr = new int[80000];
for (int i = 0;i < 80000;i++){
arr[i] = (int) (Math.random() * 800000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datas1 = simpleDateFormat.format(date1);
System.out.println("排序前的时间为:"+datas1);
bubbleSoft(arr);
Date date2 = new Date();
String datas2 = simpleDateFormat.format(date2);
System.out.println("排序后的时间为:"+datas2);**/
}
public static void bubbleSoft(int[] arr){
int temp;
boolean flag = false;
for (int i = 0;i < arr.length;i++){
for (int j = 0;j < arr.length-1;j++){
if (arr[j] > arr[j+1]){
flag = true;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
if (!flag){
break;
}else {
flag = false;
}
}
}
}
二、选择排序
属于内部排序法,是从欲排序的数据中,按指定的规则选出某一个元素,再依规定交换位置后达到排序的目的。
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/*
选择排序
时间复杂度为 o(n^2)
*/
public class selectSoft {
public static void main(String[] args) {
int[] arr = {5, 7, 3, 1, 2,-8,132};
selectSoft(arr);
/** int[] arr = new int[80000];
for (int i = 0;i < 80000;i++){
arr[i] = (int) (Math.random() * 800000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datas1 = simpleDateFormat.format(date1);
System.out.println("排序前的时间为:"+datas1);
selectSoft(arr);
Date date2 = new Date();
String datas2 = simpleDateFormat.format(date2);
System.out.println("排序后的时间为:"+datas2);**/
}
public static void selectSoft(int[] arr) {
for (int i = 0;i < arr.length-1;i++) {
int minIndex = i;
int min = arr[i];
for (int j = i+1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
minIndex = j;
}
}
if (minIndex != i){
arr[minIndex] = arr[i];
arr[i] = min;
}
System.out.println("第" + (i + 1) + "lun");
System.out.println(Arrays.toString(arr));
}
}
}
三、插入排序
插入式排序属于内部排序法,是对欲排序的元素以插入的方式寻找该元素适当的位置,以达到排序的目的。
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/*
插入排序
时间复杂度为 o(n^2)
*/
public class insertSoft {
public static void main(String[] args) {
int[] arr = {5,4,3,2,1,-4};
insertSoft(arr);
/**int[] arr = new int[80000];
for (int i = 0;i < 80000;i++){
arr[i] = (int) (Math.random() * 800000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datas1 = simpleDateFormat.format(date1);
System.out.println("排序前的时间为:"+datas1);
insertSoft(arr);
Date date2 = new Date();
String datas2 = simpleDateFormat.format(date2);
System.out.println("排序后的时间为:"+datas2);**/
}
public static void insertSoft(int[] arr){
for (int i = 1;i < arr.length;i++){
int insertVal = arr[i];
int insertIndex = i - 1;
while (insertIndex >= 0 && insertVal < arr[insertIndex]){
arr[insertIndex+1] = arr[insertIndex];
insertIndex--;
}
arr[insertIndex + 1] = insertVal;
System.out.println("第"+i+"轮");
System.out.println(Arrays.toString(arr));
}
}
}
四、shell排序
它是简单插入排序经过改进之后的一个高效版本,也称为缩小量排序。
shell排序是把记录按下标的一定增量分组,对每组使用直接插入排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1的时候,整个文件被分为一组,算法终止。
shell排序分为两种方法:交换法和移动法
交换法(略微慢的一种)
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/*
希尔排序 , 交换法
时间复杂度 o(n^s) (1<s<2)
*/
public class shellSoft {
public static void main(String[] args) {
int[] arr = {8,3,9,4,2,-6,-5,1,7,0};
shellSoft(arr);
/** int[] arr = new int[80000];
for (int i = 0;i < 80000;i++){
arr[i] = (int) (Math.random() * 800000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datas1 = simpleDateFormat.format(date1);
System.out.println("排序前的时间为:"+datas1);
shellSoft(arr);
Date date2 = new Date();
String datas2 = simpleDateFormat.format(date2);
System.out.println("排序后的时间为:"+datas2);**/
}
public static void shellSoft(int[] arr){
int temp = 0;
for (int gap = arr.length/2;gap > 0;gap /= 2){
for (int i = gap;i < arr.length;i++){
for (int j = i - gap;j >= 0;j -= gap){
if (arr[j] > arr[j + gap]){
temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
}
System.out.println("排序后:");
System.out.println(Arrays.toString(arr));
}
}
移动法
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/*
希尔排序 , 移动法
时间复杂度 o(n^s) (1<s<2)
*/
public class shellSoft {
public static void main(String[] args) {
int[] arr = {8,3,9,4,2,-6,-5,1,7,0};
shellSoft(arr);
//这里随机数量提高到8000000
/** int[] arr = new int[8000000];
for (int i = 0;i < 8000000;i++){
arr[i] = (int) (Math.random() * 80000000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datas1 = simpleDateFormat.format(date1);
System.out.println("排序前的时间为:"+datas1);
shellSoft(arr);
Date date2 = new Date();
String datas2 = simpleDateFormat.format(date2);
System.out.println("排序后的时间为:"+datas2);**/
}
public static void shellSoft(int[] arr){
for (int gap = arr.length/2;gap > 0;gap /= 2){
for (int i = gap;i < arr.length;i++) {
int j = i;
int temp = arr[j];
if (arr[j] < arr[j - gap]) {
while (j - gap >= 0 && temp < arr[j - gap]){
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}