简体   繁体   English

使用 strcmp 在 C 中按字母顺序排序

[英]Sorting alphabetically in C using strcmp

I am trying to sort records (structs) by their names.我正在尝试按名称对记录(结构)进行排序。 And I'm using strcmp to swap to detect the alphabetical order.我正在使用 strcmp 进行交换以检测字母顺序。 The swapping works fine, however it doesn't always sort the entire list.交换工作正常,但它并不总是对整个列表进行排序。 It always leaves some records in thier incorrect alphabetic order.它总是以不正确的字母顺序留下一些记录。

void sort_by_name(){
    printf("%d\n", counter);

    int i=0, j=0;
    patient *temp;

    for(;j<=counter;j++){
        for(;i<counter-1;i++){

            if(strcmp(pRecords[i]->name, pRecords[i+1]->name) > 0){

                temp = pRecords[i];
                pRecords[i] = pRecords[i+1];
                pRecords[i+1]=temp;

            }//if loops

        }//2nd for loop

    }//1st for loop

}


counter-- number of records in the system.

I think your specific problem is that you're not re-initialising i to 0 each time you start the inner loop.我认为您的具体问题是每次启动内部循环时都没有将i重新初始化为 0 。

This is a relatively simple bubblesort where the inner loop runs over the list of N items, N times (controlled by the outer loop) to get them sorted.这是一个相对简单的冒泡排序,其中内部循环在N个项目的列表上运行N次(由外部循环控制)以对它们进行排序。 But your inner loop runs over them once then seems to go off elsewhere, beyond the end of the array.但是你的内部循环运行一次然后似乎 go 在其他地方,超出数组的末尾。 That's unlikely to provide useful results:-)这不太可能提供有用的结果:-)

Your more general issue is: why are you not using the language facilities to do this, specifically qsort ?您更普遍的问题是:您为什么不使用语言工具来执行此操作,特别是qsort If you're trying to educate yourself on sorting, that's fine, but if you just want to sort, there's little point re-inventing the wheel.如果你想自学排序,那很好,但如果你只是想排序,那么重新发明轮子就没有什么意义了。

For what it's worth, I prefer the following algorithm in bubble sort.对于它的价值,我更喜欢冒泡排序中的以下算法。 It stops early if the list is sorted and it doesn't look at already sorted items:如果列表已排序并且它不查看已排序的项目,它会提前停止:

void sort_by_name(){
    int i, didSwap = 1, limit = numitems - 1;
    patient *temp;

    while (didSwap) {
        didSwap = 0;
        for (i = 0; i < limit; i++) {
            if (strcmp (pRecords[i]->name, pRecords[i+1]->name) > 0) {
                temp          = pRecords[i];
                pRecords[i]   = pRecords[i+1];
                pRecords[i+1] = temp;
                didSwap = 1;
            }
        }
        limit--;
    }
}

The didSwap variable controls when to exit. didSwap变量控制何时退出。 If you traverse the entire unsorted section without swapping then obviously, you're finished.如果您遍历整个未排序的部分而不进行交换,那么显然,您就完成了。 The limit controls that unsorted section and reduces gradually since, after each pass, there's a greater already-sorted section at the end of the list (the higher elements bubble up to the end of the list, one per pass). limit控制未排序的部分并逐渐减少,因为在每次遍历之后,列表末尾有一个更大的已排序部分(较高的元素冒泡到列表的末尾,每次遍历一个)。

I think you are trying to implement bubble sort.我认为您正在尝试实现冒泡排序。 The loop count variables seem to be a bit off.循环计数变量似乎有点偏离。

        for(;i<counter-1;i++){

should be应该

        for(i=counter-2;i>=j; i--){

You must reset the i after every iteration of the 'j' loop.您必须在“j”循环的每次迭代后重置 i。 And work backwards to have the smallest number bubble up to the front.并向后工作以使最小的数字冒泡到前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM