English 中文(简体)
C区的Quicksort, 配有护林套餐
原标题:Quicksort in C, with hoare partition
#include<stdio.h>
#include <time.h>
#include <stdlib.h> 

int w, q, p, r;
int tab[100];

void main ()
{
    int i;
    srand(time(0));
    for (i = 0; i < 100; i += 1)
    {
        tab[i]=rand()%100;
    }
    display(tab);
    r = 37;
    quicksnort(tab, 0, r-1);
    display(tab);
}


int display (int tab[])
{
    int i;
    printf("
 Your numbers : 
");
    for (i = 0; i < 100; i += 1)
    {
        printf(" %d", tab[i]);
    }
}


int quicksnort(int tab[], int m, int n)
{

    if (p<r)
    {
        q = partition(tab, m, n);
        quicksnort(tab, m, q-1);
        quicksnort(tab, q+1, n);
    }
}

int partition(int tab[], int p, int r)
{
    int x, i, j, part;
    x = tab[p];
    i = p-1;
    j = r+1;
    do
    {
        do
        {
            j = j-1;    
        } while (tab[j]<=x && j>=0); 
        do
        {
            i = i+1;    
        } while (tab[i]>=x && i<=0);
        if (i<j)
        {
            part = tab[i];
            tab[i]=tab[j];
            tab[j]=part;
        }
        else
        {
            return j;
        }
    } while (1);
}

Hi, i have problem with the code above. It will compile but when i run it, it stops and display some sort of alert of "core dump". It s based on hoare version of quicksort, to be axact on that peudocode http://screenshooter.net/5359896/jyuogoj I ve tried everything to make it work, i think that it might have something to do with pointers. I think that because i m not sure how they work in C.

(OK,我知道他们指的是细胞之类的东西, 但我迷路了, 指针指针, 或用于函数的指针, 或用于函数的指针, 或用于函数的指针表, 以及类似的东西。 我真的不知道该从何处寻找更具体的信息 )

但也许解决办法比这简单些。

问题回答

以调试器运行并查看。 以 < code> cc - g 编译, 这样它与调试器兼容。 Google < code> dbx cheatshall 来学习基本命令。 学习基本调试器使用和分离错误的时间会比撰写文章花费的时间少, 这就是为什么它是一个将时间投入到学习的好工具 。

你的代码中有些错误...

p,r,q 在使用前没有定义 。

我很惊讶你没有收到任何警告 当你编译。

我认为正在发生的事情是,由于无效的值(p,r,q.等),你正在无限地循环,而你的操作系统最终用完了内存来给程序。

另外, 请将您的代码格式化得更好, 这样更容易阅读 。 每个子条款或语句都使用嵌套结构和 8 个空格或 Tab 。

我认为在 parttion 中的循环设置了 < i , 而不是递增。

您想要将 i j 移动到相反方向,但您却将其两者都减少。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签