java代码改为objective-c

改写代码

将以下java代码改写为oc代码

import java.util.Arrays;
import java.util.Random;
public class quickSort {
public static void main(String[] args) {
  int[] a = new int[10];
        boolean flag = true;
        //random array
        for (int i = 0; i < a.length; i++) {
            Random rd = new Random();
            a[i] = rd.nextInt(10);
        }

        System.out.println("Random Array :");
        System.out.println(Arrays.toString(a));
        System.out.println();
        System.out.println("Quick Sort :");

        //快速排序开始
        quickSort(a, 0, a.length - 1);

        System.out.println(Arrays.toString(a));
}
public static int partition(int[] a, int low, int high){
int pivotkey = a[low]; //将第一个数作为pivotkey
while(low < high){
//交替移动low和high下标
while (high > low && a[high] >= pivotkey){
high--;
}
//找到一个比pivotkey小的值放在左边
a[low] = a[high];

        while (high > low && a[low] <= pivotkey){
            low++;
        }
        //找到一个比pivotkey大的值放在右边

        a[high] = a[low];
    }
    a[low] = pivotkey;

    return low;
}

public static void quickSort(int[] a,int low,int high){
    int pivot;
    if(low < high){
        pivot = partition(a,low,high);
        quickSort(a,low,pivot-1);
        quickSort(a,pivot+1,high);
    }
}

}

在Controller里边写的,请参考

- (void)viewDidLoad {
    [super viewDidLoad];
    int a[10];
//    BOOL flag = YES; // 没用到
    for (int i = 0; i < sizeof(a); i++) {
        a[i] = arc4random() % 10;
    }
    NSLog(@"Random Array :\n");
    for (int i = 0; i < sizeof(a); i++) {
        NSLog(@"%d ", a[i]);
    }
    NSLog(@"\n");
    NSLog(@"Quick Sort :\n");
    [self quickSort:a low:0 high:sizeof(a) - 1];
    for (int i = 0; i < sizeof(a); i++) {
        NSLog(@"%d ", a[i]);
    }
}

- (int)partition:(int[])a low:(int)low high:(int)high {
    int pivotkey = a[low]; //将第一个数作为pivotkey
    while(low < high){
        //交替移动low和high下标
        while (high > low && a[high] >= pivotkey){
            high--;
        }
        //找到一个比pivotkey小的值放在左边
        a[low] = a[high];
        while (high > low && a[low] <= pivotkey){
            low++;
        }
        //找到一个比pivotkey大的值放在右边
        a[high] = a[low];
    }
    a[low] = pivotkey;
    return low;
}

- (void)quickSort:(int[])a low:(int)low high:(int)high {
    int pivot;
    if(low < high){
        pivot = [self partition:a low:low high:high];
        [self quickSort:a low:low high:pivot-1];
        [self quickSort:a low:pivot+1 high:high];
    }
}

- (void) quickSortFromLeft:(NSInteger)leftIndex toRight:(NSInteger)rightIndex {
    
    if (leftIndex >= rightIndex) {
        return;
    }

    NSInteger i = leftIndex;
    NSInteger j = rightIndex;
    NSInteger base = [self.mutableArray[leftIndex] integerValue];
    
    
    while (i != j) {
        while ([self.mutableArray[j] integerValue] >= base && i < j) {
            j --;
        }
        while ([self.mutableArray[i] integerValue] <= base && i < j) {
            i ++;
        }
        
        if (i < j) {
            NSInteger temp = [self.mutableArray[j] integerValue];
            self.mutableArray[j] = self.mutableArray[i];
            self.mutableArray[i] = [NSNumber numberWithInteger:temp];

        }
    }
    
    NSInteger temp = [self.mutableArray[j] integerValue];
    self.mutableArray[j] = [NSNumber numberWithInteger:base];
    self.mutableArray[leftIndex] = [NSNumber numberWithInteger:temp];
    
    
    [self quickSortFromLeft:leftIndex toRight:i-1];
    [self quickSortFromLeft:i+1 toRight:rightIndex];
    
    
 
}