排序算法
1 插入类排序:
1.1 直接插入排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
* 把n个待排序的数字看成一个有序表和一个无序表,遍历无序表中的数字,将其与有序表中数据比较,插入到适当位置
* 时间复杂度O(n^2),空间复杂度O(1)
* 稳定的排序算法
* 适用于待排序数较少且基本有序的情况
*/
public class InsertSort {
publicstatic void main(String[] args) {
int[]arr = { 48, 62, 35, 77, 55, 14, 35, 98 };
sort(arr);
}
publicstatic void sort(int[] arr) {
inttemp = 0;
intj = 0;
for(int i = 1; i < arr.length; i++) {
temp= arr[i];
j= i - 1;
while(j >= 0 && temp < arr[j]) {
arr[j+ 1] = arr[j];
j--;
}
arr[j+ 1] = temp;
}
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
1.2 折半插入排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
*第i趟插入,先用二分法找出第i+1个元素应该插入的的位置,假定前i个数据是已经处于有序状态。
*时间复杂度O(n^2)
*改善了插入排序算法中比较次数的数量级但并未改变移动元素的时间耗费
*/
public class BinaryInsertSort {
publicstatic void main(String[] args) {
int[]arr = { 48, 62, 35, 77, 55, 14, 35, 98 };
sort(arr);
}
publicstatic void sort(int[] arr) {
inttemp = 0;
for(int i = 1; i < arr.length; i++) {
if(arr[i] < arr[i - 1]) {
temp= arr[i];
intlow = 0;
inthigh = i - 1;
intmid = 0;
while(low <= high) {
mid= (low + high) / 2;
if(temp < arr[mid]) {
high= mid - 1;
}else {
low= mid + 1;
}
}
//将low~i处数据整体向后移动1位
for(int j = i - 1; j >= low; j--) {
arr[j+ 1] = arr[j];
}
arr[low]= temp;
}
}
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
1.3 希尔排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
*将待排序序列分割成多个子序列,分别用直接插入排序,最后整个序列已经基本有序,再对序列进行一次直接插入排序
*时间复杂度O(n^1.5),空间复杂度O(1)
*不稳定
*对中等规模(n<=1000)的序列有较高的效率
*/
public class ShellInsertSort {
publicstatic void main(String[] args) {
int[]arr = { 46, 55, 13, 42, 94, 17, 05, 70 };
sort(arr);
}
publicstatic void sort(int[] arr) {
inttemp = 0;
intj = 0;
for(int d = arr.length / 2; d > 0; d /= 2) {
for(int i = d; i < arr.length; i++) {
temp= arr[i];
j= i;
while(j >= d && temp < arr[j - d]) {
arr[j] = arr[j - d];
j-= d;
}
arr[j]= temp;
}
}
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
2 交换类排序:
2.1 冒泡排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
*相邻比序法
*时间复杂度O(n^2),空间复杂度O(1)
*稳定
*
*/
public class BubbleSort {
publicstatic void main(String[] args) {
int[]arr = { 48, 62, 35, 77, 55, 14, 35, 98, 22, 40 };
sort(arr);
}
publicstatic void sort(int[] arr) {
for(int i = 0; i < arr.length - 1; i++) {
for(int j = 0; j < arr.length - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
arr[j]= arr[j] + arr[j + 1];
arr[j+ 1] = arr[j] - arr[j + 1];
arr[j]= arr[j] - arr[j + 1];
}
}
}
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
2.2 快速排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
*选出一个枢轴k,小于k的放在前边,大于k的放后边,递归调用
*时间复杂度O(n*log2n),空间复杂度O(log2n)
*不稳定
*/
public class QuickSort {
publicstatic void main(String[] args) {
int[]arr = { 48, 62, 35, 77, 55, 14, 35, 98 };
sort(arr,0, arr.length - 1);
}
publicstatic void sort(int[] arr, int low, int high) {
if(low < high) {
intpos = adjust(arr, low, high);
sort(arr,low, pos - 1);
sort(arr,pos + 1, high);
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
privatestatic int adjust(int[] arr, int low, int high) {
intpivot = arr[low];
while(low < high) {
while(high > low && compare(pivot, arr[high]) <= 0) {
high--;
}
arr[low]= arr[high];
while(low < high && compare(pivot, arr[low]) >= 0) {
low++;
}
arr[high]= arr[low];
}
arr[low]= pivot;
returnlow;
}
privatestatic int compare(int a, int b) {
returna - b;
}
}
3 选择类排序:
3.1 选择排序:
package sort;
import java.util.Arrays;
/**
*
*@author xiaoming
* 每一趟从待排序的数据元素中选出最小的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
*时间复杂度O(n^2)
*不稳定
*/
public class SelectSort {
publicstatic void main(String[] args) {
int[]arr = { 48, 62, 35, 77, 55, 14, 35, 98 };
sort(arr);
}
publicstatic void sort(int[] arr) {
intk = 0;
inttemp = 0;
for(int i = 0; i < arr.length - 1; i++) {
k= i;
for(int j = i + 1; j < arr.length; j++) {
if(arr[k] > arr[j]) {
k= j;
}
}
if(k != i) {
temp= arr[i];
arr[i]= arr[k];
arr[k]= temp;
}
}
Stringstr = Arrays.toString(arr);
System.out.println(str);
}
}
3.2 堆排序:
package sort;
/**
*
*@author xiaoming
*时间复杂度O(n*log2n)
*不稳定
*/
public class HeapSort {
/*
* 将数组调整为小根堆,即由小到大排序
*/
publicstatic int[] heap = new int[] { 1, 3, 7, 5, 2, 8, 4, 6, 10, 9 };
publicstatic void main(String[] args) {
inttemp;
/*
* 创建堆(对该堆进行简单的排序)
*/
CreateHeap();
for(int i = heap.length - 1; 0 < i; i--) {
temp= heap[0];
heap[0]= heap[i];
heap[i]= temp;
/*
* 展示每次排序后的结果
*/
for(int j = 0; j < heap.length; j++) {
System.out.print(heap[j]+ " ");
}
System.out.println();//换行
/*
* 从堆顶进行调整,使未排序堆中最大关键字到堆顶
*/
AdjustHeap(0,i);
}
}
/*
* 调整堆使其堆顶为未排序堆中最大关键字
*/
publicstatic void AdjustHeap(int location, int unSortlength) {
inttemp;
inttempLoc;
/*
* 确保左右节点存在
*/
if((tempLoc = (location + 1) * 2) < unSortlength) {
/*
* 判断左右节点大小
*/
if(heap[tempLoc] >= heap[tempLoc - 1]) {
/*
* 判断父节点与子节点的大小,若父节点小,则与大的子节点换位
*/
if(heap[location] < heap[tempLoc]) {
temp= heap[location];
heap[location]= heap[tempLoc];
heap[tempLoc]= temp;
/*
* 递归法对换位后的子节点及其子节点进行调整
*/
AdjustHeap(tempLoc,unSortlength);
}
}else {
/*
* 左节点大于右节点
*/
if(heap[location] < heap[tempLoc - 1]) {
temp= heap[location];
heap[location]= heap[tempLoc - 1];
heap[tempLoc- 1] = temp;
/*
* 递归法对换位后的子节点及其子节点进行调整
*/
AdjustHeap(tempLoc- 1, unSortlength);
}
}
}
/*
* 确保左节点存在
*/
elseif ((tempLoc = (location + 1) * 2 - 1) < unSortlength) {
/*
* 与左节点进行比较
*/
if(heap[location] < heap[tempLoc]) {
/*
* 左子节点大于父节点,将两者进行换位
*/
temp= heap[location];
heap[location]= heap[tempLoc];
heap[tempLoc]= temp;
AdjustHeap(tempLoc,unSortlength);
}
}
}
// 创建堆(对该堆进行简单的排序)
publicstatic void CreateHeap() {
for(int i = heap.length - 1; i >= 0; i--) {
AdjustHeap(i,heap.length);
}
}
}
4 归并排序:
package sort;
/**
*
*@author xiaoming
*时间复杂度O(n*log2n),空间复杂度O(n)
*稳定
*/
public class MergeSort {
privateint[] SortOut;
publicvoid printSortedOutput() {
for(int i = 0; i < this.SortOut.length; i++) {
System.out.print(this.SortOut[i]+ " ");
}
}
publicstatic void main(String[] args) {
int[]in = { 2, 5, 3, 8, 6, 7, 1, 4, 0, 9 };
MergeSortmsTest = new MergeSort(in);
msTest.printSortedOutput();
}
publicMergeSort(int[] in) {
this.SortOut= this.MergeSort(in);
}
privateint[] MergeSort(int[] i_list) {
if(i_list.length == 1) {
returni_list;
}else {
int[]listL = new int[i_list.length / 2];
int[]listR = new int[i_list.length - i_list.length / 2];
intCenter = i_list.length / 2;
for(int i = 0; i < Center; i++) {
listL[i]= i_list[i];
}
for(int i = Center, j = 0; i < i_list.length; i++, j++) {
listR[j]= i_list[i];
}
int[]SortedListL = MergeSort(listL);
int[]SortedListR = MergeSort(listR);
int[]o_list = MergeTwoList(SortedListL, SortedListR);
returno_list;
}
}
privateint[] MergeTwoList(int[] listL, int[] listR) {
inti = 0, j = 0;
int[]o_list = new int[listL.length + listR.length];
intfoot = 0;
while(i < listL.length && j < listR.length) {
if(listL[i] <= listR[j]) {
o_list[foot]= listL[i];
i++;
}else {
o_list[foot]= listR[j];
j++;
}
foot++;
}
if(i == listL.length) {
while(j < listR.length) {
o_list[foot++]= listR[j++];
}
}else { // j==listR.length
while(i < listL.length) {
o_list[foot++]= listL[i++];
}
}
returno_list;
}
}