地狱的C代码:运行到无限循环的快速排序程序:奇怪的情况

地狱的C代码:运行到无限循环的快速排序程序:奇怪的情况,c,quicksort,C,Quicksort,我已经为快速排序编写了一个C代码,看起来非常好。 但是代码工作不完美,在从数组中获取值时会奇怪地进入infinte循环或其他我不知道的东西,并且在获取值的循环之后什么也不做 #include<stdio.h> int flag=0; int partition(int *,int,int); void quicksort(int *A,int low, int high) //Code for quicksort function { int pivot;

我已经为快速排序编写了一个C代码,看起来非常好。 但是代码工作不完美,在从数组中获取值时会奇怪地进入infinte循环或其他我不知道的东西,并且在获取值的循环之后什么也不做

#include<stdio.h>
int flag=0;
int partition(int *,int,int);
void quicksort(int *A,int low, int high)          //Code for quicksort function
{
    int pivot;
    printf("%d",flag);
    flag++;
    if(low<high)
    {
     pivot =partition(A,low,high);        //calls partition function
     quicksort(A,low,pivot);
     quicksort(A,pivot,high);
    }
}
//code for partition function
int partition(int *A,int low,int high)
{
    int pivot,left,right,temp;
    pivot=A[low];
    left=low;
    right=high;
    printf("%d",flag);
    flag++;
    while(left<right)
        {
         while(A[left]<pivot)
            left++;
         while(A[right]>pivot)
            right++;
         if(left<right)
            {
             temp=A[left];
             A[left]=A[right];
             A[right]=temp;
            }
        }
    temp=A[right];
    A[right]=A[left];
    A[left]=temp;
    return right;
}

int main()
    {
        int a[10],i,n;
        printf("\n***QUICK SORT***");
        printf("\nENTER THE NUMBERS OF ENTRIES:");
        scanf("%d",&n);

        printf("\nENTER THE ENTRTIES IN ARRAY:");
        //PROBLEM IS IN THIS LOOP OR ELSE (I DONT KNOW WHAT EXACTLY WHAT THE PROBLEM IS)

        for(i=0;i<n;i++)
        {
            printf("i=%d\n",i);
            scanf("%d",&a[i]);     
        }
        //IF WE COMMENT THIS BELOW LINE OF FUNCTION CALL THEN LOOP WORKS FINE
        quicksort(a,0,n-1);    //passes the array and first and last element

        printf("\nTHE SORTED ARRAY IS:\n");
        for(i=0;i<n;i++)
            printf(" %d \n ",a[i]);
        return 0;
    }

正如许多人已经在您对代码的注释中指出的那样。您需要重新考虑快速排序算法的分区步骤-您遇到无限循环的原因是,在交换之后,左侧和右侧从未更新,从而导致无限循环

这不是我自己的,但在我学习复杂排序算法时对我有所帮助:

