2017-11-30 10 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); 

행운을 빈다!