用递归算法判断数组a[N]是否为一个递增数组

来源:互联网 发布:贵阳大数据学校招生 编辑:程序博客网 时间:2024/05/17 22:35
用递归算法判断数组a[N]是否为一个递增数组。
#include <stdio.h>

void QSort(int* data, int low, int high)
{
int newLow = low, newHeigh = high;
int temp = data[low];
while(newLow < newHeigh)
{
while( (newLow<newHeigh) && (temp<=data[newHeigh]) )
newHeigh--;

if(newLow < newHeigh)
{
data[newLow] = data[newHeigh];
newLow++;
}

while( (newLow<newHeigh) && (data[newLow]<temp) )
newLow++;

if(newLow < newHeigh)
{
data[newHeigh] = data[newLow];
newHeigh--;
}
}

data[newLow] = temp;
if(low < (newLow-1))
QSort(data, low, newLow-1);

if((newLow+1) < high)
QSort(data, newLow+1, high);
}

bool IsIncrem(int a[], const int len)
{
if(len == 1)
return true;

if(a[len-1] >= a[len-2])
return IsIncrem(a, len-1);

return false;
}

void main()
{
int i;
int array[] = {3, -2, 1, 8, 5, 100, 30, 70, 110, 90};
int len = sizeof(array)/sizeof(int);

printf("排序前:");
for(i=0; i<len; i++)
printf(" %d", array[i]);
printf("\n");

bool flag = IsIncrem(array, len);
printf("是否递增:%d\n", flag);

printf("排序后:");
QSort(array, 0, len-1);
for(i=0; i<len; i++)
printf(" %d", array[i]);
printf("\n");

flag = IsIncrem(array, len);
printf("是否递增:%d\n", flag);
}



原创粉丝点击