java首次适应算法代码 java首次适应算法代码怎么写( 五 )


*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; jlen; i++, j += 4) {
output[j] = (byte) (input[i]0xffL);
output[j + 1] = (byte) ((input[i]8)0xffL);
output[j + 2] = (byte) ((input[i]16)0xffL);
output[j + 3] = (byte) ((input[i]24)0xffL);
}
}
/*
* Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
* 只合成低32bit,高32bit清零 , 以适应原始C实现的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; jlen; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1])8)
| (b2iu(input[j + 2])16) | (b2iu(input[j + 3])24);
return;
}
/*
* b2iu是我写的一个把byte按照不考虑正负号的原则的”升位”程序,因为java没有unsigned运算
*/
public static long b2iu(byte b) {
return b0 ? b0x7F + 128 : b;
}
/*
* byteHEX() , 用来把一个byte类型的数转换成十六进制的ASCII表示,
* 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib4)0X0F];
ob[1] = Digit[ib0X0F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
keyBean m = new keyBean();
if (Array.getLength(args) == 0) { // 如果没有参数,执行标准的Test Suite
System.out.println("keyBean Test suite:");
System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));
System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));
System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));
System.out.println("keyBean(\"message digest\"):"
+ m.getkeyBeanofStr("message digest"));
System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"
+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));
System.out
.println("keyBean(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"
+ m
.getkeyBeanofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));
}
}
求使用java实现的快排算法① 代码:
public class quicksortdemo {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i = j) {
while (array[i]pivot) {
i++;
}
while (array[j]pivot) {
j--;
}
if (i = j) {
swap(i, j);
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndexj)
quickSort(lowerIndex, j);
if (ihigherIndex)
quickSort(i, higherIndex);
}
private void swap(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
quicksortdemo sorter = new quicksortdemo();
int[] input = {24,2,45,20,56,75,2,56,99,53,12};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}
② 运行:
c:\java quicksortdemo
2 2 12 20 24 45 53 56 56 75 99

推荐阅读