2013-12-18 19 views
-4

你能告訴我如何對給定數組進行排序嗎? * [0,1,1,0,0,..... n次] *最低限度的強制性; 我試試這樣如何以最小複雜度排序陣列?

for (int i=0;i<n/2;i++){ 
if(a[i]>a[n-i]) 
swap code 

} 

是最好的方法嗎?

第二個問題:我們做的某個時候這樣

Button btn =new button; 
Btn.setchangelistner(new OnclickListner); 

OnclickListner是接口爲什麼與新

一樣**new runnable**

+2

張貼問題時,不要採取任何快捷方式。 –

+3

分開你的問題。不要;二合一後,它很難回答 – Sinkingpoint

+0

請定義最低複雜度。 'Arrays.sort(a);'非常簡單。另外,你的方式不起作用。你的第二個問題是不完整的,但我想你問什麼是[匿名類](http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)。 –

回答

3

你能做到這樣使用

Arrays.sort(a); 
+0

我需要一些最小複雜度的算法?沒有使用inbuid函數 – user2648752

+0

複雜性是NlogN – John

+0

我不想Array.sort :( – user2648752

0

問題一:

Array a = ["1", "2", "Hello", "World"]; 

Arrays.sort(a); 

應該這樣做。

我不確定你問的是兩個問題。你應該重新提交它作爲一個單獨的問題,並使其更清晰。

+2

'Arrays.sort(a)'不會返回值,它是一個過程;它會修改要排序的數組, t downvote,但這就是爲什麼有人這樣做,我會upvote,如果你改變你的答案 –

+0

數組只有零和一個元素.. – user2648752

+0

謝謝你,你是對的!編輯。 – AnthonyM

1

您應始終使用Arrays.sort()對數組進行排序,因爲它非常高效。它使用快速排序算法的一個版本:

將指定的字節數組按照數字升序排序。排序算法是一個經過調整的quicksort,改編自Jon L. Bentley和M. Douglas McIlroy的「Engineering a Sort Function」,Software-Practice and Experience,Vol。 23(11)第1249-1265頁(1993年11月)。該算法在許多數據集上提供n * log(n)性能,導致其他快速排序降至二次性能。

不要試圖自己排序數組,除非有特定的方法,你想它排序。 Arrays.sort()將能夠做得更快,並且使用更少的內存。

N * logN(quicksort)是大問題規模下最有效的算法。如果你有一個非常小的問題大小,那麼有更高效的算法,但對於任何真實世界的問題大小,這是最好的算法。

總結:

int array[] = new int[1000]; 

// fill array 

Arrays.sort(array); 

// you are passing your array as a pointer 
// so there is no need for assignment here. 

編輯:

我想出了出來是線性時間,如果數組只有1和0的簡單排序算法。有更好的解決方案在那裏,但這應該讓你開始:

/** 
* This will sort an array of just 1s and 0s 
* 
* @param array the array to sort 
*/ 
public int[] simpleSort(int[] array) 
{ 
    int[] sorted = new int[array.length]; 
    int start1 = 0; // where to start putting 1s 
    int end0 = sorted.length - 1; // where to start putting 0s 

    for(int x = 0;x < sorted.length;x++) 
    { 
     if(array[x] == 1) 
     { 
      sorted[start1] = 1; 
      start1++; 
     }else{ 
      sorted[end0] = 0; 
      end0--; 
     } 
    } 

    return sorted; 
} 
+0

我不使用inbuid函數的java 。我想做自己的排序程序 – user2648752

+0

@ user2648752你想要編寫一個實際的排序功能的代碼嗎? – John

+0

是..沒有使用array.sort – user2648752

0
從明顯 Arrays.sort()方法是這樣

旁白:

import java.util.Arrays; 

Arrays.sort(array); 

我已經創建了一個插入排序算法的方法來排序的int[](中評論來自於我是一名noob程序員)。插入似乎對我和我所有的嘗試都是最快的。

public int[] sort(int[] array) // public static int[] sort(int[] array) 
{ 
    int pass = 0, comp = 0; // represents pass as the position, in the array, of the value being inserted; and comp, as the comparison variable, that is the value being inserted. 
    int temp; // represents a temporary int to assist in switching array values. 

    for (pass = 1; pass < array.length; pass++) // for every position in the array (except the first element)... 
    { 
     comp = pass; // have the comparison/insertion variable be equal to which pass the method is on. 

     while (comp > 0) // only do this while the comparison/insertion variable position is greater than 0 (because otherwise it wouldn't have any adjacent positions (to the left) left). 

      if (array[comp] < array[comp - 1]) // if the comparison/insertion value is less than its adjacent position (to the left), swap the values: 
      { 
       temp = array[comp]; // set the temporary variable to hold the value of the comparison/insertion variable's value. 
       array[comp] = array[comp - 1]; // change the comparison/insertion variable's value to be its adjacent positions value. 
       array[comp - 1] = temp; // set the adjacent positions value to be the temporary variable's value. 
       comp --; // set the comparison to be backwards 1 to keep checking where it needs to finally be inserted. 
      } 
      else // otherwise (if the comparison variable value is greater than or equal to its adjacent position to the left)... 
      { 
       break; // exit the do loop to avoid an endless cycle. 
      } 
    } 
    return array; 
} 

樣品輸入/用途:

public static void main(String[] args) 
{ 
    int[] array = {54, 7, 2, 7, 15, 16}; 
    System.out.print(Arrays.toString(sort(array))); 
} 

[2,7,7,15,16,54]

+0

只有o和1個元素的數組 – user2648752

+0

@ user2648752它也適用於此。 –

+0

但是想要的是複雜性? – user2648752