对应Entity写的算法

Entity的配套排序,有直接插入排序,快速排序,冒泡排序,希尔排序,好像没别的排序了。
package com.util;

public class Algorithm {
	//直接插入
	public static Entity[] injectionSort(Entity[] entities) {
		int j;
		int total = entities.length;
		Entity temp;
		for(int i = 1; i<total; i++) {
			for(j = 0; j<i; j++) {
				if(entities[i].compareTo(entities[j]) < 0) {
					temp = entities[j];
					entities[j] = entities[i];
					entities[i] = temp;
				}
			}
		}
		return entities;
	}
	//冒泡
	public static Entity[] bubbleSort(Entity[] entities) {
		int j;
		int total = entities.length;
		Entity temp;
		for(int i = 1; i < total; i++) {
			for( j = total - 1; j >= i ;j--) {
				if(entities[j].compareTo(entities[j-1]) < 0 ) {
					temp = entities[j-1];
					entities[j-1] = entities[j];
					entities[j] = temp;
				}
			}
		}
		
		return entities;
	}
	//快速排序法
	public static Entity[] quickSort(Entity[] entities) {
		Entity temp;
		int total = entities.length;
		if(total == 1) {
			return entities;
		}
		int low = 0;
		int high = total - 1 ;
		int index = low;
		while(low < high) {
			//寻找右面比要比较值大的数
			while(entities[index].compareTo(entities[high]) < 0) {
				high--;
			}
			if(entities[index].compareTo(entities[high]) > 0) {
				temp = entities[index];
				entities[index] = entities[high];
				entities[high] = temp;
				index = high;
			}
			//寻找左面比要比较值小的数
			while(entities[index].compareTo(entities[low]) > 0) {
				low++;
			}
			if(entities[index].compareTo(entities[low]) < 0) {
				temp = entities[index];
				entities[index] = entities[low];
				entities[low] = temp;
				index = low;
			}
		}
		int i;
		if(index == total-1) {
			Entity[] leftEntities = new Entity[total-1];
			for(i = 0; i<total-1; i++) {
				leftEntities[i] = entities[i];
			}
			leftEntities = quickSort(leftEntities);
			
			for(i=0; i<leftEntities.length; i++) {
				entities[i] = leftEntities[i];
			}
			return entities;
		}else if(index == 0) {
			Entity[] rightEntities = new Entity[total-1];
			for(i = 1; i<total; i++) {
				rightEntities[i-1] = entities[i];
			}
			rightEntities = quickSort(rightEntities);
			for(i=0; i<rightEntities.length; i++) {
				entities[index+i+1] = rightEntities[i];
			}
			return entities;
		} else {
			Entity[] leftEntities = new Entity[index];
			i = 0;
			for(; i<index; i++) {
				leftEntities[i] = entities[i];
			}
			Entity[] rightEntities = new Entity[total-index-1];
			for(i = index+1; i<total; i++) {
				rightEntities[i-index-1] = entities[i];
			}
			
			leftEntities = quickSort(leftEntities);
			rightEntities = quickSort(rightEntities);
			
			for(i=0; i<leftEntities.length; i++) {
				entities[i] = leftEntities[i];
			}
			for(i=0; i<rightEntities.length; i++) {
				entities[index+i+1] = rightEntities[i];
			}
			return entities;
		}	
	}
	//shell排序
	public static Entity[] shellSort(Entity[] entities, int d) {
		int total = entities.length; 
		//d为每次的增量
		d = (d+1)/2;
		if(d<3) {
			entities = injectionSort(entities);
			return entities;
		}
		//m为每个小组中的最多元素数量
		int m ;
		if(total%d == 0)m  = total/d;
		else m = total/d+1;
		Entity[] temp = null;
		for(int j = 0; j<d; j++) {
			//n为每个小组中的实际元素数量
			int n = m;
			if((n-1)*d+j >= total)
				n--;
			temp = new Entity[n];
			for(int i = 0; i<n; i++) {
					temp[i] = entities[d*i+j];
			}
			temp = injectionSort(temp);
			for(int i = 0; i<n; i++) {
					entities[d*i+j] = temp[i];
			}
			temp = null;
		}
		entities = shellSort(entities,d);
		return entities;
	}
}

