第二十八课作业(递归排序)
本帖最后由 wai1216 于 2016-6-19 11:27 编辑个人评价写得略丑。。。
--------------
1.
void stol(int *a, int b)
{
int temp;
if (b - 1 <= 0)
return;
if (*(a + b - 2) < *(a + b - 1))
{
temp = *(a + b - 1);
*(a + b - 1) = *(a + b - 2);
*(a + b - 2) = temp;
stol(a, b - 1);
}
stol(a, b - 1);
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
for (i = n; i > 0; i--)
{
stol(a, i);
}
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
看视频之后的2,3
--------------
2.void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void stol(int *a, int n)
{
int i = 0;
for (i = n; i > 0; i--)/*将循环放在了函数里面*/
{
if (n - 1 <= 0)
return;
if (*(a + n - 2) < *(a + n - 1))
{
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
stol(a, n - 1);
}
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
-------------------
3.void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
int Retn(int a,int b)
{
return a < b ? 1: 0;
}
void stol(int *a, int n)
{
int i = 0;
if (n - 1 <= 0)
return;
for (i = n; i > 0; i--)
{
if (Retn(*(a + n - 2),*(a + n - 1)))/*修改if判断,以及逻辑化简写stol*/
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
}
void main()
{
int i = 0, n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
-------------
4.void swap(int * a,int * b)
{
int temp = *b;
*b = *a;
*a = temp;
}
int Retn(int a,int b)
{
return a < b ? 1: 0;
}
void stol(int *a, int n)
{
int i = 0;
if (n - 1 <= 0)
return;
for (i = n; i > 0; i--)
{
if (Retn(*(a + n - 2),*(a + n - 1)))
swap(a + n - 2,a + n - 1);
stol(a, n - 1);
}
}
void Showarray(int *a,int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
printf("%d ", *(a + i));
}
}
void main()
{
int n = 10;
int a[] = { 12,45,17,95,24,67,32,11,5,100 };
stol(a,n);
Showarray(a,n);/*调用函数来输出*/
}
页:
[1]