2015-10-19 38 views
0

我在Java中的數組排序零,正數和負數的數組

int num[] = {5, -3, 0, -18, 1, 2, 0}; 

我想使它看起來像這些

{-3, -18, 0, 0, 5, 1, 2} 

我現在所擁有的是這個

for (int i=0; i<num.length;i++) 
    { 
     for (int j=1; j<num.length-1;j++) 
     { 
      if (num[j]<0) 
      { 
       temp=num[j]; 
       c=num[j-1]; 
       num[j-1]=temp; 
       num[j]=c; 
      } 
     } 
    } 

但這種只是負數,我找不到解決方案如何排序零。 你能幫我嗎?

+9

我不明白的排序邏輯,你可以擴大一點? – Tunaki

+0

我需要在乞討數組中有負數,然後是零,最後是正數。 –

+2

@Tunaki將未排序的數組變成另一個未排序的數組... ...? – redFIVE

回答

5

您可以使用Arrays.sort()用自定義的比較器,這將保持等號的數字順序:

Integer[] num = {5, -3, 0, -18, 1, 2, 0}; 
Arrays.sort(num, (a, b) -> Integer.signum(a) - Integer.signum(b)); 
System.out.println(Arrays.toString(num)); // [-3, -18, 0, 0, 5, 1, 2] 

編輯:因爲事實證明,OP只是想一個普通的那種,那將是

Arrays.sort(num); 
+1

'Integer.signum'可能是一個更好的選擇。 – Bubletan

+0

加上一個lambda是我認爲最清潔的方法 – silentprogrammer

2

其主要思想是從int[]的頭部和尾部向內迭代2個指針,直到它們相遇,並且如果positivenegative錯位,則將它們交換。這將工作:

public class Solution { 
    public void sortNumbers(int[] A) { 
     int a = 0; 
     int b = A.length - 1; 
     for (int i = 0; i < A.length && i <= b; i++) { 
      int cur = A[i]; 
      if (cur < 0) { 
       this.swap(A, i, a); 
       a++; 
      } else if (cur > 0) { 
       this.swap(A, i, b); 
       b--; 
       i--; 
      } 
     } 
    } 

    private void swap(int[] A, int i, int j) { 
     int tmp = A[i]; 
     A[i] = A[j]; 
     A[j] = tmp; 
    } 
} 

運行時間爲O(n),其中n是int[]

1

長度可以使用一個簡單的冒泡排序,比較相鄰元素的跡象,並交換那些誰站在錯誤的(即i-th有「大牌子」比(i+1)-th):

for (int j = num.length - 1; j >= 0; j--) { 
    for (int i = 0; i < j; i++) { 
     if (Integer.signum(num[i]) > Integer.signum(num[i+1])) { // comparing signs 
      int temp = num[i]; 
      num[i] = num[i+1]; 
      num[i+1] = temp;    
     } 
    } 
} 

冒泡排序是穩定的,因此「平等」的元素(在這裏,相等的元素被認爲是那些誰具有相同的符號)不會改變它們的順序,讓你的願望d結果。這種簡單的算法效率不高,但仍然有助於理解排序方法(不需要額外空間並僅在初始陣列上運行)是如何實現的。

+0

Awsome,謝謝! –

+0

首先我誤解了你的問題,我剛剛編輯了我的答案以符合實際要求 –

1

下面是一個使用布爾帶做一個簡單的implentation ... while循環和對內環:

public static void main(String[] args) { 
    int num[] = {5, -3, 0, -18, 1, 2, 0}; 

    int temp = 0; 
    boolean finished = false; 

    do{ 
     finished = true; // This will stay true if nothing needs to be changed in your array. 
     for (int i = 0 ; i < num.length - 1 ; i++){ 
      if (num[i] > num[i+1]){ 
       finished = false; // Can not go off the loop if it is not sorted yet. 
       temp = num[i]; // Interchanging of array's indexes 
       num[i] = num[i+1]; 
       num[i+1] = temp; 
      } 
     } 
    } while(!finished); 

    System.out.println(Arrays.toString(num)); 
} 

輸出

[-18, -3, 0, 0, 1, 2, 5] 
+0

與使用['Arrays.sort()'](http://docs.oracle.com/javase/7/docs) /api/java/util/Arrays.html#sort(INT []))? – dguay

+0

@dguay算法我的朋友。 Sort()不是沒有趣味。 –

+0

@dguay'Arrays.sort()'的實現方式非常不同 –

0

有可用於從一個簡單的冒泡排序的整數數組許多不同種類的排序到一個稍微複雜的堆排序。有些種類比另一種更快,例如堆排序比泡泡排序更快,但是這主要影響大型數組。這是由於數字之間的交換和比較的數量。

冒泡排序

public class BubbleSort 
{ 
    public void sortArray(int[] a) 
    {  
     int c, d, swap; 

     for(c = 1; c < a.length; c++) 
     { 
      for (d = 0; d < a.length - c; d++) 
      { 
       if (a[d] > a[d+1]) 
       { 
        swap = a[d]; 
        a[d] = a[d+1]; 
        a[d+1] = swap; 
       } 
      } 
     } 

     for(int i=0;i<a.length;i++) 
     { 
      int correctNumber = i+1; 
      System.out.println("Value "+correctNumber+" of the sorted array which was sorted via the Bubble Sort is: "+a[i]); 
     } 
    } 
} 

堆排序

public class HeapSort 
{ 
    private static int[] a; 
    private static int n; 
    private static int left; 
    private static int right; 
    private static int largest; 


    public static void buildheap(int []a) 
    { 
     n=a.length-1; 
     for(int i=n/2;i>=0;i--) 
     { 
      maxheap(a,i); 
     } 
    } 

    public static void maxheap(int[] a, int i) 
    { 
     left=2*i; 
     right=2*i+1; 
     if(left <= n && a[left] > a[i]) 
     { 
      largest=left; 
     } 
     else 
     { 
      largest=i; 
     } 

     if(right <= n && a[right] > a[largest]) 
     { 
      largest=right; 
     } 

     if(largest!=i) 
     { 
      exchange(i,largest); 
      maxheap(a, largest); 
     } 
    } 

    public static void exchange(int i, int j) 
    { 
     int t=a[i]; 
     a[i]=a[j]; 
     a[j]=t; 
    } 

    public static void sort(int[] a0) 
    { 
     a=a0; 
     buildheap(a); 

     for(int i=n;i>0;i--) 
     { 
      exchange(0, i); 
      n=n-1; 
      maxheap(a, 0); 
     } 

     for(int i=0;i<a.length;i++) 
     { 
      int correctNumber = i+1; 
      System.out.println("Value "+correctNumber+" of the sorted array which was sorted via the Heap Sort is: "+a[i]); 
     } 
    } 
}