zoukankan      html  css  js  c++  java
  • iOS 选择排序

    简单选择排序的基本思想:(从小到大)

    第1趟,在待排序记录r[1]~r[n]中选出最小的记录,将它与r[1]交换;

    第2趟,在待排序记录r[2]~r[n]中选出最小的记录,将它与r[2]交换;

    以此类推,第i趟在待排序记录r[i]~r[n]中选出最小的记录,将它与r[i]交换,使有序序列不断增长直到全部排序完毕。

    代码如下:

    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1,@100,@4,@3,nil];
            for (int i = 0; i< arr.count; i++) {
                for (int j = i +1; j<arr.count; j++) {
                    if (arr[i] > arr[j]) {
                        [arr exchangeObjectAtIndex:i withObjectAtIndex:j];
                    }
                }
            }
            NSLog(@"%@",arr);
        }
        return 0;
    }
    

     运行结果如下:

    2018-01-13 10:44:43.666392+0800 选择排序[953:128931] (
        1,
        3,
        4,
        100
    )
    Program ended with exit code: 0
  • 相关阅读:
    php apc
    nginx https
    js弹出确认框,挺全
    websocket nodejs
    nodejs express测试
    【C++】Mandelbrot集绘制(生成ppm文件)
    【Scheme】Huffman树
    【Scheme】符号求导
    【Scheme】树结构
    【Scheme】序列的操作
  • 原文地址:https://www.cnblogs.com/guohai-stronger/p/8278552.html
Copyright © 2011-2022 走看看