public class SortCollections {
//待排序的数组
int []data={1,8,9,2,4,7,6,5,10,16,3,22};
//冒泡排序
public void Bubbling(int []array)
{
for(int i=0;i<array.length;i++)
{
for(int j=i+1;j<array.length;j++)
{
if(array[i]>array[j])swap(i,j,array);
}
}
}
//快速排序
public void QuickSort(int []array,int start,int end )
{
if(!(end>start)) return;
int i=start;
int j=end;
int key=array[start];
while(j>i)
{
while(j>i)
{
if(array[j]<key) break;
j--;
}
swap(i,j,array);
while(j>i)
{
if(array[i]>key) break;
i++;
}
swap(i,j,array);
}
QuickSort(array,start,i-1);
QuickSort(array,i+1,end);
}
//简单插入排序
public void SimpleInsert(int []array,int start)
{
int i,t;
if(start>=array.length) return;
t=array[start];
for( i=0;i<start;i++)
{
if(array[i]>array[start]) break;
}
for(int j=start;j>i;j--)
{
array[j]=array[j-1];
}
array[i]=t;
SimpleInsert(array, ++start);
}
//希尔排序
public void Shell(int []array,int interval)
{
if(interval==0) return;
int i=0;
while(i<interval)
{
int j=(array.length-1)/interval;
for(int k=1;k<j;k++)
{
int l=0;
int t=array[k*interval+i];
for(;l<k;l++)
{
if(array[l*interval+i]>t) break;
}
for(int m=k;m>l;m--)
{
array[m*interval+i]=array[(m-1)*interval+i];
}
array[l*interval+i]=t;
}
i++;
}
interval/=2;
Shell(array,interval);
}
//选择排序
public void Choose(int []array,int start)
{
if(start>=array.length) return;
int minindex=-1;
int minvalue=Integer.MAX_VALUE;
for(int i=start;i<array.length;i++)
{
if(array[i]<minvalue)
{
minvalue=array[i];
minindex=i;
}
}
swap(minindex,start,array);
Choose(array,++start);
}
//归并排序
//合并
public void merge(int []array,int start,int mid,int end,int []tempt)
{
int start2=mid+1;
int satart1=start;
int count=0;
while(start2<=end&&satart1<=mid)
{
if(array[satart1]<array[start2]) tempt[count++]=array[satart1++];
else tempt[count++]=array[start2++];
}
while(start2<=end)
{
tempt[count++]=array[start2++];
}
while(satart1<=mid)
{
tempt[count++]=array[satart1++];
}
for(int i=0;i<end-start+1;i++)
{
array[start+i]=tempt[i];
}
}
//归并排序
public void mergesort(int []array,int start,int end,int []temept)
{
if(end>start)
{
int mid=(start+end)/2;
mergesort(array,start,mid,temept);
mergesort(array,mid+1,end,temept);
merge(array,start,mid,end,temept);
}
} //堆排序
//构造最小堆
public void heapinit(int []array,int len)
{
if(len<=0) return;
for(int i=len;i>=0;i--)
{
int x=2*i+1;
int y=2*i+2;
if(x>array.length-1)
{
break;
}
else if(y>array.length-1)
{
if(array[x]<array[i]) swap(i,x,array);
}
else
{
int index=array[x]>array[y]? y:x;
if(array[index]<array[i]) swap(i,index,array);
}
}
heapinit(array,--len);
}
public void reheap(int []array,int end)
{
if(end<=0) return;
swap(0,end,array);
int i=0;
while(i<end)
{
int x=2*i+1;
int y=2*i+2;
if(!(x<end)) break;
else if(!(y<end))
{
if(array[x]<array[i]) {swap(i,x,array); i=x;}
else break;
}
else
{
int index=array[x]>array[y]? y:x;
if(array[index]<array[i]) {swap(i,index,array); i=index;}
else break;
}
}
reheap(array,--end);
}
//堆排序
public void heapsort(int []array)
{
heapinit(array,array.length-1);
reheap(array,array.length-1);
}
@Test
public void Test()
{
heapsort(data);
PrintResult();
}
//交换数组的两个元素
public void swap(int i,int j,int []array)
{
int t=array[i];
array[i]=array[j];
array[j]=t;
}
//打印结果
public void PrintResult()
{
for(int i=0;i<data.length;i++)
{
System.out.print(data[i]+" ");
}
}
}
剩下的有待完成。。。。