单词冒泡排序算法

测试单词序列:cusmattribute5:applydate:hostserialno:productcode:clearbrno:banksystype:brno:tellerno:

terminalno:accbrno:bankdate:bankid:banktime:bankstatus:bankdealcode:bankdealmsg:

draccbrnolist:craccbrnolist:dracnolist:cracnolist:accamount:accamount1:accamount2:

accamount3:acctid:flowtype:workdate:agentserialno:tradestep:origapplydate:orighostserialno:

origbankdate:origbankid:crtdatetime:altdatetime:prduattribute1:prduattribute2:prduattribute3:

prduattribute4:prduattribute5:checkresult:cusmattribute1:cusmattribute2:cusmattribute3:

cusmattribute4:

排序后序列:

accamount1:accamount2:accamount3:accamount:accbrno:acctid:agentserialno:altdatetime:

applydate:bankdate:bankdealcode:bankdealmsg:bankid:bankstatus:banksystype:banktime:

brno:checkresult:clearbrno:craccbrnolist:cracnolist:crtdatetime:cusmattribute1:cusmattribute2:

cusmattribute3:cusmattribute4:cusmattribute5:draccbrnolist:dracnolist:flowtype:hostserialno:

origapplydate:origbankdate:origbankid:orighostserialno:prduattribute1:prduattribute2:

prduattribute3:prduattribute4:prduattribute5:productcode:tellerno:terminalno:tradestep:

workdate:

算法源码

 

private static String[] sort(String[] strarr) {
	for (int i = 0; i < strarr.length; i++) {
		for (int j = strarr.length - 1; j > i; j--) {
			exchange(strarr, j, 0, 'd');
		}
	}
	return strarr;
}

private static void exchange(String[] arr, int j, int level, char desc) {
	if (!arr[j].equals(arr[j - 1])) {
		String tmp;
		char cur = 0;
		char pre = 0;
		if (level < arr[j].length()) {
			cur = arr[j].charAt(level);
		} else {
			cur = arr[j].charAt(arr[j].length() - 1);
		}
		if (level < arr[j - 1].length()) {
			pre = arr[j - 1].charAt(level);
		} else {
			pre = arr[j - 1].charAt(arr[j - 1].length() - 1);
		}
		switch (desc) {
		case 'd':
			if (cur < pre) {
				tmp = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = tmp;
			} else if (cur == pre) {
				exchange(arr, j, ++level, desc);
			}
			break;
		case 'a':
			if (cur > pre) {
				tmp = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = tmp;
			} else if (cur == pre) {
				exchange(arr, j, ++level, desc);
			}
			break;
		}
	}
}
String[] s = sb.toString().split(":");
for (int j = 0; j < s.length; j++) {
	System.out.println(s[j]);
}
 

 

你可能感兴趣的:(java,数据结构,排序,冒泡)