简体   繁体   English

C 插入排序不是从小到大排序

[英]C insertion sort not sorting from least to greatest

I have an insertion sort that was working when it sorted integers, however I had to change it to sort an array of structs for a different project.我有一个插入排序,它在对整数进行排序时可以正常工作,但是我不得不更改它以对不同项目的结构数组进行排序。 The array is 10 people's names and ages.数组是10个人的名字和年龄。 The array is first passed to a sorting function and then a for loop is set to iterate through the array.该数组首先传递给一个排序 function 然后设置一个 for 循环来遍历该数组。 I think the issue I am having is the array is not looping correctly because the print out of the new array is in a mixed up order still.我认为我遇到的问题是数组没有正确循环,因为新数组的打印顺序仍然是混乱的。 Here is the code I have...这是我的代码......

#include <stdio.h>

#define SWAP(x,y) do {typeof(x) SWAP = x; x=y; y=SWAP;} while (0)


typedef struct Person{
        int age;
        char name[10];

}Person;

//-----------------------------

void sort(Person* arr,int n){

    int i, x, y;
    for (i=1;i<n;i++){
        x=arr[i].age;
        y = i-1;

        while(y>=0 && arr[y].age > x){
                arr[y+1]=arr[y];
                y=y-1;
        }
        SWAP(arr[y+1],arr[i]);
    }
}

int main(){

    struct Person p1 = {26, "devin"};
    struct Person p2 = {28, "bob"};
    struct Person p3 = {22, "derric"};
    struct Person p4 = {20, "amy"};
    struct Person p5 = {19, "melvin"};
    struct Person p6 = {17, "ashley"};
    struct Person p7 = {31, "haley"};
    struct Person p8 = {32, "larry"};
    struct Person p9 = {24, "ben"};
    struct Person p10 = {40, "bobby"};


    struct Person arr[] = {p1,p2,p3,p4,p5,p6,p7,p8,p9,p10};

    int n = sizeof(arr)/sizeof(arr[0]);

    printf("--Array Before Sort--\n");
    print(arr, n);

    printf("--Array After Sort--\n");
    sort(arr,n);

    //printf("\n--Sorted Array--\n");
    print(arr,n);
}

The code is missing the print function because I believe that is working properly and I'm trying to condense what I need help with.该代码缺少打印 function,因为我相信它工作正常并且我正在尝试压缩我需要帮助的内容。 I believe the issue is in the for and while loops in the sort function. Here is the output...我认为问题出在排序 function 中的 for 和 while 循环中。这是 output...

--Array Before Sort--
26 devin, 28 bob, 22 derric, 20 amy, 19 melvin, 17 ashley, 31 haley, 32 larry, 24 ben, 40 bobby, 
--Array After Sort--
32 larry, 28 bob, 28 bob, 28 bob, 28 bob, 26 devin, 28 bob, 31 haley, 32 larry, 40 bobby, 

The names are supposed to be sorted from youngest to oldest.这些名字应该按照从小到大的顺序排列。 Any ideas?有任何想法吗?

You just need to make a few small changes to sort function:你只需要做一些小的改变来排序 function:

void sort(Person* arr, int n)
{
    int i, y;
    for (i = 1; i < n; i++){
        Person x = arr[i];
        y = i - 1;

        while (y >= 0 && arr[y].age > x.age){
                arr[y + 1] = arr[y];
                y = y - 1;
        }
        SWAP(arr[y + 1], x);
    }
}

I would also replace SWAP(arr[y+1], x);我也会替换SWAP(arr[y+1], x); with just arr[y+1] = x;只有arr[y+1] = x; because it makes code more readable and it probably is better for overall performance.因为它使代码更具可读性,并且可能对整体性能更好。

Instead of erroneously writing a sorting function, I'd advise spending time learning how to use tested standard library functions such as qsort() .我建议不要错误地编写排序 function,而是花时间学习如何使用经过测试的标准库函数,例如qsort() The exercise of traversing arrays and swapping elements is all-well-and-good, but you'll soon bang into other frustrations that needlessly burn up precious hours.遍历 arrays 和交换元素的练习很好,但是您很快就会遇到其他挫折,这些挫折会不必要地浪费宝贵的时间。

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

typedef struct {
    int age;
    char name[10];
} Person_t;

void print( Person_t p[], size_t n ) {
    for( size_t i = 0; i < n; i++ )
        printf( "#%d: %s %d\n", i, p[i].name, p[i].age );
}

int cmpAge( const void *p1, const void *p2) {
    return ((Person_t*)p1)->age - ((Person_t*)p2)->age;
}

int cmpNam( const void *p1, const void *p2) {
    return strcmp( ((Person_t*)p1)->name, ((Person_t*)p2)->name );
}

int main(){
    Person_t arr[] = {
        {26, "devin"},
        {28, "bob"},
        {22, "derric"},
        {20, "amy"},
        {19, "melvin"},
        {17, "ashley"},
        {31, "haley"},
        {32, "larry"},
        {24, "ben"},
        {40, "bobby"},
    };
    int n = sizeof arr/sizeof arr[0];

    printf("--Array Before Sort--\n");
    print(arr, n);

    qsort( arr, n, sizeof arr[0], cmpAge );
    printf("--Array After age Sort--\n");
    print(arr,n);

    qsort( arr, n, sizeof arr[0], cmpNam );
    printf("--Array After name Sort--\n");
    print(arr,n);

    return 0;
}

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

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