目录
- 1. 稳点与非稳定排序
- 2. 冒泡排序
- 3. 简单选择排序
- 4. 直接插入排序
- 5. 快排
- 6. 堆排
- 7. 归并
1. 稳点与非稳定排序
- 不稳定的:快排、堆排、选择
- 原地排序:快排也是
- 非原地排序:归并 和三个线性时间排序:桶排序 ,计数,基数
2. 冒泡排序
- 时间复杂度 O(n*n)
- 空间复杂度 O(1)
- 稳定
public class ReviewToo {
public int[] BubbleSort(int[] a) {
int temp;
boolean flag;
o:
for (int i = 1; i < a.length; i++) {
flag = false;
for (int j = 0; j < a.length - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
flag = true;
}
}
if (flag == false) {
break o;
}
}
return a;
}
3. 简单选择排序
- 时间复杂度 O(n*n)
- 空间复杂度 O(1)
- 不稳定
public int[] simpleSelectSort(int[] a) {
int temp, min;
for (int i = 0; i < a.length - 1; i++) {
min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j]) {
min = j;
}
}
if (min != i) {
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
return a;
}
4. 直接插入排序
- 时间复杂度 O(n*n)
- 空间复杂度 O(1)
- 稳定
public int[] straightInsertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int value = a[i];
int index = i - 1;
while (index >= 0 && value < a[index]) {
if (value < a[index]) {
a[index+1]=a[index];
index--;
}
}
a[index+1]=value;
}
return a;
}
}
5. 快排
- 时间复杂度 O(n* log n)
- 空间复杂度 O(log n)
- 不稳定
public class FastSort {
public static void quikSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int num = arr[left];
int start = left;
int end = right;
while (start < end) {
while (start < end && arr[end] >= num) {
end--;
}
while (start < end && arr[start] <= num) {
start++;
}
if (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}
arr[left] = arr[start];
arr[start] = num;
quikSort(arr, left, start - 1);
quikSort(arr, start + 1, right);
}
public static void main(String[] args) {
int[] arr = {34, 1, 5, -2, 0, 35, 36, 38};
quikSort(arr, 0, arr.length - 1);
for (int value : arr) {
System.out.print(value+" ");
}
}
}
6. 堆排
public class Re {
public static void heapSort(int[] arr) {
int len = arr.length;
int[] top = new int[100];
for (int i = 0; i < top.length; i++) {
top[i] = arr[i];
}
buildHeap(top);
for (int i = len-1; i >=0 ; i--) {
swap(arr,0,i);
len--;
heapify(arr,0,len);
}
}
public static void buildHeap(int[] arr) {
int len = arr.length;
for (int i = len / 2 - 1; i >= 0; i--) {
heapify(arr, i, len);
}
}
public static void heapify(int[] arr, int i, int len) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int min = i;
if (left < len && arr[min] > arr[left]) {
min = left;
}
if (right < len && arr[min] > arr[right]) {
min = right;
}
if (min != i) {
swap(arr, min, i);
heapify(arr, min, len);
}
}
public static void swap(int[] arr, int i, int j) {
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
7. 归并
import java.util.Arrays;
public class MergeSort {
public static void split(int[] arr,int left,int right,int[] temp){
if (left<right){
int mid=(left+right)/2;
split(arr, left, mid, temp);
split(arr, mid+1, right, temp);
merge(arr,left,right,mid,temp);
}
}
public static void merge(int[] arr, int left, int right, int mid, int[] temp) {
int i = left;
int j = mid + 1;
int index = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[index] = arr[i];
i++;
index++;
} else {
temp[index] = arr[j];
j++;
index++;
}
}
while (j <= right) {
temp[index] = arr[j];
index++;
j++;
}
while ((i <= mid)) {
temp[index] = arr[i];
index++;
i++;
}
index=0;
int tempLeft=left;
while (tempLeft<=right){
arr[tempLeft]=temp[index];
tempLeft++;
index++;
}
}
public static void main(String[] args) {
int[] arr = new int[]{8, 4, 5, 7, 6, 2, 3, 9};
int[] temp=new int[arr.length];
split(arr,0, arr.length-1,temp);
System.out.println(Arrays.toString(arr));
}
}