几种常见的排序算法及其比较
1. 快速排序 及其 图解
import java.util.Random;
import java.util.Scanner;
public class QuickSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int[] arr = new int[m];
Random random = new Random();
for (int i = 0; i < m; i++) {
int r = random.nextInt(100) + 1;
arr[i] = r;
}
for (int i : arr) {
System.out.print(i + "\t");
}
System.out.println();
quickSort(arr, 0, m - 1);
for (int i : arr) {
System.out.print(i + "\t");
}
}
public static void quickSort(int array[], int left, int right) {
if (left >= right) return;
int x = array[left],i = left - 1, j = right + 1;
while(i < j)
{
do i++; while(array[i] < x);
do j--; while(array[j] > x);
if(i < j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
quickSort(array, left, j);
quickSort(array, j + 1, right);
}
}
2.归并排序 及其 图解
import java.util.Random;
import java.util.Scanner;
/**
* @author
* @version V1.0
* @create 2020-08-16 20:35
* @Description 归并排序
*/
public class MergeSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int[] arr = new int[m];
Random random = new Random();
for (int i = 0; i < m; i++) {
int r = random.nextInt(100) + 1;
arr[i] = r;
}
System.out.println("随机的数组: ");
for (int i : arr) {
System.out.print(i + "\t");
}
System.out.println();
System.out.println("排序后的数组: ");
mergeSort(arr, 0, m - 1);
for (int i : arr) {
System.out.print(i + "\t");
}
}
private static void mergeSort(int[] array, int left, int right) {
//当前区间只有一个数或者没有数 不用排序
if(left >= right) return;
int middle = (left + right) >>> 1; //确定中间分界点
//递归排序,排序完之后就有序了
mergeSort(array,left,middle);//递归排序左边
mergeSort(array,middle + 1,right);//递归排序右边
int[] temp = new int[array.length];
int k = 0; // 表示临时数组中有多少个数了。
// i 指针指向左半边的起点,j 指向右半边的起点
int i = left, j = middle + 1;
while (i <= middle && j <= right)
{
//把左边的部分放到临时数组
if(array[i] < array[j]) temp[k++] = array[i++];
else temp[k++] = array[j++];
}
//循环结束之后,把剩下的拿过来,有可能左半边的没有循环完,有可能右半边的没有循环完
while(i <= middle) temp[k++] = array[i++];
while(j <= right) temp[k++] = array[j++];
//把临时数组赋值回原数组
for(i = left, j = 0; i <= right; i++,j++)
{
array[i] = temp[j];
}
}
}
几种排序算法代码比较
package com.test.da819;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class CompareSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int[] arr = new int[m];
int[] brr = new int[m];
int[] crr = new int[m];
int[] drr = new int[m];
int[] err = new int[m];
int[] frr = new int[m];
// 随机
Random random = new Random();
for (int i = 0; i < m; i++) {
int r = random.nextInt(100) + 1;
arr[i] = r;
brr[i] = r;
crr[i] = r;
drr[i] = r;
err[i] = r;
frr[i] = r;
}
// 倒序
// for (int i = 0,j = m; i < m; i++,j--) {
// arr[i] = j;
// brr[i] = j;
// crr[i] = j;
// drr[i] = j;
// err[i] = j;
// frr[i] = j;
// }
// for (int i : arr) {
// System.out.print(i + "\t");
// }
// System.out.println();
//
System.out.println();
System.out.print("快速排序:");
QuickSort1 quickSort1 = new QuickSort1();
quickSort1.caleTime(arr, 0, arr.length - 1);
System.out.println();
System.out.print("归并排序: ");
MergeSort1 mergeSort1 = new MergeSort1();
mergeSort1.caleTime(brr, 0, brr.length - 1);
System.out.println();
System.out.print("Arrays.sort()排序:");
Sort1 sort1 = new Sort1();
sort1.caleTime(crr, 0, 0);
System.out.println();
System.out.print("优化后的冒泡:");
BubbleSort1 bubbleSort1 = new BubbleSort1();
bubbleSort1.caleTime(drr, 0, 0);
System.out.println();
System.out.print("未优化的冒泡排序:");
BubbleSort2 bubbleSort2 = new BubbleSort2();
bubbleSort2.caleTime(err, 0, 0);
System.out.println();
System.out.print("选择排序: ");
SelectionSort selectionSort = new SelectionSort();
selectionSort.caleTime(frr, 0, 0);
System.out.println();
// for (int i : arr) {
// System.out.print(i + "\t");
// }
// System.out.println();
}
}
abstract class CompareTime {
abstract void sorttest(int[] arr, int left, int right);
public void caleTime(int[] array, int left, int right) {
long start = System.currentTimeMillis();
sorttest(array, left, right);
long end = System.currentTimeMillis();
System.out.println("消耗的时间是:" + (end - start) + " ms");
}
}
class QuickSort1 extends CompareTime {
@Override
void sorttest(int[] array, int left, int right) {
if (left >= right)
return;
int x = array[left], i = left - 1, j = right + 1;
while (i < j) {
do
i++;
while (array[i] < x);
do
j--;
while (array[j] > x);
if (i < j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
sorttest(array, left, j);
sorttest(array, j + 1, right);
}
}
class MergeSort1 extends CompareTime {
@Override
void sorttest(int[] arr, int left, int right) {
if (left >= right)
return;
int middle = (left + right) >>> 1;
sorttest(arr, left, middle);
sorttest(arr, middle + 1, right);
int[] temp = new int[arr.length];
int k = 0;// 记录临时数组的长度
int i = left, j = middle + 1;
while (i <= middle && j <= right) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
while (i <= middle)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left, j = 0; i <= right; i++, j++) {
arr[i] = temp[j];
}
}
}
class Sort1 extends CompareTime {
@Override
void sorttest(int[] arr, int left, int right) {
Arrays.sort(arr);
}
}
class BubbleSort1 extends CompareTime {
@Override
void sorttest(int[] arr, int left, int right) {
boolean exchange;
for (int i = 0; i < arr.length - 1; i++) {
exchange = false;
for (int j = arr.length - 1; j > i; j--) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
exchange = true;
}
}
if (!exchange)
return;
}
}
}
class BubbleSort2 extends CompareTime {
@Override
void sorttest(int[] arr, int left, int right) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j + 1] < arr[j]) {
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
}
class SelectionSort extends CompareTime {
@Override
void sorttest(int[] arr, int left, int right) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[index] > arr[j]) {
index = j;
}
}
if (index != i) {
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
}
}
上述排序时间的比较,发现 归并排序 并不是 理想型的,选择排序反而有点优秀。我也不知道是什么原因。欢迎大佬评论~。
还有就是共享数组用这个方法实在是很low,希望大佬点出更优秀的方法。