#import <Foundation/Foundation.h>
//       宏名 替换的内容[千万别加分号]
#define VALUE 10

//命名规范,纯大写或者是k开头的驼峰命名法
#define AS 52.02

#define SUM(A, B) A + B

#define SQUARE(A) ((A) + (A))//安全

#define MAXXX(A, B) (((A) > (B)) ? (A) : (B))

#define MAX_NUMBER(A, B, C) ((((A) > (B)) ? (A) : (B)) > (C) ? (((A) > (B)) ? (A) : (B)) : (C))

#define MAX_ABC(A, B, C) MAXXX(A, B) > (C) ? MAXXX(A, B) : (C)

#define MAX_ABCD(A,B,C,D) MAX_ABC(A,B,C) > (D) ? MAX_ABC(A,B,C) : (D)

//宏定义 冒泡排序
#define kBubbleSort(array, number)\
for (int i = 0; i < number - 1; i++){\
for (int j = 0; j < number - 1 - i; j++){\
if (array[j] > array[j + 1]){\
int team = array[j];\
array[j] = array[j + 1];\
array[j + 1] = team;\
}\
}\
}\
//宏定义 选择排序
#define kChooseSort(A,N)\
for (int i = 0; i < n - 1; i++){\
int num = i;\
for (int j = i + 1; j < n; j++){\
if (A[mum] > A[j]){\
num = j;\
}\
}\
if (num != i){\
int team = A[min];\
A[min] = A[i];\
A[i] = team;\
}\
}\

int main(int argc, const char * argv[]) {


 
    //宏定义[预编译指令]
    //本质就是替换
//    int a = VALUE ;
//    float b = AS;
//    printf("%d %f", a, b);
    
//    int array[VALUE] = {0};
//    for (int i = 0; i < VALUE; i++){

//        array[i] = arc4random() % 101;
//    printf("%d\t",array[i]);
//    }
    
//    int a = SUM(5, 8);
//    printf("%d",a);
    
//    int a = SQUARE(5);
//    printf("%d",a);
//    
//    int a = MAXXX(15, 9);
//    printf("%d",a);
    
//    int a = MAX_NUMBER(20, 11, 19);
//    printf("%d",a);
    
//    int a = MAX_ABC(25,19,20);
//    printf("%d",a);
//    int a = MAX_ABCD(15,111,18,20);
//    printf("%d",a);
    
    //在switch case里面定义变量要加{ };
//    int array[VALUE] = {0};
//    for (int i = 0; i < VALUE; i++){

//            array[i] = arc4random() % 101;
//        printf("%d\t",array[i]);
//        }
//    printf("\n");
//    for (int i = 0; i < VALUE; i++) {

//        kBubbleSort(array, VALUE);
//        printf("%d\t",array[i]);
//    }
    
    //冒泡排序[算法优化]
//    int a[10] = {0};
//    for (int i = 0;i < 10; i++){

//        a[i] = arc4random() % 101;
//        printf("%d\t",a[i]);
//    }
    int a[10] ={15, 64, 56 , 65, 48 , 56, 49, 25, 69,34};
    //定义BOOL值假定是否需要下一趟比较
    BOOL needNeat = YES;
    for (int i = 0;i < 10 - 1 && needNeat == YES; i++){

        
        needNeat = NO;//没趟开始假定不需要下一趟比较
        for (int j = 0; j <10 - 1 -i ; j++) {

            if (a[j] > a[j + 1]) {

                //如果这一趟中有过一次交换说明还是乱序
                needNeat = YES;
                int tem = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tem;
            }
        }
    }
    printf("\n");
    for (int i = 0; i < 10; i++) {

        printf("%d\t",a[i]);
    }
   
//    //选择排序
//    int a[10] = {0};
//    for (int i = 0;i < 10; i++){

//        a[i] = arc4random() % 101;
//        printf("%d\t",a[i]);
//    }
//    for (int i = 0; i < 10- 1; i++) {

//        int min = i;
//        for (int j = i + 1; j < 10; j++) {

//            if (a[min] > a[j]) {

//                min = j;
//            }
//        }
//        if (min != i) {

//            int tem = a[min];
//            a[min] = a[i];
//            a[i] = tem;
//        }
//    }printf("\n");
//    for (int i = 0; i < 10; i++) {

//        printf("%d\t",a[i]);
//    }
    
    //折半查找[二分法查找](仅适用于排好序的数组)
    int start = 0;//开始下标
    int end = 10 - 1;//结束下标
    int mid = (start + end) / 2;//中见下标
    int number = 64;//要从数组中检索的值
    //如果中间的数不是我们想要找的数,并且开始下标小于结束,循环继续
    while (a[mid] > number && start < end) {

        //如果中间数比要找的数大,说明在左半部范围内
        if (a[mid] > number) {

            end = mid -1;//调整结束下标
        }else {//否则在右半部范围内
            start = mid + 1;//调整开始下标
        }
        //再次确定中间值
        mid = (start + end) / 2;
    }
    if (a[mid] == number) {//确定要找的数,并输出
        printf("%d\n",mid);
    }
    return 0;
}