// main.cpp
// insertSort
// Created by Jason on 16/9/20.
// Copyright © 2016年 Jason. All rights reserved.
#include <iostream>
using namespace std;
//下面代码作用:当需要C++ 和C 需要混和编译时,需要下面的宏定义
//#ifndef MAX_H //防止重复引用
//#define MAX_H
//#ifdef __cplusplus //凡是C++的编译器都定义了该预编译宏,通过它来检测当前编译器是否使用的是C++编译器。
//extern "C"{
//#endif
// int max(int a,int b);
//#ifdef __cplusplus
// }
//#endif
//#endif
-----------------------------------------------------------------------------------------------------
/**
在一定的上下文环境中,指针和数组是等同的,并非所有情况下如此
数组的定义并不等同于指针的外部声明(注意声明和定义的区别,定义是为一个变量或者对象分配内存空间,而声明只是描述类型)。
**/
//下面代码作用:返回数组元素的个数
//当判断是字符串数组时,需要减一(beacuse 会多算个"\0"字符(结束字符))
//#define ARR_SIZE(array,len) (len = (sizeof(array) / sizeof(array[0])-1));
//如何判断是否是字符串数组,我也没有找到办法,看到网上有人说使用KMP算法,下次研究下(int arr[] = {"abcdefg"})。
////sizeof 不是一个函数,而是单目运算符
#define ARR_SIZE(array,len) (len = (sizeof(array) / sizeof(array[0])));
//插入排序算法原理:
//http://blog.csdn.net/left_la/article/details/8656425
//时间复杂度是O(n^2);空间复杂度是O(1)
void insertSort(int R[],int n) //传递数组和数组元素个数
{
int i ,j;
int temp;
for(i = 0;i<n;++i)
{
temp = R[i];
j = i - 1;
while(j>=0 && temp<R[j])
{
R[j+1] = R[j];
--j;
}
R[j+1] = temp;
}
}
int main(int argc, const char * argv[]) {
int arr[] = {0,8,2,10,50,12,6,7,5,3};
int len;
ARR_SIZE(arr,len);
insertSort(arr, len);
for(int i=0;i<len;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}