曾经学数据结构的时候,各种排序练的很熟,但是想过用Java怎么实现吗,以下给出来给你看看,当然闲着就当学习数据结构了,因为jdk提供的工具足够你应付所有事情。
插入排序:
package org.rut.util.algorithm.support;
imp
public class InsertSort implements SortUtil.Sort{
public void sort(int[] da
int temp;
for(int i=1;i<da
for(int j=i;(j>0)&&(da
SortUtil.swap(da
}
}
}
}
冒泡排序:
package org.rut.util.algorithm.support;
imp
public class BubbleSort implements SortUtil.Sort{
public void sort(int[] da
int temp;
for(int i=0;i<da
for(int j=da
if(da
SortUtil.swap(da
}
}
}
}
}
选择排序:
package org.rut.util.algorithm.support;
imp
public class SelectionSort implements SortUtil.Sort {
public void sort(int[] da
int temp;
for (int i = 0; i < da
int lowIndex = i;
for (int j = da
if (da
lowIndex = j;
}
}
SortUtil.swap(da
}
}
}
Shell排序:
package org.rut.util.algorithm.support;
imp
public class ShellSort implements SortUtil.Sort{
public void sort(int[] da
for(int i=da
for(int j=0;j<i;j++){
insertSort(da
}
}
insertSort(da
}
private void insertSort(int[] da
int temp;
for(int i=start+inc;i<da
for(int j=i;(j>=inc)&&(da
SortUtil.swap(da
}
}
}
}
快速排序:
package org.rut.util.algorithm.support;
imp
public class QuickSort implements SortUtil.Sort{
public void sort(int[] da
quickSort(da
}
private void quickSort(int[] da
int pivotIndex=(i+j)/2;
//swap
SortUtil.swap(da
int k=partition(da
SortUtil.swap(da
if((k-i)>1) quickSort(da
if((j-k)>1) quickSort(da
}
private int partition(int[] da
do{
while(da
while((r!=0)&&da
SortUtil.swap(da
}
while(l<r);
SortUtil.swap(da
return l;
}
}
改进后的快速排序:
package org.rut.util.algorithm.support;
imp
public class ImprovedQuickSort implements SortUtil.Sort {
private static int MAX_STACK_SIZE=4096;
private static int THRESHOLD=10;
public void sort(int[] da
int[] stack=new int[MAX_STACK_SIZE];
int top=-1;
int pivot;
int pivotIndex,l,r;
stack[++top]=0;
stack[++top]=da
while(top>0){
int j=stack[top--];
int i=stack[top--];
pivotIndex=(i+j)/2;
pivot=da
SortUtil.swap(da
//partition
l=i-1;
r=j;
do{
while(da
while((r!=0)&&(da
SortUtil.swap(da
}
while(l<r);
SortUtil.swap(da
SortUtil.swap(da
if((l-i)>THRESHOLD){
stack[++top]=i;
stack[++top]=l-1;
}
if((j-l)>THRESHOLD){
stack[++top]=l+1;
stack[++top]=j;
}
}
//new InsertSort().sort(da
insertSort(da
}
private void insertSort(int[] da
int temp;
for(int i=1;i<da
for(int j=i;(j>0)&&(da
SortUtil.swap(da
}
}
}
}
归并排序:
package org.rut.util.algorithm.support;
imp
public class MergeSort implements SortUtil.Sort{
public void sort(int[] da
int[] temp=new int[da
mergeSort(da
}
private void mergeSort(int[] da
int mid=(l+r)/2;
if(l==r) return ;
mergeSort(da
mergeSort(da
for(int i=l;i<=r;i++){
temp=da
}
int i1=l;
int i2=mid+1;
for(int cur=l;cur<=r;cur++){
if(i1==mid+1)
da
else if(i2>r)
da
else if(temp[i1]<temp[i2])
da
else
da
}
}
}
改进后的归并排序:
package org.rut.util.algorithm.support;
imp
public class ImprovedMergeSort implements SortUtil.Sort {
private static final int THRESHOLD = 10;
public void sort(int[] da
int[] temp=new int[da
mergeSort(da
}
private void mergeSort(int[] da
int i, j, k;
int mid = (l + r) / 2;
if (l == r)
return;
if ((mid - l) >= THRESHOLD)
mergeSort(da
else
insertSort(da
if ((r - mid) > THRESHOLD)
mergeSort(da
else
insertSort(da
for (i = l; i <= mid; i++) {
temp = da
}
for (j = 1; j <= r - mid; j++) {
temp[r - j + 1] = da
}
int a = temp[l];
int b = temp[r];
for (i = l, j = r, k = l; k <= r; k++) {
if (a < b) {
da
a = temp;
} else {
da
b = temp[j];
}
}
}
private void insertSort(int[] da
for(int i=start+1;i<start+len;i++){
for(int j=i;(j>start) && da
SortUtil.swap(da
}
}
}
}
堆排序:
package org.rut.util.algorithm.support;
imp
public class HeapSort implements SortUtil.Sort{
public void sort(int[] da
MaxHeap h=new MaxHeap();
h.init(da
for(int i=0;i<da
h.remove();
System.arraycopy(h.queue,1,da
}
private static class MaxHeap{
void init(int[] da
this.queue=new int[da
for(int i=0;i<da
queue[++size]=da
fixUp(size);
}
}
private int size=0;
private int[] queue;
public int get() {
return queue[1];
}
public void remove() {
SortUtil.swap(queue,1,size--);
fixDown(1);
}
//fixdown
private void fixDown(int k) {
int j;
while ((j = k << 1) <= size) {
if (j < size && queue[j]<queue[j+1])
j++;
if (queue[k]>queue[j]) //不用交换
break;
SortUtil.swap(queue,j,k);
k = j;
}
}
private void fixUp(int k) {
while (k > 1) {
int j = k >> 1;
if (queue[j]>queue[k])
break;
SortUtil.swap(queue,j,k);
k = j;
}
}
}
}
SortUtil:
package org.rut.util.algorithm;
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class SortUtil {
public final static int INSERT = 1;
public final static int BUBBLE = 2;
public final static int SELECTION = 3;
public final static int SHELL = 4;
public final static int QUICK = 5;
public final static int IMPROVED_QUICK = 6;
public final static int MERGE = 7;
public final static int IMPROVED_MERGE = 8;
public final static int HEAP = 9;
public static void sort(int[] da
sort(da
}
private static String[] name={
"insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap"
};
private static Sort[] impl=new Sort[]{
new InsertSort(),
new BubbleSort(),
new SelectionSort(),
new ShellSort(),
new QuickSort(),
new ImprovedQuickSort(),
new MergeSort(),
new ImprovedMergeSort(),
new HeapSort()
};
public static String toString(int algorithm){
return name[algorithm-1];
}
public static void sort(int[] da
impl[algorithm-1].sort(da
}
public static interface Sort {
public void sort(int[] da
}
public static void swap(int[] da
int temp = da
da
da
}
}