java 实现八大排序算法
public class DiffSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {3,1,5,7,2,4,9,6,10,8,23,25,13,17,15,68,37,46,30,23,9,6,323,45,67,78,2,4,56,7,7,8,5,23,4,5,6,7,34,2,2,2,34,45};
System.out.println("排序前初始值");
DiffSort ds = new DiffSort();
ds.print(a);
// ds.insertSort(a);
// ds.shellSort(a);
ds.QuickSort(a, 0, a.length-1);
// ds.BubbleSort(a);
// ds.SelectSort(a);
// ds.HeapSort(a);
// int[] temp = new int[a.length];
// ds.mergeSort(a, 0, a.length-1, temp);
System.out.println("\n排序后结果:");
ds.print(a);
}
void print(int a[]){
for(int j= 0; j<a.length; j++){
System.out.print(a[j] + " ");
}
}
// 插入排序
private void insertSort(int[] a){
for(int i=1;i<a.length;i++){
int temp = a[i];
int j;
for(j=i-1;j>=0&&a[j]>temp;j--)
a[j+1] = a[j];
a[j+1] = temp;
}
}
// 希尔排序
private void shellSort(int[] a){
int dif=a.length;
while(true)
{
dif=dif/2;
for(int x=0;x<dif;x++) //分别对dif个组进行插入排序
{
for(int i=x+dif;i<a.length;i=i+dif)
{
int temp=a[i];
int j;
for(j=i-dif;j>=0&&a[j]>temp;j=j-dif)
{
a[j+dif]=a[j];
}
a[j+dif]=temp;
}
}
if(dif==1)
{
break;
}
}
}
// 冒泡排序
private void BubbleSort(int[] a) {
// TODO Auto-generated method stub
int length = a.length;
for(int i=0;i<length-1;i++){
for(int j=length-1;j>i;j--){
if(a[j]<a[j-1]){
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}
// 快速排序
private void QuickSort(int[] a, int begin,int end){
if(a!=null && begin<end){
int mid = getMid(a,begin,end);
if (begin<mid)
QuickSort(a, begin, mid);
if(mid+1<end)
QuickSort(a, mid+1, end);
}
}
private int getMid(int[] a,int begin,int end){
int low = begin;
int high = end;
int temp = a[low];
while(low<high){
while(low<high && a[high]>=temp){
high--;
}
if(low<high){
a[low] = a[high];
}
while(low<high && a[low]<=temp){
low++;
}
if(low<high){
a[high] = a[low];
}
}
a[low] = temp;
return low;
}
// 选择排序
private void SelectSort(int[] a){
int length = a.length;
for(int i=0;i<length-1;i++){
int temp = a[i];
int min = i;
for(int j=i;j<length;j++){
if(a[j ]<a[min]){
min = j;
}
}
if(i!=min){
a[i] = a[min];
a[min] = temp;
}
}
}
// 堆排序
// 重新调整堆
void HeapAdjust(int[] a,int s, int length)
{
int tmp = a[s];
int child = 2*s+1;
while (child < length) {
if(child+1 <length && a[child]<a[child+1]) {
++child ;
}
if(a[s]<a[child]) {
a[s] = a[child];
s = child;
child = 2*s+1;
} else {
break;
}
a[s] = tmp;
}
}
// 建立堆
void BuildingHeap(int[] a, int length)
{
for (int i = (length -1) / 2 ; i >= 0; --i)
HeapAdjust(a,i,length);
}
// 堆排主程序
void HeapSort(int[] a)
{
int length = a.length;
BuildingHeap(a, length);
for (int i = length - 1; i > 0; --i)
{
//交换堆顶元素H[0]和堆中最后一个元素
int temp = a[i]; a[i] = a[0]; a[0] = temp;
HeapAdjust(a,0,i);
}
}
// 归并排序
void mergeSort(int[] a,int begin, int end,int[] temp){
if(begin<end){
int mid = (begin+end)/2;
mergeSort(a,begin,mid,temp);
mergeSort(a,mid+1,end,temp);
merge(a,begin,mid,end,temp);
}
}
private void merge(int[] a, int begin,int mid, int end,int[] temp) {
// TODO Auto-generated method stub
int i=begin;
int j=mid+1,k=0;
while(i<=mid && j<=end){
if(a[i]<=a[j]){
temp[k]=a[i];
i++;k++;
}
else{
temp[k]=a[j];
j++;k++;
}
}
while(i<=mid){
temp[k] = a[i];
i++;
k++;
}
while(j<=end){
temp[k] = a[j];
j++;
k++;
}
for(i=0;i<k;i++){
a[begin+i] = temp[i];
}
}
// 桶排序
private void bucketSort(int[] a){
}
}
posted on 2018-04-17 15:14 zhangsan12 阅读(160) 评论(0) 收藏 举报
浙公网安备 33010602011771号