void quickSort(int arr[], int left, int right) {
  int i = left, j = right;
  int tmp;
  int pivot = arr[(left + right) / 2];
  /* partition */
  while (i <= j) {
        while (arr[i] < pivot)
              i++;
        while (arr[j] > pivot)
              j--;
        if (i <= j) {
              tmp = arr[i];
              arr[i] = arr[j];
              arr[j] = tmp;
              i++;
              j--;
        }
  };
您可能会发现这很有帮助,正如许多人所说的,您可能需要清理代码以帮助调试;应该是对的; 或者两边都在增加。

在您的函数快速排序中

这些论点应该是这样的-

quicksort(A,low,pivot-1);  
quicksort(A,pivot+1,high);
在函数划分中

在这个循环中,右边应该递减

    while(A[right]>pivot)
        right--;
最后介绍了该函数中的交换

 temp=A[right];
 A[right]=A[left];
 A[left]=temp;
但实际上,正确的位置应该是支点的最后一个位置

 A[left]=A[right]
 A[right]=pivot

还有一个建议,请在main function中增加数组a[]的大小。

正如许多评论者已经指出的那样,这段代码中存在很多问题,我只是想向您展示一个实际可行的实现,包括关于所做工作的有用注释:

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

#define CHUNK_SIZE 16

static void quicksort_rec(int *l, int *r)
{
    int *p, *ll, *rr, t;

    if (r-l < 1) return; /* only one element left -> nothing to sort */

    p = r;      /* use last element for pivot (any element would do) */
    rr = r-1;   /* pointer to last element to compare with pivot */
    ll = l;     /* pointer to first element to compare with pivot */

    while (1)
    {
        /* move ll until we find something greater than pivot on the left */
        while (ll <= rr && *ll <= *p) ++ll;

        /* move rr until we find something smaller than pivot on the right */
        while (ll <= rr && *rr >= *p) --rr;

        /* ll and rr met? then we're done with this step */
        if (rr <= ll)
        {
            /* swap pivot to the "meeting position" */
            t = *p;
            *p = *ll;
            *ll = t;

            /* sort partitions recursively */
            quicksort_rec(l, ll-1);
            quicksort_rec(ll+1, r);

            /* done */
            return;
        }

        /* swap greater element on the left with smaller element on the right */
        t = *rr;
        *rr = *ll;
        *ll = t;
    }
}

static void quicksort(int *v, int num)
{
    quicksort_rec(v, v+num-1);
}


int main()
{
    char buf[64];       /* buffer for user input */
    int *values;        /* pointer to dynamically allocated array */
    int avail;          /* number of currently free slots in the array */
    int entries = 0;    /* number of total entries in the array */
    int i;              /* iterating variable */

    puts("Quicksort Example\n"
         "=================\n"
         "\n"
         "Enter whole numbers to sort, just hitting enter starts sorting.\n");

    /* allocate first chunk of memory */
    values = malloc(CHUNK_SIZE * sizeof(int));

    if (!values)
    {
        perror("malloc");
        exit(1);
    }
    avail = CHUNK_SIZE;

    while (1)
    {
        fputs("Please enter next number: ", stdout);

        /* try reading user input, if impossible, break */
        if (!fgets(buf, 64, stdin)) break;

        /* if input is empty, break */
        if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n') break;

        /* convert input to integer as next value for array */
        values[entries] = atoi(buf);
        printf("Added `%d' to sort list\n", values[entries]);
        ++entries;

        /* check whether there is space left in the array */
        if (!--avail)
        {
            /* if not, increase array size by the next chunk */
            values = realloc(values, (entries + CHUNK_SIZE) * sizeof(int));
            if (!values)
            {
                perror("realloc");
                exit(1);
            }

            /* reset available slots to size of chunk */
            avail = CHUNK_SIZE;
        }
    }

    printf("Now sorting %d elements with quicksort.\n", entries);

    /* sort the array */
    quicksort(values, entries);

    puts("Result:");

    for (i = 0; i < entries; ++i)
    {
        printf("%d\n", values[i]);
    }

    free(values);
    return 0;
}

我不知道到底是什么问题,除非你使用苹果II,当然:1:不要尖叫!2然后使用调试器找出答案。定义[10],然后要求用户输入大小……惹麻烦……你的分区函数就是你的罪犯,确保你的逻辑正确。@bentank-我同意,枢轴位于数组的最低元素,没有一个局部变量被实例化,在最后几行中,似乎在交换已经发生之后,又进行了另一次交换,这让人相信这就是错误所在@Nikhil Pandit-您可能希望对代码进行一些格式化,以帮助调试过程。如果你认为这是C代码,那就等着瞧吧……请一致地缩进。请使用有意义的评论。例如,“调用分区函数”不是一个有意义的注释。这些信息从代码声明中显而易见,简单地用英语陈述是毫无意义的。函数前的注释需要说明函数的用途、参数的详细信息以及返回值的含义。
 A[left]=A[right]
 A[right]=pivot
#include <stdio.h>       
#include <stdlib.h>

#define CHUNK_SIZE 16

static void quicksort_rec(int *l, int *r)
{
    int *p, *ll, *rr, t;

    if (r-l < 1) return; /* only one element left -> nothing to sort */

    p = r;      /* use last element for pivot (any element would do) */
    rr = r-1;   /* pointer to last element to compare with pivot */
    ll = l;     /* pointer to first element to compare with pivot */

    while (1)
    {
        /* move ll until we find something greater than pivot on the left */
        while (ll <= rr && *ll <= *p) ++ll;

        /* move rr until we find something smaller than pivot on the right */
        while (ll <= rr && *rr >= *p) --rr;

        /* ll and rr met? then we're done with this step */
        if (rr <= ll)
        {
            /* swap pivot to the "meeting position" */
            t = *p;
            *p = *ll;
            *ll = t;

            /* sort partitions recursively */
            quicksort_rec(l, ll-1);
            quicksort_rec(ll+1, r);

            /* done */
            return;
        }

        /* swap greater element on the left with smaller element on the right */
        t = *rr;
        *rr = *ll;
        *ll = t;
    }
}

static void quicksort(int *v, int num)
{
    quicksort_rec(v, v+num-1);
}


int main()
{
    char buf[64];       /* buffer for user input */
    int *values;        /* pointer to dynamically allocated array */
    int avail;          /* number of currently free slots in the array */
    int entries = 0;    /* number of total entries in the array */
    int i;              /* iterating variable */

    puts("Quicksort Example\n"
         "=================\n"
         "\n"
         "Enter whole numbers to sort, just hitting enter starts sorting.\n");

    /* allocate first chunk of memory */
    values = malloc(CHUNK_SIZE * sizeof(int));

    if (!values)
    {
        perror("malloc");
        exit(1);
    }
    avail = CHUNK_SIZE;

    while (1)
    {
        fputs("Please enter next number: ", stdout);

        /* try reading user input, if impossible, break */
        if (!fgets(buf, 64, stdin)) break;

        /* if input is empty, break */
        if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n') break;

        /* convert input to integer as next value for array */
        values[entries] = atoi(buf);
        printf("Added `%d' to sort list\n", values[entries]);
        ++entries;

        /* check whether there is space left in the array */
        if (!--avail)
        {
            /* if not, increase array size by the next chunk */
            values = realloc(values, (entries + CHUNK_SIZE) * sizeof(int));
            if (!values)
            {
                perror("realloc");
                exit(1);
            }

            /* reset available slots to size of chunk */
            avail = CHUNK_SIZE;
        }
    }

    printf("Now sorting %d elements with quicksort.\n", entries);

    /* sort the array */
    quicksort(values, entries);

    puts("Result:");

    for (i = 0; i < entries; ++i)
    {
        printf("%d\n", values[i]);
    }

    free(values);
    return 0;
}