算法

//冒泡排序从大到小

  • (void)orderBubbleSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count - 1; i ++) {
    for (int j = 0; j < mArray.count - i - 1; j ++) {
    if ([mArray[j] intValue] > [mArray[j + 1] intValue]) {
    [mArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
    }
    }
    }
    NSLog(@"从小到大%@",mArray);
    }

//冒泡排序从小到大

  • (void)reverseBubbleSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count - 1; i ++) {
    for (int j = 0; j < mArray.count - i - 1; j ++) {
    if ([mArray[j] intValue] < [mArray[j + 1] intValue]) {
    [mArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
    }
    }
    }
    NSLog(@"从大到大小%@",mArray);
    }

//快速排序

  • (void)quickSort:(NSMutableArray *)mArray leftIndex:(int )left rightIndex:(int )right
    {
    if (left < right) {
    int temp = [self getMiddleIndex:mArray leftIndex:left rightIndex:right];
    [self quickSort:mArray leftIndex:left rightIndex:temp - 1];
    [self quickSort:mArray leftIndex:temp + 1 rightIndex:right];
    }
    NSLog(@"%@",mArray);
    }

  • (int )getMiddleIndex:(NSMutableArray *)mArray leftIndex:(int )left rightIndex:(int )right {
    int tempValue = [mArray[left] intValue];
    while (left < right) {
    while ((left < right) && (tempValue <= [mArray[right] intValue])) {
    right --;
    }
    if (left < right) {
    mArray[left] = mArray[right];
    }
    while (left < right && ([mArray[left] intValue] <= tempValue)) {
    left ++;
    }
    if (left < right) {
    mArray[right] = mArray[left];
    }
    }
    mArray[left] = [NSNumber numberWithInt:tempValue];
    return left;
    }

  • (void)HTTquickSort:(NSMutableArray *)quickArray leftIndex:(NSInteger)left rightIndex:(NSInteger )right
    {
    NSInteger i = left;
    NSInteger j = right;
    NSString *t;
    if (left>right) {
    NSLog(@"%@",quickArray);
    return;
    }
    NSString *temp = quickArray[left];
    while (i != j) {
    while (([quickArray[j] integerValue]>[temp integerValue] || [quickArray[j] integerValue] == [temp integerValue]) && i<j)
    j--;
    while (([quickArray[i] integerValue] < [temp integerValue] || [quickArray[i] integerValue] == [temp integerValue]) && i<j)
    i++;
    if (i<j) {
    t = quickArray[i];
    quickArray[i] = quickArray[j];
    quickArray[j] = t;

      }
    

    }
    quickArray[left] = quickArray[i];
    quickArray[i] = temp;

    [self HTTquickSort:quickArray leftIndex:left rightIndex:i-1];
    [self HTTquickSort:quickArray leftIndex:i+1 rightIndex:right];

}

//选择排序

  • (void)selectSort:(NSMutableArray *)mArray {
    for (int i = 0; i < mArray.count; i ++) {
    int minIndex = i;
    for (int j = i + 1; j < mArray.count; j ++) {
    if ([mArray[minIndex] intValue] > [mArray[j] intValue]) {
    minIndex = j;
    }
    }
    if(minIndex != i) {
    //如果不是无序区的最小值位置不是默认的第一个数据,则交换。
    [mArray exchangeObjectAtIndex:i withObjectAtIndex:minIndex];
    }
    }
    }

//直接插入排序

  • (void)insertSort:(NSMutableArray*)array {
    for (int i = 0;i < [array count] - 1;i ++) {
    if([[array objectAtIndex:i + 1]intValue] < [[array objectAtIndex:i] intValue]) {
    NSNumber *temp=[array objectAtIndex:i+1];
    for (int j = i + 1; j > 0 && [[array objectAtIndex:j - 1] intValue] > [temp intValue]; j --){
    [array exchangeObjectAtIndex:j - 1 withObjectAtIndex:j];
    }
    }
    }
    }

//二分插入排序

  • (void)binaryInsertSort:(NSMutableArray *)mArray {
    //索引从1开始 默认让出第一元素为默认有序表 从第二个元素开始比较
    for(int i = 1 ; i < [mArray count] ; i++){
    //二分查找
    int temp= [[mArray objectAtIndex:i] intValue];
    int left = 0;
    int right = i - 1;
    while (left <= right) {
    int middle = (left + right)/2;
    if(temp < [[mArray objectAtIndex:middle] intValue]){
    right = middle - 1;
    } else {
    left = middle + 1;
    }
    }
    //排序
    for(int j = i ; j > left; j --){
    [mArray replaceObjectAtIndex:j withObject:[mArray objectAtIndex:j - 1]];
    }
    [mArray replaceObjectAtIndex:left withObject:[NSNumber numberWithInt:temp]];
    }
    }

//希尔排序
-(void)shellSort:(NSMutableArray *)mArray {
int gap = (int)[mArray count] / 2;
while (gap >= 1) {
for(int i = gap ; i < [mArray count]; i ++){
int temp = [[mArray objectAtIndex:i] intValue];
int j = i;
while (j >= gap && temp < [[mArray objectAtIndex:(j - gap)] intValue]) {
[mArray replaceObjectAtIndex:j withObject:[mArray objectAtIndex:j-gap]];
j -= gap;
}
[mArray replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
}
gap = gap / 2;
}
}

//堆排序(目前不支持负数)

  • (void)heapSort:(NSMutableArray *)mArray isAsc:(BOOL)isAsc {
    for (NSInteger i = mArray.count / 2 - 1; i >= 0; -- i) {
    [self siftWithLow:i high:mArray.count asc:isAsc array:mArray];
    }
    for (NSInteger i = mArray.count - 1; i >= 1; -- i) {
    id temp = mArray[0];
    mArray[0] = mArray[i];
    mArray[i] = temp;
    [self siftWithLow:0 high:i asc:isAsc array:mArray];
    }
    }

  • (void)siftWithLow:(NSInteger)low high:(NSInteger)high asc:(BOOL)isAsc array:(NSMutableArray *)mArray {
    NSInteger left = 2 * low + 1;
    NSInteger right = left + 1;
    NSInteger lastIndex = low;
    //左子节点大的情况
    if (left < high && ((mArray[left] > mArray[lastIndex] && isAsc) || (mArray[left] < mArray[lastIndex] && !isAsc))) lastIndex = left;
    //右子节点大的情况
    if (right < high && ((mArray[right] > mArray[lastIndex] && isAsc) || (mArray[right] < mArray[lastIndex] && !isAsc))) lastIndex = right;
    //节点改变
    if (lastIndex != low) {
    //较大的节点值将交换到其所在节点的父节点
    id temp = mArray[low];
    mArray[low] = mArray[lastIndex];
    mArray[lastIndex] = temp;
    //递归遍历
    [self siftWithLow:lastIndex high:high asc:isAsc array:mArray];
    }
    }
    @end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容