书上怎么教的我就怎么写的,只要是人话,翻译机器语言还是可以的,虽然用了快一天的时间,不过收获不小,不过好像快速排序、希尔排序和书上的差好多,它没我代码那么多,忘了说了,英文丑的可以,知道标准的留言转告下,高手勿喷啊。下面是测试代码
package com.util;


import java.util.Date;


public class TestAlgorithm {

	@Test
	public void testInjectionSort() {
		Entity e1 = new Entity.Builder(4).setC('b').build(); 
		Entity e2 = new Entity.Builder(1).setC('a').build(); 
		Entity e3 = new Entity.Builder(1).setC('c').build(); 
		Entity e4 = new Entity.Builder(3).setC('a').build(); 
		Entity[] entities = {e1,e2,e3,e4};
		for(Entity e : entities ) {
			System.out.println(e);
		}
		entities = Algorithm.injectionSort(entities);	
		System.out.println();
		for(Entity e : entities ) {
			System.out.println(e);
		}
	}
	
	@Test
	public void testBubbleSort() {
		Entity e1 = new Entity.Builder(4).setC('b').build(); 
		Entity e2 = new Entity.Builder(1).setC('a').build(); 
		Entity e3 = new Entity.Builder(1).setC('c').build(); 
		Entity e4 = new Entity.Builder(3).setC('a').build(); 
		Entity[] entities = {e1,e2,e3,e4};
		for(Entity e : entities ) {
			System.out.println(e);
		}
		entities = Algorithm.bubbleSort(entities);	
		System.out.println();
		for(Entity e : entities ) {
			System.out.println(e);
		}
	}
	
	@Test
	public void testQuickSort() {
		Entity e1 = new Entity.Builder(49).setC('b').build(); 
		Entity e2 = new Entity.Builder(38).setC('a').build(); 
		Entity e3 = new Entity.Builder(65).setC('c').build(); 
		Entity e4 = new Entity.Builder(97).setC('a').build(); 
		Entity e5 = new Entity.Builder(76).build();
		Entity e6 = new Entity.Builder(13).build();
		Entity e7 = new Entity.Builder(27).build();
		Entity e8 = new Entity.Builder(49).build();
		Entity[] entities = {e1,e2,e3,e4,e5,e6,e7,e8};
		for(Entity e : entities ) {
			System.out.println(e);
		}
		entities = Algorithm.quickSort(entities);	
		System.out.println();
		for(Entity e : entities ) {
			System.out.println(e);
		}
	}
	@Test
	public void testShellSort() {
		Entity e1 = new Entity.Builder(49).build(); 
		Entity e2 = new Entity.Builder(38).setC('a').build(); 
		Entity e3 = new Entity.Builder(65).setC('c').build(); 
		Entity e4 = new Entity.Builder(97).setC('a').build(); 
		Entity e5 = new Entity.Builder(76).build();
		Entity e6 = new Entity.Builder(13).build();
		Entity e7 = new Entity.Builder(27).build();
		Entity e8 = new Entity.Builder(49).build();
		Entity e9 = new Entity.Builder(55).build();
		Entity e10 = new Entity.Builder(4).build();
		Entity[] entities = {e1,e2,e3,e4,e5,e6,e7,e8,e9,e10};
		for(Entity e : entities ) {
			System.out.println(e);
		}
		entities = Algorithm.shellSort(entities,entities.length);	
		System.out.println();
		for(Entity e : entities ) {
			System.out.println(e);
		}
	}
}

你可能感兴趣的:(C++,c,算法,JUnit,J#)