java算法:排序实现

java算法:排序实现

排序最基本规则:先比较,再对数据项进行排序。

例1:数据项接口

Java代码 复制代码
  1. interface Item{   
  2.     boolean less(Item v);   
  3. }  
interface Item{
	boolean less(Item v);
}

例2:排序方法类

Java代码 复制代码
  1. class Sort{   
  2.     static boolean less(Item v, Item w){   
  3.         return v.less(w);   
  4.     }   
  5.     static void exch(Item [] a, int i, int j){   
  6.         Item t = a[i];   
  7.         a[i] = a[j];   
  8.         a[j] = t;   
  9.     }   
  10.     static void comExch(Item [] a, int i, int j){   
  11.         if(less(a[j], a[i])){   
  12.             exch(a, i, j);   
  13.     }   
  14.     void sort(Item [] a, int l, int r){   
  15.         example(a, l, r);   
  16.     }   
  17.     static void example(Item [] a, int l, int r){   
  18.         for(int i = l; i < r; i++){   
  19.             for(int j = i; j > l; j--){   
  20.                 compExch(a, j - 1, j);   
  21.             }   
  22.         }   
  23.     }   
  24. }  
class Sort{
	static boolean less(Item v, Item w){
		return v.less(w);
	}
	static void exch(Item [] a, int i, int j){
		Item t = a[i];
		a[i] = a[j];
		a[j] = t;
	}
	static void comExch(Item [] a, int i, int j){
		if(less(a[j], a[i])){
			exch(a, i, j);
	}
	void sort(Item [] a, int l, int r){
		example(a, l, r);
	}
	static void example(Item [] a, int l, int r){
		for(int i = l; i < r; i++){
			for(int j = i; j > l; j--){
				compExch(a, j - 1, j);
			}
		}
	}
}

数据项ADT的接口:数据项,或要排序的一般对象;数据项数组。

例3:数据项ADT接口

Java代码 复制代码
  1. class MyItem implements Item{   
  2.     public boolean less(Item)   
  3.     void read()   
  4.     void rand()   
  5.     pubic String toString()   
  6. }  
class MyItem implements Item{
	public boolean less(Item)
	void read()
	void rand()
	pubic String toString()
}

排序算法不仅对数据项,也对数据项数组起作用。

例4:可排序的数组ADT接口

Java代码 复制代码
  1. class SArray{   
  2.     SArray(int)   
  3.     void rand()   
  4.     void read()   
  5.     void show(intint)   
  6.     void sort(intint)   
  7. }  
class SArray{
	SArray(int)
	void rand()
	void read()
	void show(int, int)
	void sort(int, int)
}

例5:可排序数组的排序驱动程序

Java代码 复制代码
  1. class ArraySort{   
  2.     public static void main(String [] args){   
  3.         int N = 100;   
  4.         SArray sa = new SArray(100);   
  5.         sa.rand();   
  6.         sa.sort(0, N - 1);   
  7.         sa.show(0, N - 1);   
  8.     }   
  9. }  
class ArraySort{
	public static void main(String [] args){
		int N = 100;
		SArray sa = new SArray(100);
		sa.rand();
		sa.sort(0, N - 1);
		sa.show(0, N - 1);
	}
}

该程序表明:可以定义一个这样的计算而不需要涉及要排序的数据类型。

模块化的组织方式允许用别的实现来代替,这取决于该应用。如,当对大型数组测试排序时,可能会使用只输出部分数组的show的实现。

例6:可排序数组ADT的样本实现

Java代码 复制代码
  1. class MyArray{   
  2.     private Item[] a;   
  3.     private int N;   
  4.     MyArray(int N){   
  5.         this.N = N;   
  6.         a = new Item[N];   
  7.         for(int i = 0; i < N; i++){   
  8.             a[i] = new Item();   
  9.         }   
  10.     }   
  11.     void rand(){   
  12.         for(int i = 0; i < N; i++){   
  13.             a[i].rand();   
  14.         }   
  15.     }   
  16.     void read(){   
  17.         for(int i = 0; i < N; i++){   
  18.             if(!In.empty()){   
  19.                 a[i].read();   
  20.             }   
  21.         }   
  22.     }   
  23.     void show(int l, int r){   
  24.         for(int  i = l; i < r; i++){   
  25.             System.out.println(a[i] + " ");   
  26.         }   
  27.     }   
  28.     void sort(int l, int r){   
  29.         Sort.sort(a, l, r);   
  30.     }   
  31. }  
class MyArray{
	private Item[] a;
	private int N;
	MyArray(int N){
		this.N = N;
		a = new Item[N];
		for(int i = 0; i < N; i++){
			a[i] = new Item();
		}
	}
	void rand(){
		for(int i = 0; i < N; i++){
			a[i].rand();
		}
	}
	void read(){
		for(int i = 0; i < N; i++){
			if(!In.empty()){
				a[i].read();
			}
		}
	}
	void show(int l, int r){
		for(int  i = l; i < r; i++){
			System.out.println(a[i] + " ");
		}
	}
	void sort(int l, int r){
		Sort.sort(a, l, r);
	}
}

对不同类型数据项进行排序的ADT实现。

例7:整数数据项的ADT实现

Java代码 复制代码
  1. class MyItem implements Item{   
  2.     private int key;   
  3.     public boolean less(Item v){   
  4.         return key < ((Item)v).key;   
  5.     }   
  6.     void read(){   
  7.         key = In.getInt();   
  8.     }   
  9.     void rand(){   
  10.         key = (int)(1000 * Math.random());   
  11.     }   
  12.     public String toString(){   
  13.         return key + "";   
  14.     }   
  15. }  
class MyItem implements Item{
	private int key;
	public boolean less(Item v){
		return key < ((Item)v).key;
	}
	void read(){
		key = In.getInt();
	}
	void rand(){
		key = (int)(1000 * Math.random());
	}
	public String toString(){
		return key + "";
	}
}

例8:样本记录类

Java代码 复制代码
  1. class Record{   
  2.     int id;   
  3.     double balance;   
  4.     String who;   
  5.     static int SortKeyField = 0;   
  6.     public String toString(){   
  7.         return id + " " + balance + " " + who;   
  8.     }   
  9. }  
class Record{
	int id;
	double balance;
	String who;
	static int SortKeyField = 0;
	public String toString(){
		return id + " " + balance + " " + who;
	}
}

例9:记录项的ADT实现

Java代码 复制代码
  1. class MyItem extends Record implements Item{   
  2.     public boolean less(Item v){   
  3.         MyItem r = (MyItem) v;   
  4.         switch(SortKeyField){   
  5.             case 2return who.compareTo(r.who) < 0;   
  6.             case 1return balance < r.balance;   
  7.             defaultreturn id < r.id;   
  8.         }   
  9.     }   
  10.     void read(){   
  11.         id = In.getInt();   
  12.         balance = In.getDouble();   
  13.         who = In.getString();   
  14.     }   
  15. }  
class MyItem extends Record implements Item{
	public boolean less(Item v){
		MyItem r = (MyItem) v;
		switch(SortKeyField){
			case 2: return who.compareTo(r.who) < 0;
			case 1: return balance < r.balance;
			default: return id < r.id;
		}
	}
	void read(){
		id = In.getInt();
		balance = In.getDouble();
		who = In.getString();
	}
}

上述方法在经典文献中被称为指针排序。

例10:字符串数据项的ADT实现

Java代码 复制代码
  1. class MyItem implements Item{   
  2.     String key;   
  3.     public boolean(Item v){   
  4.         return key.compareTo(((MyItem)v).key) < 0;   
  5.     }   
  6.     void read(){   
  7.         key = In.getString();   
  8.     }   
  9.     void rand(){   
  10.         int a = (int)('a');   
  11.         key = "";   
  12.         for(int i = 0; i < 1 + 9*Math.random(); i++){   
  13.             key += (char)(a + 26*Math.random();   
  14.         }   
  15.     }   
  16.     public String toString(){   
  17.         return key;   
  18.     }   
  19. }  
class MyItem implements Item{
	String key;
	public boolean(Item v){
		return key.compareTo(((MyItem)v).key) < 0;
	}
	void read(){
		key = In.getString();
	}
	void rand(){
		int a = (int)('a');
		key = "";
		for(int i = 0; i < 1 + 9*Math.random(); i++){
			key += (char)(a + 26*Math.random();
		}
	}
	public String toString(){
		return key;
	}
}

说明了在java中对关键字使用String域直接实现String对象的MyItemADT。

java方法有很多好处。在排序情况下,使用引用避免对排序的数组直接进行操作。即时是只读文件,也能对它进行“排序”。而且,通过使用多重引用数组,能对一个数据体用两种不同的排序表示法来表示。这种不改变数据就能对数据进行操作的灵活性在许多应用中都十分有用的,同时,使用引用能节省移动整个记录的开销。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值