自己以前学的是C++版的数据结构后来学java对于数据结构中的排序虽然熟悉但是都已经忘了
所以今天就重新用java写了,如有不足之处,还望不吝赐教!
先从简单的开始吧
直接插入排序
主要思想:每次与前一个关键字比较大的值向后移一位。
package com.iss.demo;
public class InsertSort {
/**
* 直接插入比较的基本原理每次与前一个比较大的向后移
* @param args
*/
// 直接插入排序
public static void main(String[] args) {
int[] a = { 2, 1, 4, 2, 7, 8, 6 };
System.out.println("排序前:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
// 每次与前一个进行比较,若前面一个比较若比自己大就像后移一位
for (int i = 1; i < a.length; i++) {
int j;
int temp = a[i];
for (j = i - 1; j >= 0; j--) {
if (a[j] > temp) {
a[j + 1] = a[j];// 如果a[j]>temp向后移一位
} else {
break;
}
}
a[j + 1] = temp;//不是a[j] 原因是循环后j--
}
System.out.println();
System.out.println("排序后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
选择排序
主要思想:一趟排序中选出最小的赋给数组的第一个值。
public class SelectionSort {
// 选择排序每次比较取出最小值给一个下标
public static void selectionSort(int[] a) {
int min = 0;
int temp;
for (int i = 0; i < a.length; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) {
min = j;
}
}
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
public static void main(String[] args) {
int[] a = { 2, 1, 4, 5, 9, 8, 7 };
selectionSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
最经典冒泡排序
主要思想:每一趟两两比较取最大值一共比较n-1-i次。
public class BubbleSort {
public void bubbleSort(int[] arr) {
// 冒泡排序基本原理比较取最大值 每一趟需要比较n-1-i次
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = { 2, 1, 3, 4, 6, 5 };
// new BubbleSort().bubbleSort(arr);
// new BubbleSort().SelectionSort(arr);
new BubbleSort().bubbleSort1(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
最不容易的理解的快速排序
主要思想:
通过一趟排序将排序记录分割成独立的两部分,其中一部分记录的关键字比另一部分的关键字小,
然后对这两部分再进行排序直到,整个序列有序。
算法思想:把整个序列看做一个数组,把第零个位置和最后一个比较,如果比它小交换,否则不
做处理;交换以后再和小的那端比。比他小不交换,比他大交换。这样循环往复,一趟排序完成,
左边就是比中轴小的,右边就是比中轴大的,然后再用分治法,分别对独立的数组进行排序。
package com.iss.sort;
public class QuickSort {
public int getMiddle(int[] a, int low, int high) {
int temp = a[low];
while (low < high) {
while (low < high && temp <= a[high]) {
high--;
}// 若不加等于会死循环
a[low] = a[high];
while (low < high && temp >= a[low]) {
low++;
}
a[high] = a[low];
}
a[low] = temp;// 中轴记录到尾
return low;
}
public void quickSort(int[] a, int low, int high) {
if (low < high) {
int middle = this.getMiddle(a, low, high);
quickSort(a, low, middle - 1);
quickSort(a, middle + 1, high);
}
}
// public void quick_sort(int[] a) {
// if (a.length > 0) {
// this.quickSort(a, 0, a.length - 1);
// }
// }
public static void main(String[] args) {
int[] a = { 2, 1, 4, 2, 6, 8, 90, 4, 5 };
new QuickSort().quickSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
附赠另一种方式的快速排序
package com.iss.sort;
public class QuickSort1 {
public void quickSort(int[] a, int left, int right) {
int i, j, middle, temp;
i = left;
j = right;
middle = (i + j) / 2;
while (i < j) {
while (a[i] < a[middle] && i < right) {
i++;
}// 不能是等于是等于中间的值不变了就无法成功排序
while (a[j] > a[middle] && j > left) {
j--;
}
if (i <= j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
if (i < right) {
this.quickSort(a, i, right);
}
if (j > left) {
this.quickSort(a, left, j);
}
}
public static void main(String[] args) {
int[] a = { 2, 2, 1, 4, 67, 3, 7, 8, 9 };
new QuickSort1().quickSort(a, 0, a.length - 1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
几种排序的时间复杂度
选择排序算法时间复杂度是O(n^2)
插入排序算法时间复杂度是O(n^2)
冒泡排序算法时间复杂度是O(n^2)
快速排序是不稳定的。最理想情况算法时间复杂度O(nlog2n),最坏O(n^2)。