冒泡排序
/*
* 冒泡排序
* 相邻元素比较,大的元素往后调
*/
public static void bubbleSort(int array[]){
for(int i = array.length - 1
boolean flag = false
for(int j = 0
if(array[j] > array[j+1]){
swap(array, j, j+1)
flag = true
}
}
if(!flag)
break
}
}
选择排序
/*
* 选择排序
* 每个位置选择当前元素最小的
*/
public static void selectSort(int array[]){
for(int i = 0
int minPosition = i
int min = array[i]
for(int j = i+1
if(array[j] < min){
min = array[j]
minPosition = j
}
}
//若i不是当前元素最小的,则和找到的那个元素交换
if(i != minPosition){
array[minPosition] = array[i]
array[i] = min
}
}
}
插入排序
public static void insertSort(int array[]){
for(int i = 1 ; i < array.length ; i++){
int current = array[i];
int j = i;
for(; j > 0 && array[j - 1] > current ; j--){
array[j] = array[j - 1];
}
array[j] = current;
}
}
快速排序
public static void quickSort(int a[]){
qSort(a, 0, a.length - 1);
}
public static void quickSortNonRecursion(int array[]){
if (array == null || array.length == 1) return;
Stack<Integer> s = new Stack<Integer>();
s.push(0);
s.push(array.length - 1);
while (!s.empty()) {
int right = s.pop();
int left = s.pop();
if (right <= left) continue;
int i = partition(array, left, right);
if (left < i - 1) {
s.push(left);
s.push(i - 1);
}
if (i + 1 < right) {
s.push(i+1);
s.push(right);
}
}
}
public static void qSort(int a[],int low,int high){
int pivot = 0;
if(low < high){
pivot = partition(a,low,high);
qSort(a,low,pivot);
qSort(a,pivot + 1,high);
}
}
public static int partition(int a[],int low,int high){
int pivotkey = a[low];
while(low < high){
while(low < high && a[high] >= pivotkey){
high--;
}
a[low] = a[high];
while(low < high && a[low] <= pivotkey){
low++;
}
a[high] = a[low];
}
a[low] = pivotkey;
return low;
}
归并排序
public static void merge(int a[], int first, int mid, int last, int temp[]){
int i = first,j = mid+1;
int k = 0;
while(i <= mid && j<= last){
if(a[i]<a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i <= mid)
temp[k++] = a[i++];
while(j <= last)
temp[k++] = a[j++];
for(i = 0 ; i < k ; i++)
a[first+i] = temp[i];
}
public static void mSort(int a[], int first,int last, int temp[]){
if(first < last){
int mid = (first + last) / 2;
mSort(a, first, mid, temp);
mSort(a, mid+1, last, temp);
merge(a, first, mid, last, temp);
}
}
public static void mergeSort(int a[]){
int[] temp = new int[a.length];
mSort(a, 0, a.length-1, temp);
}
希尔排序
/*
* 希尔排序
* 按照不同步长对元素进行插入排序
* 插入排序的一种
*/
public static void shellSort(int a[]){
if(a == null || a.length == 0){
return
}
int len = a.length
//初始化增量
int inc = len
do{
//增量变化规则
inc = inc / 3 + 1
for(int i = inc
//待排元素
int cur = a[i]
int j = i
//向前扫描,只要发现待排元素比较小,就插入
for(
//移除空位
a[j] = a[j - inc]
}
//元素插入
a[j] = cur
}
}while(inc > 1)
}
堆排序
public static void heapSort(int[] a) {
if(a == null || a.length == 0){
return;
}
int len = a.length;
//从尾部开始,调整成最大堆
for(int i = len / 2 - 1; i >= 0; i--){
maxHeapDown(a, i, len - 1);
}
//从最后一个元素开始对序列进行调整,不断缩小调整的范围直到第一个元素
for(int i = len - 1; i >= 0; i--){
//交换a[0]和a[i]。交换后,a[i]是a[0..i]中最大
int tmp = a[0];
a[0] = a[i];
a[i] = tmp;
//调整a[0..i - 1],使得a[0..i - 1]仍然是一个最大堆
maxHeapDown(a, 0, i - 1);
}
}
private static void maxHeapDown(int[] a, int lo, int hi){
//记录当前结点位置
int curIndex = lo;
//记录左孩子结点
int left = 2 * curIndex + 1;
//记录当前结点的值
int curVal = a[curIndex];
//保证curIndex,leftIndex,rightIndex中,curIndex对应的值最大
for(; left <= hi; curIndex = left, left = 2 * left + 1){
//左右孩子中选择较大者
if(left < hi && a[left] < a[left + 1]){
left++;
}
if(curVal >= a[left]){
break;
}else{
a[curIndex] = a[left];
a[left] = curVal;
}
}
}
基数排序
/*
* 基数排序
* 按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位
*/
public static void radixSort(int[] array,int d)
{
int n=1
int k=0
int length=array.length
int[][] bucket=new int[10][length]
int[] order=new int[length]
while(n<d)
{
for(int num:array) //将数组array里的每个数字放在相应的桶里
{
int digit=(num/n)%10
bucket[digit][order[digit]]=num
order[digit]++
}
for(int i=0
{
if(order[i]!=0) //这个桶里有数据,从上到下遍历这个桶并将数据保存到原数组中
{
for(int j=0
{
array[k]=bucket[i][j]
k++
}
}
order[i]=0
}
n*=10
k=0
}
}
通用的swap函数
public static void swap(int array[],int i,int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
总结
- 下面是一个总的表格,大致总结了我们常见的所有的排序算法的特点。

