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提供的方法非常有限,很多平时经常用到的方法都没有,统统要自己重新写,就懒得每次想方法了,写好了复制复制吧.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值