- 冒泡排序
/**
* 冒泡排序
*/
public static int[] mpSort(int[] a) {
boolean falg = true;
for(int i=0;i<a.length-1;i++) {
falg = true;
for(int j=0;j<a.length-1;j++) {
if(a[j]>a[j+1]) {
falg = false;
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
if(falg) {
break;
}
}
return a;
}
- 插入排序
/**
* 插入排序
*/
public static int[] insertSort(int[] a) {
for(int i=1;i<a.length;i++) {
int temp = a[i];
int j;
for(j=i;j>0&&temp<a[j-1];j--) {
a[j] = a[j-1];
}
a[j] = temp;
}
return a;
}
- 希尔排序
/**
* 希尔排序 二分法
*/
public static int[] ShellSort(int[] a) {
int step = a.length;
while(step!=0) {
for(int i=step;i<a.length;i++) {
int temp = a[i];
int j;
for(j=i;j>step&&temp<a[j-step];j-=step) {
a[j] = a[j-step];
}
a[j] = temp;
}
step = step/2;
}
return a;
}
/**
* 希尔排序 斐波拉数列
*/
public static int[] ShellSortByFibo(int[] a) throws Exception {
//创造斐波拉数列
int[] fibo = CreateFibo();
for(int n=fibo.length-1;n>0;n--){
int step = fibo[n];
for(int i=step;i<a.length;i++) {
int temp = a[i];
int j;
for(j=i;j>step&&temp<a[j-step];j-=step) {
a[j] = a[j-step];
}
a[j] = temp;
}
}
return a;
}
- 选择排序
/**
* 选择排序
*/
public static int[] SelectSort(int[] a) {
for(int i=0;i<a.length;i++) {
int n = i;
for(int j=i+1;j<a.length;j++) {
if(a[n]>a[j]) {
n=j;
}
}
int temp = a[i];
a[i] = a[n];
a[n] = temp;
}
return a;
}
- 快速排序
/**
* 快速排序
*/
public static int[] QSort(int low,int high,int[] a) {
if(low<high) {
int pivotloc = Partition(low,high,a);
QSort(low,pivotloc-1,a);
QSort(pivotloc+1,high,a);
}
return a;
}
public static int Partition(int i,int j,int[] a) {
int pivot = a[i];
while(i<j) {
while(i<j&&a[j]>=pivot) {
j--;
}
if(i<j) {
a[i] = a[j];
i++;
}
while(i<j&&a[i]<pivot) {
i++;
}
if(i<j) {
a[j] = a[i];
j--;
}
}
a[i] = pivot;
return i;
}
- 归并排序
/**
*归并排序
*/
public static void merge(int[] r,int[] order,int h,int m,int t) {
int i=h,j=m+1,k=h;
while(i<=m&&j<=t) {
if(r[i]<r[j]) {
order[k++] = r[i++];
}
else
order[k++] = r[j++];
}
while(i<=m) {
order[k++] = r[i++];
}
while(j<=t) {
order[k++] = r[j++];
}
}
public static void mergepass(int[] r,int[] order,int s,int n) {
int p=0;
while(p+2*s-1<=n-1) {
merge(r,order,p,p+s-1,p+2*s-1);
p+=2*s;
}
if(p+s-1<n-1) {
merge(r,order,p,p+s-1,n-1);
}
else {
for(int i=p;i<n-1;i++) {
order[i] = r[i];
}
}
}
public static int[] mergeSort(int[] r) {
int s=1;
int n = r.length;
int[] temp = new int[n];
while(s<n) {
mergepass(r,temp,s,n);
s*=2;
mergepass(temp,r,s,n);
s*=2;
}
return r;
}
- 堆排序
/**
*堆排序
*/
public static int[] heapSort(int[] r) {
int temp = r.length;
for(int i=r.length/2;i>0;i--)
{
sift(i,temp,r);
}
while(temp>0) {
int temp2 = r[temp-1];
r[temp-1] = r[0];
r[0] = temp2;
temp--;
sift(1,temp,r);
}
return r;
}
public static void sift(int low,int high,int[] r) {
int i = low;
boolean falg = true;
int index = 0;
for (int j = i * 2; j <= high && falg; j = (index+1)*2) {
if (j + 1 <= high) {
index = (r[j - 1] < r[j]) ? j - 1 : j;
} else {
index = j - 1;
}
if (r[j/2-1] > r[index]) {// 将父节点与子节点中较小的交换
int temp1 = r[j/2-1];
r[j/2-1] = r[index];
r[index] = temp1;
} else
falg = false;
}
}
- 链式基数排序
/**
* @author ASUS
* 链式基数排序
*/
static Node<Integer>[] a = new Node[10];
static Node<Integer>[] b = new Node[10];
public static int[] LinkBaseSort(int[] r,int dr) {
Node<Integer> head = new Node(r[0]);
Node<Integer> p = head;
for(int i=1;i<r.length;i++) {
Node<Integer> node = new Node(r[i]);
p.next = node;
p = p.next;
}
int d=1;
while(d<=dr) {
p = head;
while(p!=null) {
int num = (p.data%(int)(Math.pow(10, d))-(p.data%(int)(Math.pow(10, d-1))))/(int)(Math.pow(10, d-1));
switch(num) {
case 0: addNode(0,p.data);break;
case 1: addNode(1,p.data);break;
case 2: addNode(2,p.data);break;
case 3: addNode(3,p.data);break;
case 4: addNode(4,p.data);break;
case 5: addNode(5,p.data);break;
case 6: addNode(6,p.data);break;
case 7: addNode(7,p.data);break;
case 8: addNode(8,p.data);break;
case 9: addNode(9,p.data);break;
}
p = p.next;
}
for(int i=0;i<b.length-1;i++) {
if(b[i]!=null) {
for(int j=i+1;j<a.length;j++) {
if(a[j]!=null) {
b[i].next = a[j];
i = j-1;
break;
}
}
}
}
for(int i=0;i<a.length;i++) {
if(a[i]!=null) {
head = a[i];
break;
}
}
for(int i=0;i<a.length;i++) {
a[i] = null;
b[i] = null;
}
d++;
}
p = head;
int index = 0;
while(p!=null) {
r[index] = p.data;
index++;
p = p.next;
}
return r;
}
public static void addNode(int i,int n) {
if(a[i]==null) {
a[i] = new Node(n);
b[i] = a[i];
}
else {
b[i].next = new Node(n);
b[i] = b[i].next;
}
}
}
class Node<T>{
public T data;
public Node<T> next;
public Node() {
this(null,null);
}
public Node(T data) {
this(data,null);
}
public Node(T data,Node<T> next) {
this.data = data;
this.next = next;
}
}