[英]Java sorting array positive ascending to negative ascending
我無法解決這個問題,我需要從數組 A 輸出像 {1,2,3,4,-1,-2,-3,-4} 來自數組中的隨機數,然后將其寫入另一個數組 B . 到目前為止,我的實驗代碼不能像我那樣工作
public static void main(String[] args) {
int a[] = {5,4,3,2,1,-3,-2,-30};
int length = a.length - 1;
for (int i = 0 ; i < length ; i++) {
for (int j = 0 ; j < length-i ; j++) {
if (a[j] < a[j+1]) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}
for (int x : a) {
System.out.print(x+" ");
}
}
輸出是 5 4 3 2 1 -2 -3 -30 ,但我需要 1,2,3,4,5,-2,-3,-30
更新:
public static void main(String[] args) {
int a[] = {5,4,3,2,1,-3,-2,-30,-1,-15,8};
int length = a.length - 1;
for (int i = 0 ; i < length ; i++) {
for (int j = 0 ; j < length-i ; j++) {
if (a[j] < a[j+1]) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
} else {
if (a[j] > a[j+1] && a[j+1] > 0) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
}
}
for (int x : a) {
System.out.print(x+" ");
}
}
我離我的目標更近了,但是 8 1 2 3 4 5 -1 -2 -3 -15 -30 ,這個數字 8 毀了這一切
如果我理解正確,你想在兩件事之后排序。 正數從低到高,負數從高到低。
您可以先從高到低排序,然后在第二次運行數組時跳過所有正數,然后從高到低排序。
這有幫助嗎? 我可以寫一些代碼,但我相信這是你現在想要學習的東西:)
添加一個 if-else 來區分正負情況。
if (a[j] < 0) {
if (a[j] < a[j+1]) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
} else {
if (a[j] > a[j+1] && a[j+1] > 0) {
int swap = a[j];
a[j] = a[j+1];
a[j+1] = swap;
}
}
算法:
遍歷數組並將正數存儲在一個中,將負數存儲在另一個中。 O(i)
按升序對正數數組進行排序。 O(mLog(m))
對負數進行降序排序。 O(nLog(n))
創建輸入大小的最終數組。
添加所有正數組排序值。 然后添加負數組排序值。 O(i)
總計:O(i) + O(mLog(m)) + O(nLog(n)) + O(i) = O(mLog(m)) 如果 m > n
我在這里使用了庫函數。 但是,如果您願意,您可以使用相同的想法編寫函數。
public class PostivieAsendingNegativeDesending implements Comparator<Integer> {
public static void main(String args[]) {
int fullList[] = {5, 4, 3, 2, 1, -3, -2, -30};
ArrayList<Integer> subList = new ArrayList<>();
ArrayList<Integer> subList2 = new ArrayList<>();
for (int i = 0; i < fullList.length; i++) {
if (fullList[i] < 0) {
subList2.add((fullList[i]));
} else {
subList.add(fullList[i]);
}
}
Collections.sort(subList);
Collections.sort(subList2, new PostivieAsendingNegativeDesending());
subList.addAll(subList2);
for (int i = 0; i < subList.size(); i++) {
System.out.print(subList.get(i) + " ");
}
System.out.println("");
}
@Override
public int compare(Integer n1, Integer n2) {
return n2 - n1;
}
}
這將完成僅使用基本循環的技巧
public static void main(String[] args) {
int a[] = { 5, 4, 3, 2, 1, -3, -2, -30 };
int length = a.length - 1;
int pos = 0, neg = 0;
// find total count of positive and negative numbers
for (int i = 0; i <= length; i++) {
if (a[i] < 0)
neg++;
else
pos++;
}
// initialize the arrays based on 'pos' and 'neg'
int posArr[] = new int[pos];
int negArr[] = new int[neg];
// store pos and neg values in the arrays
int countPos = 0, countNeg = 0;
for (int i = 0; i <= length; i++) {
if (a[i] < 0) {
negArr[countNeg] = a[i];
countNeg++;
} else {
posArr[countPos] = a[i];
countPos++;
}
}
// sort positive numbers
for (int i = 0; i < posArr.length - 1; i++) {
for (int j = 0; j < posArr.length - 1 - i; j++) {
if (posArr[j] > posArr[j + 1]) {
int swap = posArr[j];
posArr[j] = posArr[j + 1];
posArr[j + 1] = swap;
}
}
}
// sort negative numbers
for (int i = 0; i < negArr.length - 1; i++) {
for (int j = 0; j < negArr.length - 1 - i; j++) {
if (negArr[j] < negArr[j + 1]) {
int swap = negArr[j];
negArr[j] = negArr[j + 1];
negArr[j + 1] = swap;
}
}
}
// 1. print out posArr[] and then negArr[]
// or
// 2. merge them into another array and print
}
邏輯解釋如下:
找出正數和負數的總數。
在各自的數組中創建和存儲正值和負值。
按升序對正數組進行排序。
按降序對負數組進行排序。
打印出正數組,然后是負數組或將它們合並到另一個並打印。
我建議另一種方法。 您應該嘗試制定精確比較必須遵守的規則。
您的要求似乎有以下規則:
警告:您正在使用嵌套的 for 循環,這意味着如果數組變大,處理時間將呈指數增長。 好消息是:您不需要將 for 循環嵌套到另一個 for 循環中。 我建議寫一個Comparator
代替:
// The contract of Comparator's only method 'compare(i, j)' is that you
// return a negative value if i < j, a positive (nonzero) value if i > j and
// 0 if they are equal.
final Comparator<Integer> c = (i, j) -> { // I'm using a lambda expression,
// see footnote
// If i is positive and j is negative, then i must come first
if (i >= 0 && j < 0) {
return -1;
}
// If i is negative and j is positive, then j must come first
else if (i < 0 && j >= 0) {
return 1;
}
// Else, we can just subtract i from j or j from i, depending of whether
// i is negative or positive
else {
return (i < 0 ? j - i : i - j);
}
}
您的代碼可能如下所示:
int[] a = { 5, 4, 3, 2, 1, -3, -2, -30 };
int[] yourSortedIntArray = Arrays.stream(a)
.boxed()
.sorted(c) // Your Comparator, could also added inline, like
// .sorted((i, j) -> { ... })
.mapToInt(i -> i)
.toArray();
Lambda 表達式是 Java 8 中的一個新概念。Java 教程提供了一些有價值的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.