Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222441
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-03-15 09:23:44

2.4 A Java Quicksort

2011-03-11 Fri

  • One big difference from C or C++ is that in Java it is not possible to pass a comparison function to another function; there are no function pointers.
  • Instead we create an interface whose sole cotent is a function that compares two Objects. For each data type to be sorted, we then create a class with a member function that implements the interface for that data type. We pass an instance of that class to the sort function, which in turn uses the comparison function within the class to compare elements.

We begin by defining an interface named Cmp that declares a single member, a comparison function cmp that compares two Objects:

  1. interface Cmp {
  2.     int cmp(Ojbect x, Object y);
  3. }

Then we can write comparison functions that implement this interface; for example, this class defines a function that compares Integers:

  1. /* Icmp: Integer comparison */
  2. class Icmp implements Cmp {
  3.     public int cmp(Object o1, Object o2)
  4.     {
  5.         int i1 = ((Integer) o1).intValue();
  6.         int i2 = ((Integer) o2).intValue();
  7.         if (i1 < i2)
  8.             return -1;
  9.         else if (i1 == i2)
  10.             return 0;
  11.         else
  12.             return 1;
  13.     }
  14. }
and this compares Strings:
  1. /* Scmp: String comparison */
  2. class Scmp implements Cmp {
  3.     public int cmp(Object o1, Object o2)
  4.     {
  5.         String s1 = (String) o1;
  6.         String s2 = (String) o2;
  7.         return s1.compareTo(s2);
  8.     }
  9. }

We can sort only types that are derived from Object with this mechanism; it cannot be applied to the basic types like int or double.

With these components, we can now translate the C quicksort function into Java and have it call the comparison function from a Cmp object passed in as an argument. The most significant change is the use of indices left and right, since Java does not have pointers into arrays.

  1. /* Quicksort.sort: quicksort v[left]..v[right] */
  2. static void sort(Object[] v, int left, int right, Cmp cmp){
  3.     int i, last;
  4.     if (left >= right) //nothing to do
  5.         return;
  6.     swap(v, left, rand(left, right));//move pivot elem
  7.     last = left;
  8.     for (i = left+1; i <= right; i++) //artition
  9.         if (cmp.cmp(v[i], v[left]) < 0)
  10.             swap (v, ++last, i);
  11.     swap(v, left, last); //restore pivot elem
  12.     sort (v, left, last-1, cmp); //recursively sort
  13.     sort (v, last+1, right, cmp); //each part
  14. }

Quicksort.sort used cmp to comare a pair of objects, and calls swap as before to interchange them.

  1. /* Quicksort.swap: swap v[i] and v[j] */
  2. static void swap(Object[] v, int i, int j)
  3. {
  4.     Object temp;
  5.     temp = v[i];
  6.     v[i] = v[j];
  7.     v[j] = temp;
  8. }

Random number generation is done by a function that produces a random integer in the range left to right inclusive:

  1. static Random rgen = new Random();

  2. /* Quicksort.rand: return random integer in [left, right] */
  3. static int rand(int left, int right)
  4. {
  5.     return left + Math.abs(rgen.nextInt())%(right-left+1);
  6. }

We compute the absolute value, using Math.abs, because java's random number generator returns negative integers as well as positive.

The functions sort, swap, and rand, and the generator object rgen are the members of a class Quicksort.

Finally, to call Quicksort.sort to sort a String array, we would say

  1. public static void main(String[] argv)
  2. {

  3.     String[] sarr = new String[] {"mark", "tau", "atau", "haitao"};
  4.     Quicksort.sort(sarr, 0, sarr.length-1, new Scmp());
  5.         System.out.println("The sorted array is " );
  6.         for (String s : sarr)
  7.         System.out.println(s);
  8.     System.out.println("\n");
  9. }
阅读(1142) | 评论(0) | 转发(0) |
0

上一篇:tpop(2.3): Libraries

下一篇:tpop(2.5): O-Notation

给主人留下些什么吧!~~