冒泡排序、选择排序、插入排序、归并排序、快速排序、堆排序、希尔排序都是比较基础的排序算法,这几种排序时间复杂度、空间复杂度、稳定性如下图所示:
以下是JAVA实现的代码,供大家参考:
1、 冒泡排序
importjava.util.*;
publicclass BubbleSort {
publicint[] bubbleSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
inti,j,temp;
for(i=n-1;i>=0;i--)
{
for(j=0;j<i;j++)
{
if(A[j]>A[i])
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}
}
returnA;
}
}
2、选择排序
importjava.util.*;
publicclass SelectionSort {
publicint[] selectionSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
inti,j,ti,temp;
for(i=0;i<n-1;i++)
{
ti=i;
for(j=i+1;j<n;j++)
{
if(A[ti]>A[j])
{
ti=j;
}
}
temp=A[ti];
A[ti]=A[i];
A[i]=temp;
}
}
returnA;
}
}
3、插入排序
importjava.util.*;
publicclass InsertionSort {
publicint[] insertionSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
inti,j,ti,temp;
for(i=1;i<n;i++){
ti=i;
for(j=i-1;j>=0;j--)
{
if(A[ti]<A[j])
{
temp=A[ti];
A[ti]=A[j];
A[j]=temp;
ti=j;
}
}
}
}
returnA;
}
}
4、归并排序
importjava.util.*;
publicclass MergeSort {
publicint[] mergeSort(int[] A, intn) {
// write code here
if(A!=null&&n>2){
process(A,0,n-1);
}
returnA;
}
privatevoid process(int[] a,ints,inte)
{
if(s==e){
return;
}
intmid=(s+e)/2;
process(a,s,mid);
process(a,mid+1,e);
merge(a,s,mid,e);
}
privatevoid merge(int[] arr,intleft,intmid,intright)
{
int[] help = newint[right - left + 1];
intl = left;
intr = mid + 1;
intindex = 0;
while(l <= mid && r <= right) {
if(arr[l] <= arr[r]) {
help[index++] = arr[l++];
} else{
help[index++] = arr[r++];
}
}
while(l <= mid) {
help[index++] = arr[l++];
}
while(r <= right) {
help[index++] = arr[r++];
}
for(inti = 0; i < help.length; i++) {
arr[left + i] = help[i];
}
}
}
5、快速排序
importjava.util.*;
publicclass QuickSort {
publicint[] quickSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
//quickSortOne(A,0,n-1);
quickSortTwo(A,0,n-1);
}
returnA;
}
publicvoid quickSortOne(int[] A, intleft, intright){
if(left < right){
intk,tp,key,low,high;
//计算折半K
k = ( left + right ) / 2;
//置换
tp = A[left] ;
A[left] = A[k] ;
A[k] = tp;
//key
key = A[left] ;
//赋值
low = left+1;
high = right ;
while(low <= high){
while(high >= left && A[high] > key){ high--; }
while(low <= right && A[low] <= key){ low++; }
if(low < high){
//置换
tp = A[low] ;
A[low] = A[high] ;
A[high] = tp ;
}
}
//置换
tp = A[left] ;
A[left] = A[high] ;
A[high] = tp;
//回调
quickSortOne(A,left,high-1);
quickSortOne(A,high+1,right);
}
}
//精简版
publicvoid quickSortTwo(int[] A, intleft, intright)
{
if( left < right ) {
intlow,high,key;
low=left;
high=right;
key=A[left];
while(low<high)
{
while( low < high && A[high] >= key)
high--;
if(low < high)
A[low++] = A[high] ;
while(low < high && A[low] < key )
low++;
if(low < high)
A[high--] = A[low] ;
}
A[low] = key ;
quickSortTwo(A, left , low-1);
quickSortTwo(A, low+1, right);
}
}
}
6、堆排序
importjava.util.*;
publicclass HeapSort {
publicint[] heapSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
inttp;
for(inti=n;i>0;i--)
{
heapSortStep(A,i,n);
}
for(inti=n;i>1;i--)
{
tp=A[0];
A[0]=A[i-1];
A[i-1]=tp;
heapSortStep(A,1,i-1);
}
}
returnA;
}
publicvoid heapSortStep(int[] A,inti,intn)
{
inttp,j;
tp=A[i-1];
for(j=2*i;j<=n;j*=2)
{
if(j<n&&A[j-1]<A[j])
++j;
if(tp>=A[j-1])
break;
A[i-1]=A[j-1];
i=j;
}
A[i-1]=tp;
}
}
7、希尔排序
importjava.util.*;
publicclass ShellSort {
publicint[] shellSort(int[] A, intn) {
// write code here
if(A!=null&&n>2)
{
/*
int d,tp;
d=n;
while(true)
{
for(int i=0;i<d;i++)
{
for(int j=i;(j+d)<n;j+=d)
{
if(A[j]>A[j+d])
{
tp=A[j];
A[j]=A[j+d];
A[j+d]=tp;
}
}
}
if(d==1) break;
d--;
}
*/
intd,tp,i,j,k;
d=n;
while(true)
{
d/=2;
for(i=0;i<d;i++)
{
for(j=i+d;j<n;j+=d)
{
tp=A[j];
for(k=j-d;k>=0&&A[k]>tp;k-=d)
{
A[k+d]=A[k];
}
A[k+d]=tp;
}
}
if(d==1) break;
}
}
returnA;
}
}