为什么该程序没有得到任何输出来使用递归对数组进行冒泡排序?

时间:2019-03-10 10:08:04

标签: c arrays

我试图做的是运行对(n-1)次进行排序的过程,并因此对main函数中的if else条件进行了排序。由于我们必须使用递归,因此我在用户定义的函数中使用了if else阶梯。

#include <stdio.h>

int bin (int a[]);

int n, i, a[100], j = 0, temp, count = 0;

int main ()
{
    printf ("enter the number of elements\n");
    scanf ("%d", &n);

    printf ("enter the array elements\n");
    for (i = 0; i < n; i++)
        scanf ("%d", &a[i]);

    bin (a);
    if (count == n - 1)
    {
        for (i = 0; i < n; i++)
        {
            printf ("%d", a[i]);
        }
        return 0;
    }
    else
    {
        j = 0;
        bin (a);
    }
}

int bin (int a[])
{
    if (j != n - 1 && a[j] > a[j + 1])
    {
        temp = a[j];
        a[j] = a[j + 1];
        a[j + 1] = temp;
        j++;
        bin (a);
    }
    else if (j != n - 1 && a[j] < a[j + 1])
    {
        j = j + 1;
        bin (a);
    }
    else
    {
        count++;
        return 0;
    }
}

2 个答案:

答案 0 :(得分:2)

考虑输入

4           // aka n
1 1 4 5     // the array elements

第一次调用bin时,变量j的值为0,因此a[j] > a[j + 1]将为false(因为a[0]等于a[1])。同样,a[j] < a[j + 1]将为false。所以你最终执行了

else
{
    count++;
    return 0;
}

即该代码将使count递增,使其变为1,然后函数返回。

返回main,条件(count == n - 1)将为假,因此您将在本部分结束:

else
{
    j = 0;
    bin (a);
}

什么都不打印。因此-没有输出。

答案 1 :(得分:0)

取决于元素的数量:

  • 1:Containers: 1 Running: 0 Paused: 0 Stopped: 1 Images: 2 Server Version: 18.09.3 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: nvidia runc Default Runtime: nvidia Init Binary: docker-init containerd version: e6b3f5632f50dbc4e9cb6288d911bf4f5e95b18e runc version: 12f6a991201fdb8f82579582d5e00e28fba06d0a-dirty init version: fec3683 Security Options: apparmor seccomp Profile: default Kernel Version: 4.4.0-1060-aws Operating System: Ubuntu 16.04.4 LTS OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 7.795GiB Name: ip-10-220-3-78 ID: YZ6T:HXM4:XJNW:GUDY:XA6J:U2KX:R7CJ:TQHE:TPXY:HCNA:R4VL:M3AZ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community Engine WARNING: API is accessible on http://0.0.0.0:2375 without encryption. Access to the remote API is equivalent to root access on the host. Refer to the 'Docker daemon attack surface' section in the documentation for more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface WARNING: No swap limit support 为假,所以您不打印
  • 2:(count == n - 1)始终为true,因此您进行打印并对数组进行了排序
  • > 1:(count == n - 1)始终为假,因此您永远不会打印(确保我使用了蛮力进行检查)

我还建议您始终检查 scanf 的结果,以确保输入了有效值。

使用(奇怪的)递归进行排序的方法有很多,其中一种可以是:

(count == n - 1)

编译和执行:

#include <stdio.h>
#include <stdlib.h>

int sort(int a[], int max)
{
  if (max == 0)
    return 0;

  int modified;

  if (a[0] > a[1]) {
    int v = a[0];

    a[0] = a[1];
    a[1] = v;
    modified = 1;
  }
  else
    modified = 0;

  return (sort(a + 1, max - 1) && sort(a, max - 1)) || modified;
}

int main ()
{
  int n, i;

  printf ("enter the number of elements\n");
  if ((scanf ("%d", &n) != 1) || (n <= 0))
    return -1;

  int * a = malloc(n * sizeof(int));

  if (a == NULL) {
    puts("not enough memory");
    return -1;
  }

  printf ("enter the array elements\n");
  for (i = 0; i < n; i++) {
    if (scanf ("%d", &a[i]) != 1) {
      puts("invalid value");
      return -1;
    }
  }

  sort(a, n - 1);

  for (i = 0; i < n; i++)
    printf ("%d ", a[i]);
  putchar('\n');

  free(a);

  return 0;
}