当前位置: 代码迷 >> J2ME >> j2me 排序步骤 整数数组
  详细解决方案

j2me 排序步骤 整数数组

热度:9497   发布时间:2013-02-25 21:31:33
j2me 排序方法 整数数组
package xxx.yyy.zzz.tst;import java.util.Random;public class MySortMethodTst {	/**	 * @param args	 */	public static void main(String[] args) {		int[] array = new int[100];		Random random = new Random();		/* load some random values into the array */		for (int i = 0; i < 100; i++) {			array[i] = random.nextInt() % 100;		}		/* print the original array */		System.out.println("===***=== Before sorting: ");		for (int i = 0; i < array.length; i++) {			System.out.printf(" %d ", array[i]);		}		System.out.println();		// use mySortMethod		mySortMethod(array, 0, array.length - 1);		System.out.println("===***=== After sorting: ");		for (int i = 0; i < array.length; i++) {			System.out.printf(" %d ", array[i]);		}		System.out.println();	}	/* sort everything in between lowIndex and highIndex */	private static void mySortMethod(int[] arrayToSort, int lowIndex,			int highIndex) {		int i = lowIndex;		int j = highIndex;		int y = 0;		/* compare value */		int z = arrayToSort[(lowIndex + highIndex) / 2];		/* partition */		do {			/* find member above ... */			while (arrayToSort[i] < z)				i++;			/* find element below ... */			while (arrayToSort[j] > z)				j--;			if (i <= j) {				/* swap two elements */				y = arrayToSort[i];				arrayToSort[i] = arrayToSort[j];				arrayToSort[j] = y;				i++;				j--;			}		} while (i <= j);		/* recurse */		if (lowIndex < j)			mySortMethod(arrayToSort, lowIndex, j);		if (i < highIndex)			mySortMethod(arrayToSort, i, highIndex);	}}


j2me的api提供的方法非常有限,很多平时经常用到的方法都没有,统统要自己重新写,就懒得每次想方法了,写好了复制复制吧.
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!