2017-11-30 25 views

答えて

1

exchangeObject関数を使用すると、以下の関数が正しい方法です。

NSArray *arr = @[@10, @20, @60, @40, @50, @30]; 
    int n = (int)arr.count; 

    NSMutableArray *arrm = [arr mutableCopy]; 

     for (int i = n-1; i > 0; i--) 
     { 
      // Check if arrm[i] is not in order 
      if (arrm[i] < arrm[i-1]) 
      { 
       // Find the other element to be 
       // swapped with arr[i] 
       int j = i-1; 
       while (j>=0 && arrm[i] < arrm[j]) 
        j--; 

       // Swap the pair 
       [arrm exchangeObjectAtIndex:i withObjectAtIndex:j+1]; 
       break; 
      } 
     } 

    NSLog(@"Given array is :%@",arr); 
    NSLog(@"Sorted array is :%@",arrm); 

幸運!