用Java实现几种常见的排序算法及SortUtil

转自:http://www.gdglc.com/v2/pages/root/viewdetail.asp?id=303

用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等。

插入排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class InsertSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int temp;

 for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);

}

}

冒泡排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class BubbleSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int temp;

 for(int i=0;i for(int j=data.length-1;j>i;j--)

 {

  if(data[j] SortUtil.swap(data,j,j-1);

 }

}

}

选择排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class SelectionSort implements SortUtil.Sort

{

/*

* (non-Javadoc)

*

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int temp;

 for (int i = 0; i < data.length; i++)

 {

  int lowIndex = i;

  for (int j = data.length - 1; j >i; j--)

  {

   if (data[j] < data[lowIndex])

   {

    lowIndex = j;

   }

  }

  SortUtil.swap(data,i,lowIndex);

 }

}

}

Shell排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class ShellSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 for(int i=data.length/2;i>2;i/=2)

 {

  for(int j=0;j insertSort(data,j,i);

 }

}

insertSort(data,0,1);

}

/**

* @param data

* @param j

* @param i

*/

private void insertSort(int[] data, int start, int inc)

{

int temp;

for(int i=start+inc;i for(int j=i;(j>=inc)&&(data[j] SortUtil.swap(data,j,j-inc);

}

快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class QuickSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 quickSort(data,0,data.length-1);

}

private void quickSort(int[] data,int i,int j)

{

 int pivotIndex=(i+j)/2;

 //swap

 SortUtil.swap(data,pivotIndex,j);

 int k=partition(data,i-1,j,data[j]);

 SortUtil.swap(data,k,j);

 if((k-i)>1) quickSort(data,i,k-1);

 if((j-k)>1) quickSort(data,k+1,j);

}

/**

* @param data

* @param i

* @param j

* @return

*/

private int partition(int[] data, int l, int r,int pivot)

{

 do

 {

  while(data[++l] while((r!=0)&&data[--r]>pivot);

  SortUtil.swap(data,l,r);

 }

 while(l SortUtil.swap(data,l,r);

 return l;

}

}

改进后的快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class ImprovedQuickSort implements SortUtil.Sort

{

private static int MAX_STACK_SIZE=4096;

private static int THRESHOLD=10;

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int[] stack=new int[MAX_STACK_SIZE];

 int top=-1;

 int pivot;

 int pivotIndex,l,r;

 stack[++top]=0;

 stack[++top]=data.length-1;

 while(top>0)

 {

  int j=stack[top--];

  int i=stack[top--];

  pivotIndex=(i+j)/2;

  pivot=data[pivotIndex];

  SortUtil.swap(data,pivotIndex,j);

  //partition

  l=i-1;

  r=j;

  do

  {

   while(data[++l] while((r!=0)&&(data[--r]>pivot));

   SortUtil.swap(data,l,r);

  }

  while(l SortUtil.swap(data,l,r);

  SortUtil.swap(data,l,j);

  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(data);

 insertSort(data);

}

/**

* @param data

*/

private void insertSort(int[] data)

{

 int temp;

 for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);

}

}

归并排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class MergeSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int[] temp=new int[data.length];

 mergeSort(data,temp,0,data.length-1);

}

private void mergeSort(int[] data,int[] temp,int l,int r)

{

 int mid=(l+r)/2;

 if(l==r) return ;

 mergeSort(data,temp,l,mid);

 mergeSort(data,temp,mid+1,r);

 for(int i=l;i<=r;i++){

 temp[i]=data[i];

}

int i1=l;

int i2=mid+1;

for(int cur=l;cur<=r;cur++)

{

 if(i1==mid+1)

 data[cur]=temp[i2++];

 else if(i2>r)

 data[cur]=temp[i1++];

 else if(temp[i1] data[cur]=temp[i1++];

 else

 data[cur]=temp[i2++];

}

}

改进后的归并排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class ImprovedMergeSort implements SortUtil.Sort

{

private static final int THRESHOLD = 10;

/*

* (non-Javadoc)

*

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 int[] temp=new int[data.length];

 mergeSort(data,temp,0,data.length-1);

}

private void mergeSort(int[] data, int[] temp, int l, int r)

{

 int i, j, k;

 int mid = (l + r) / 2;

 if (l == r)

 return;

 if ((mid - l) >= THRESHOLD)

 mergeSort(data, temp, l, mid);

 else

 insertSort(data, l, mid - l + 1);

 if ((r - mid) >THRESHOLD)

 mergeSort(data, temp, mid + 1, r);

 else

 insertSort(data, mid + 1, r - mid);

 for (i = l; i <= mid; i++)

 {

  temp[i] = data[i];

 }

 for (j = 1; j <= r - mid; j++)

 {

  temp[r - j + 1] = data[j + mid];

 }

 int a = temp[l];

 int b = temp[r];

 for (i = l, j = r, k = l; k <= r; k++)

 {

  if (a < b)

  {

   data[k] = temp[i++];

   a = temp[i];

  }

  else

  {

   data[k] = temp[j--];

   b = temp[j];

  }

 }

}

/**

* @param data

* @param l

* @param i

*/

private void insertSort(int[] data, int start, int len)

{

 for(int i=start+1;i for(int j=i;(j>start) && data[j] SortUtil.swap(data,j,j-1);

}

}

堆排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class HeapSort implements SortUtil.Sort

{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data)

{

 MaxHeap h=new MaxHeap();

 h.init(data);

 for(int i=0;i h.remove();

 System.arraycopy(h.queue,1,data,0,data.length);

}

private static class MaxHeap

{

 void init(int[] data)

 {

  this.queue=new int[data.length+1];

  for(int i=0;i queue[++size]=data[i];

  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] 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;

import org.rut.util.algorithm.support.BubbleSort;

import org.rut.util.algorithm.support.HeapSort;

import org.rut.util.algorithm.support.ImprovedMergeSort;

import org.rut.util.algorithm.support.ImprovedQuickSort;

import org.rut.util.algorithm.support.InsertSort;

import org.rut.util.algorithm.support.MergeSort;

import org.rut.util.algorithm.support.QuickSort;

import org.rut.util.algorithm.support.SelectionSort;

import org.rut.util.algorithm.support.ShellSort;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

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[] data)

{

 sort(data, IMPROVED_QUICK);

}

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[] data, int algorithm)

{

 impl[algorithm-1].sort(data);

}

public static interface Sort

{

 public void sort(int[] data);

}

public static void swap(int[] data, int i, int j)

{

 int temp = data[i];

 data[i] = data[j];

 data[j] = temp;

}

}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: public class SortInt { public int[] sort(int[] nums) { int temp; for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] > nums[j]) { temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } return nums; } } ### 回答2: 这是一个用Java编写的类,其中包含一个sort()方法,用于对传入的整数进行排序并从小到大输出。完整代码如下: ```java import java.util.Arrays; public class SortClass { public static void sort(int[] nums) { Arrays.sort(nums); for (int num : nums) { System.out.print(num + " "); } } public static void main(String[] args) { int[] nums = {5, 3, 9, 1, 7}; sort(nums); } } ``` 在这个类中,我们首先导入了`java.util.Arrays`类,以便使用其中的排序方法。然后,我们定义了一个静态方法`sort()`,它接受一个整数数组作为参数。在方法中,我们使用`Arrays.sort()`方法对传入的整数数组进行排序。最后,我们使用增强型for循环遍历排序后的数组,并使用`System.out.print()`逐个输出每个排序后的整数。在`main()`方法中,我们创建一个整数数组`nums`并初始化其值。然后,我们调用`sort()`方法并将`nums`作为参数传递给它,实现对整数数组的排序和输出。 ### 回答3: 下面是一个使用Java编写的类,其中包含一个方法sort(),能够对传入的整数数组进行排序,并按照从小到大的顺序输出。 ```java public class SortUtil { public static void sort(int[] nums) { int n = nums.length; for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (nums[j] > nums[j+1]) { int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } System.out.println("排序结果为:"); for (int i = 0; i < n; i++) { System.out.print(nums[i] + " "); } System.out.println(); } public static void main(String[] args) { int[] nums = {6, 3, 9, 2, 1, 7, 4, 5, 8}; sort(nums); // 调用sort()方法对整数数组进行排序 } } ``` 以上代码中,使用冒泡排序的算法对传入的整数数组进行排序。sort()方法的参数是一个整数数组,在方法中使用两个嵌套的循环进行比较和交换操作,最终得到排序后的数组。在main()方法中创建一个示例的整数数组,并调用sort()方法进行排序。排序完成后,使用循环遍历并打印排序结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值