編程題--數組,請你編寫一個函數,返回該數組排序后


給定一個數組,請你編寫一個函數,返回該數組排序后的形式。

示例1

輸入:
[5,2,3,1,4]
返回值:
[1,2,3,4,5]

示例2

輸入:
[5,1,6,2,5]
返回值:
[1,2,5,5,6]

備注:

數組的長度不大於100000,數組中每個數的絕對值不超過10^9109

 

import java.util.*;


public class Solution {
/**
* 代碼中的類名、方法名、參數名已經指定,請勿修改,直接返回方法規定的值即可
* 將給定數組排序
* @param arr int整型一維數組 待排序的數組
* @return int整型一維數組
*/
public int[] MySort (int[] arr) {
// write code here
LinkedList<Integer> sortedList = new LinkedList<Integer>();
for(int i=0;i<arr.length;i++){
int t = arr[i];
if(sortedList.size()==0){
sortedList.add(t);
continue;
}else{
int m = sortedList.size();
for(int n=0;n<m;n++){
if(t>sortedList.get(n) && n == (m-1)){
sortedList.add(t);
}else if(t<=sortedList.get(n)){
sortedList.add(n,t);
break;
}
continue;
}
}

}
int[] result = new int[5];
for(int n=0;n<sortedList.size();n++){
result[n] = sortedList.get(n);
}

return result;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM