基数ソートアルゴリズム実装(Java版)


import java.util.ArrayList;

/**
 * @function:        
 * */
public class RadixSort {

	//         ,         
	private ArrayList[] tempList = new ArrayList[10];

	/**
	 * @function:    ,     RadixSort 
	 * */
	public RadixSort() {
		for (int i = 0; i < this.tempList.length; i++) {
			this.tempList[i] = new ArrayList();
		}
	}

	//     
	private String[] Collection(int length) {
		String[] a = new String[length];
		int k = 0;
		for (int i = 0; i < this.tempList.length; i++) {
			for (int j = 0; j < this.tempList[i].size(); j++) {
				a[k++] = (String) this.tempList[i].get(j);
			}
			this.tempList[i] = new ArrayList();
		}
		return a;
	}

	/**
	 * @function:    
	 * @param: unsort         
	 * @param: index         
	 * */
	private void Distribution(String[] unsort, int index) {
		for (int i = 0; i < unsort.length; i++) {
			int j = Integer.parseInt(unsort[i].substring(index, index + 1));
			tempList[j].add(unsort[i]);
		}
	}

	/**
	 * @function:      
	 * */
	public String[] radix(String[] a) {
		for (int i = 2; i >= 0; i--) {
			this.Distribution(a, i);
			a = this.Collection(a.length);
		}
		return a;
	}

	/**
	 * @function:    
	 * */
	private void print(String[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		RadixSort r = new RadixSort();
		String[] a = new String[] { "278", "109", "063", "930", "589", "333", "679" };
		//     
		r.print(a);
		//     
		a = r.radix(a);
		//     
		r.print(a);
	}
}