java 数组排序
/* ?
? ? *@description ? : ? 實現任意兩整數數組的按從小到大重組 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 先排序,再重組 ?
? ? *@Author ? ? ? ? ? ? : ? dalily ?
? ? *@Date ? ? ? ? ? ? ? ? : ? 2004-2-22 ?
? ? */ ?
? public ? class ? ArraySortClass{ ?
? ? ? ? ? ?
? //測試方法,先排序后重組 ?
? public ? static ? void ? main(String ? args[]){ ?
? ? ? ? ? ? ? ? ? ? ? int[] ? A ? ={12,8,6,15,10,17}; ?
? ? ? ? ? ? ? ? ? ? ? int[] ? B ? ={7,11,13,2,9,3}; ?
? ?
? ? ? //對數組排序 ?
? ? ? int[] ? A1 ? = ? bubbleSort(A); ?
? ? ? int[] ? B1 ? = ? bubbleSort(B); ?
? ?
? ? ? //對數組重組 ?
? ? ? int[] ? C ? = ? assembleArray(A,B); ?
? ? ? ? ? ? ? ? ? ? ? //輸出數組 ?
? ? ? ? ? ? ? ? ? ? ? for ? (int ? m=0;m<12;m++){ ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("the ? C ? is ? :" ? + ? C[m]); ?
? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ?
? ? ? ? ? } ?
? ?
? /* ?
? ? * ? 實現兩個有序整數數組按從小到大的順序重組 ?
? ? * ? ? ? ? ? ? 三個過程:1.按順序比較兩個數組中數據,只到其中一個已經沒有剩余數據 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2.如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3.如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? * ? @param ? A ? : ? the ? first ? Array ?
? ? * ? @param ? B ? : ? the ? second ? Array ?
? ? * ? @result ? ? : ? the ? assembled ? Array ?
? ? */ ?
? public ? static ? int[] ? assembleArray(int[] ? A, ? int[] ? B){ ?
? ? ? int ? iALength ? = ? A.length; ?
? ? ? ? ? ? ? int ? iBLength ? = ? B.length; ?
? ? ? int[] ? C ? = ? new ? int[iALength ? + ? iBLength]; ?
? ? ? int ? i=0; ?
? ? ? int ? j=0; ?
? ? ? int ? k=0; ?
? ? ? ?
? ? ? //按順序比較兩個數組中數據,只到其中一個已經沒有剩余數據 ?
? ? ? while ? (i<iALength ? && ? j ? <iBLength){ ? ? ?
? ? if ? (A[i] ? <=B[j]){ ?
? ? ? ? ? C[k] ? = ? A[i]; ?
? ? ? ? ? ? ? ? i++; ?
? ? } ? else{ ?
? ? ? ? ? C[k] ? = ? B[j]; ?
? ? ? ? ? j++; ?
? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? k++; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? } ? ?
? ?
? ? ? //如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? ? if ? (i<iALength){ ?
? ? for ? (int ? m=i;m<iALength;m++){ ?
? ? C[k] ? = ? A[m]; ?
? ? ? ? ? ? k++; ?
? ? } ?
? ? ? } ?
? ?
? ? ? //如果比較后的B數組還有剩余,則把B數組的其余數據補充到C數組 ?
? ? ? if ? (j<iBLength){ ?
? ? for ? (int ? n=j;n<iBLength;n++ ? ) ? { ?
? ? C[k] ? = ? B[n]; ?
? ? ? ? ? ? k++; ?
? ? } ?
? ? ? } ?
? ? ? return ? C; ?
? } ?
? ?
? /* ?
? ? * ? 采用冒泡排序實現整數數組排序 ?
? ? * ? @param ? A ? : ? the ? before ? Array ?
? ? * ? @param ? B ? : ? the ? sorted ? Array ?
? ? */ ?
? public ? static ? int[] ? ? bubbleSort(int ? A[]){ ?
? int ? iAlength ? = ? A.length; ?
? int ? i ? = ? 0; ?
? int ? k ? = ? 0 ? ; ?
? int ? temp ? = ? 0; ?
? while ? (i ? < ? iAlength){ ?
? ? ? for ? (k=i+1;k<iAlength;k++){ ?
? ? if ? (A[k]<A[i]){ ?
? ? temp ? = ? A[i]; ?
? ? A[i] ? = ? ? A[k]; ?
? ? A[k] ? = ? temp; ?
? ? } ?
? ? ? } ? ?
? ? ? i++; ?
? } ?
? return ? A; ?
? } ?
? }??
----------------------------------------------------------------------------------------------------------------------
如果使用List=ArrayList,則可以使用這樣的方法:
Collections.sort((List)LLID,new Comparator(){
???public int compare(Object one,Object two){
????int cc=(((class name)one).getKey().compareTo(((class name)two).getKey()));
???? return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
???}
??});
很方便的哦!
? ? *@description ? : ? 實現任意兩整數數組的按從小到大重組 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 先排序,再重組 ?
? ? *@Author ? ? ? ? ? ? : ? dalily ?
? ? *@Date ? ? ? ? ? ? ? ? : ? 2004-2-22 ?
? ? */ ?
? public ? class ? ArraySortClass{ ?
? ? ? ? ? ?
? //測試方法,先排序后重組 ?
? public ? static ? void ? main(String ? args[]){ ?
? ? ? ? ? ? ? ? ? ? ? int[] ? A ? ={12,8,6,15,10,17}; ?
? ? ? ? ? ? ? ? ? ? ? int[] ? B ? ={7,11,13,2,9,3}; ?
? ?
? ? ? //對數組排序 ?
? ? ? int[] ? A1 ? = ? bubbleSort(A); ?
? ? ? int[] ? B1 ? = ? bubbleSort(B); ?
? ?
? ? ? //對數組重組 ?
? ? ? int[] ? C ? = ? assembleArray(A,B); ?
? ? ? ? ? ? ? ? ? ? ? //輸出數組 ?
? ? ? ? ? ? ? ? ? ? ? for ? (int ? m=0;m<12;m++){ ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("the ? C ? is ? :" ? + ? C[m]); ?
? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ?
? ? ? ? ? } ?
? ?
? /* ?
? ? * ? 實現兩個有序整數數組按從小到大的順序重組 ?
? ? * ? ? ? ? ? ? 三個過程:1.按順序比較兩個數組中數據,只到其中一個已經沒有剩余數據 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2.如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? * ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3.如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? * ? @param ? A ? : ? the ? first ? Array ?
? ? * ? @param ? B ? : ? the ? second ? Array ?
? ? * ? @result ? ? : ? the ? assembled ? Array ?
? ? */ ?
? public ? static ? int[] ? assembleArray(int[] ? A, ? int[] ? B){ ?
? ? ? int ? iALength ? = ? A.length; ?
? ? ? ? ? ? ? int ? iBLength ? = ? B.length; ?
? ? ? int[] ? C ? = ? new ? int[iALength ? + ? iBLength]; ?
? ? ? int ? i=0; ?
? ? ? int ? j=0; ?
? ? ? int ? k=0; ?
? ? ? ?
? ? ? //按順序比較兩個數組中數據,只到其中一個已經沒有剩余數據 ?
? ? ? while ? (i<iALength ? && ? j ? <iBLength){ ? ? ?
? ? if ? (A[i] ? <=B[j]){ ?
? ? ? ? ? C[k] ? = ? A[i]; ?
? ? ? ? ? ? ? ? i++; ?
? ? } ? else{ ?
? ? ? ? ? C[k] ? = ? B[j]; ?
? ? ? ? ? j++; ?
? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? k++; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? } ? ?
? ?
? ? ? //如果比較后的A數組還有剩余,則把A數組的其余數據補充到C數組 ?
? ? ? if ? (i<iALength){ ?
? ? for ? (int ? m=i;m<iALength;m++){ ?
? ? C[k] ? = ? A[m]; ?
? ? ? ? ? ? k++; ?
? ? } ?
? ? ? } ?
? ?
? ? ? //如果比較后的B數組還有剩余,則把B數組的其余數據補充到C數組 ?
? ? ? if ? (j<iBLength){ ?
? ? for ? (int ? n=j;n<iBLength;n++ ? ) ? { ?
? ? C[k] ? = ? B[n]; ?
? ? ? ? ? ? k++; ?
? ? } ?
? ? ? } ?
? ? ? return ? C; ?
? } ?
? ?
? /* ?
? ? * ? 采用冒泡排序實現整數數組排序 ?
? ? * ? @param ? A ? : ? the ? before ? Array ?
? ? * ? @param ? B ? : ? the ? sorted ? Array ?
? ? */ ?
? public ? static ? int[] ? ? bubbleSort(int ? A[]){ ?
? int ? iAlength ? = ? A.length; ?
? int ? i ? = ? 0; ?
? int ? k ? = ? 0 ? ; ?
? int ? temp ? = ? 0; ?
? while ? (i ? < ? iAlength){ ?
? ? ? for ? (k=i+1;k<iAlength;k++){ ?
? ? if ? (A[k]<A[i]){ ?
? ? temp ? = ? A[i]; ?
? ? A[i] ? = ? ? A[k]; ?
? ? A[k] ? = ? temp; ?
? ? } ?
? ? ? } ? ?
? ? ? i++; ?
? } ?
? return ? A; ?
? } ?
? }??
----------------------------------------------------------------------------------------------------------------------
如果使用List=ArrayList,則可以使用這樣的方法:
Collections.sort((List)LLID,new Comparator(){
???public int compare(Object one,Object two){
????int cc=(((class name)one).getKey().compareTo(((class name)two).getKey()));
???? return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
???}
??});
很方便的哦!
總結
- 上一篇: 抽象工厂模式解析例子
- 下一篇: VS2005 宽字符 unicode字符