2014-12-13 65 views
-6

我不知道什麼是錯的代碼,它甚至會覆蓋數我想排序一個數組,並不知道什麼地方出錯了。

for (i=0; i<n-1; i++) 
     for (j=i+1; j<n; j++){ 
      if(a[i]<a[j]) 
       temp=a[i]; *// It is not working properly* 
       a[i]=a[j]; 
       a[j]=temp; 
     } 
+2

'std :: sort(a,a + N);'其中'N'是數組的長度。 – juanchopanza 2014-12-13 17:06:34

+2

舉一個簡單的例子並逐步調試 – vsoftco 2014-12-13 17:06:35

+0

@juanchopanza inb4這是作業,不能使用算法 – Borgleader 2014-12-13 17:08:40

回答

4

這不是Python中,你需要括在大括號多個語句​​,以便他們能夠全部執行,只有當條件符合if

if(a[i]<a[j]) 
{ 
    temp=a[i]; *// It is not working properly* 
    a[i]=a[j]; 
    a[j]=temp; 
} 
+0

非常感謝! – MMM 2014-12-13 18:39:15

相關問題