java算法:排序实现

java算法:排序实现

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

例1:数据项接口

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

例2:排序方法类

Java代码 复制代码
  1. classSort{
  2. staticbooleanless(Itemv,Itemw){
  3. returnv.less(w);
  4. }
  5. staticvoidexch(Item[]a,inti,intj){
  6. Itemt=a[i];
  7. a[i]=a[j];
  8. a[j]=t;
  9. }
  10. staticvoidcomExch(Item[]a,inti,intj){
  11. if(less(a[j],a[i])){
  12. exch(a,i,j);
  13. }
  14. voidsort(Item[]a,intl,intr){
  15. example(a,l,r);
  16. }
  17. staticvoidexample(Item[]a,intl,intr){
  18. for(inti=l;i<r;i++){
  19. for(intj=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. classMyItemimplementsItem{
  2. publicbooleanless(Item)
  3. voidread()
  4. voidrand()
  5. pubicStringtoString()
  6. }
class MyItem implements Item{
	public boolean less(Item)
	void read()
	void rand()
	pubic String toString()
}

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

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

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

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

Java代码 复制代码
  1. classArraySort{
  2. publicstaticvoidmain(String[]args){
  3. intN=100;
  4. SArraysa=newSArray(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. classMyArray{
  2. privateItem[]a;
  3. privateintN;
  4. MyArray(intN){
  5. this.N=N;
  6. a=newItem[N];
  7. for(inti=0;i<N;i++){
  8. a[i]=newItem();
  9. }
  10. }
  11. voidrand(){
  12. for(inti=0;i<N;i++){
  13. a[i].rand();
  14. }
  15. }
  16. voidread(){
  17. for(inti=0;i<N;i++){
  18. if(!In.empty()){
  19. a[i].read();
  20. }
  21. }
  22. }
  23. voidshow(intl,intr){
  24. for(inti=l;i<r;i++){
  25. System.out.println(a[i]+"");
  26. }
  27. }
  28. voidsort(intl,intr){
  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. classMyItemimplementsItem{
  2. privateintkey;
  3. publicbooleanless(Itemv){
  4. returnkey<((Item)v).key;
  5. }
  6. voidread(){
  7. key=In.getInt();
  8. }
  9. voidrand(){
  10. key=(int)(1000*Math.random());
  11. }
  12. publicStringtoString(){
  13. returnkey+"";
  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. classRecord{
  2. intid;
  3. doublebalance;
  4. Stringwho;
  5. staticintSortKeyField=0;
  6. publicStringtoString(){
  7. returnid+""+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. classMyItemextendsRecordimplementsItem{
  2. publicbooleanless(Itemv){
  3. MyItemr=(MyItem)v;
  4. switch(SortKeyField){
  5. case2:returnwho.compareTo(r.who)<0;
  6. case1:returnbalance<r.balance;
  7. default:returnid<r.id;
  8. }
  9. }
  10. voidread(){
  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. classMyItemimplementsItem{
  2. Stringkey;
  3. publicboolean(Itemv){
  4. returnkey.compareTo(((MyItem)v).key)<0;
  5. }
  6. voidread(){
  7. key=In.getString();
  8. }
  9. voidrand(){
  10. inta=(int)('a');
  11. key="";
  12. for(inti=0;i<1+9*Math.random();i++){
  13. key+=(char)(a+26*Math.random();
  14. }
  15. }
  16. publicStringtoString(){
  17. returnkey;
  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方法有很多好处。在排序情况下,使用引用避免对排序的数组直接进行操作。即时是只读文件,也能对它进行“排序”。而且,通过使用多重引用数组,能对一个数据体用两种不同的排序表示法来表示。这种不改变数据就能对数据进行操作的灵活性在许多应用中都十分有用的,同时,使用引用能节省移动整个记录的开销。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值