简体   繁体   English

C插入排序-实现

[英]C Insertion Sort - Implementation

I've just started programming in C and need a bit of help with my implementation of Insertion Sort. 我刚刚开始使用C进行编程,在实现插入排序时需要一些帮助。

I am doing a C list insertion sort. 我正在执行C列表插入排序。

Here is the peseudocode for it, which I want to convert into C 这是它的伪代码,我想将其转换为C

otherwise 除此以外
use a loop to find the last item in the list which should precede the new person set the new person's "next" link to point to whatever follows this list item 使用循环在列表中找到应该在新人员之前的最后一项,并将新人员的“下一个”链接设置为指向此列表项之后的所有内容

set the "next" link of this item to point to the new person 设置此项的“下一个”链接以指向新人员

return the (start of the) list 返回(开始)列表

Here is my partial implementation of my pseudocode 这是我的伪代码的部分实现

else    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

Here is my full method: 这是我的完整方法:

struct person *insert_sorted (struct person *people, char *name, int age) {
//create a new space for the new person
  struct person *pointer = malloc(sizeof(struct person));
   // check it succeeded
    if(pointer == NULL)
    { 
     printf("The program could not allocate memory ");
      exit(-1);
    }
     // set the data for the new person
      strcpy(pointer -> name, name);
      pointer -> age = age;
      pointer -> next = people;
     // if the current list is empty
      if (people == NULL)
      {
        // set the new person's "next" link to point to the current list"
    pointer -> next = people;
    // return a pointer to the new person
    return pointer;
      }
else    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

If you need the full program coude, let me know. 如果您需要完整的程序说明,​​请告诉我。

Thank you! 谢谢!

Sarah :) 莎拉 :)

before inserting the element into the list *people you should check the the correct position. 在将元素插入列表之前*人,您应该检查正确的位置。 try this: 尝试这个:

struct person *insert_sorted (struct person *people, char *name, int age) {

//create a new space for the new person
struct person *pointer = malloc(sizeof(struct person));
// check it succeeded
if(pointer == NULL)
{ 
 printf("The program could not allocate memory ");
  exit(-1);
}
 // set the data for the new person
  strcpy(pointer -> name, name);
  pointer -> age = age;
struct person *cursor = people;
struct person *previous = people;
if(people == NULL){
    pointer->next = NULL;
    return pointer;
}
while(cursor!=NULL && strcmp(pointer->name,cursor->name)<0){
    previous = cursor;
    cursor = cursor->next;
}
if(previous!=NULL)
    previous->next = pointer;
pointer->next = cursor;
return people;
}

in this way you insert the new element after the first element with a name alphabetically lower and you link it with the next element. 通过这种方式,您可以在第一个元素之后插入新元素,并将其名称按字母顺序小写,然后将其与下一个元素链接。

I've done nothing to this code beyond naming things correctly, eliminating redundant comments, and running it through indent -linux . 除了正确地命名事物,消除多余的注释并通过indent -linux运行它之外,我对这段代码没有做任何事情。

struct person *insert_sorted(struct person *people, char *name, int age)
{
        struct person *newperson = malloc(sizeof *newperson);
        if (newperson == NULL) {
                printf("The program could not allocate memory ");
                exit(-1);
        }

        strcpy(newperson->name, name);
        newperson->age = age;
        newperson->next = people;

        if (people == NULL) {
                newperson->next = people;
                return newperson;
        }

        for (int i = 0; i < HOW_MANY; i++) {
                people = people->next;
                if (people->next == NULL)
                        return people;
        }

        return newperson;
}

A couple of things pop out immediately: 有几件事会立即弹出:

  • it's not clear that you'e initializing all of *newperson . 尚不清楚您是否要初始化*newperson Safest is newperson = calloc(1, sizeof *newperson) , then readers don't even have to think about it, always a good result. 最安全的是newperson = calloc(1, sizeof *newperson) ,因此读者甚至不必考虑它,总是好结果。

  • you don't show the structure definition, but you're not checking whether the inbound name fits in the storage at newperson->name -- and if newperson->name is a pointer, you've allocated none for it at all. 您没有显示结构定义,但是您没有检查入站名称是否适合newperson-> name的存储空间,并且如果newperson-> name是指针,则根本没有分配任何指针。

  • there's a redundant assignment to newperson->next, raising the opposite question, of whether there's something not apparent or perhaps missing that makes it necessary. 对newperson-> next进行了多余的分配,提出了相反的问题,即是否有不明显的东西或可能缺少的东西使之必要。

  • I don't see where in here you're comparing key values. 我看不到您在哪里比较关键值。

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

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