【问题标题】:How to execute a Selection Sort on a linked list?如何对链表执行选择排序?
【发布时间】:2019-01-03 03:20:17
【问题描述】:

我正在尝试在链表上实现选择排序。我希望它直接在链表上执行,而不是复制,使用节点指针而不是我在这里粘贴的数组索引方法:

void listClass::selectionsort(int array[], int size)
{
    int startscan, minIndex, minValue;

for (startscan = 0; startscan < (size - 1); startscan++) 
{
    minIndex = startscan;
    minValue = array[startscan];
    for (int index = startscan + 1; index < size; index++) 
    {
        if (array[index] < minValue)
        {
            minValue = array[index];
            minIndex = index;
        }
    }
    array[minIndex] = array[startscan];
    array[startscan] = minValue;
}
}

如何调整这个函数来接受我的链表?并排序?我也不想使用任何类型的 STL 容器。

【问题讨论】:

  • 否定,我正在为软件工程实习准备面试练习题

标签: c++ sorting linked-list selection


【解决方案1】:

假设列表是 { 90, 13, 5, 12 }。以指针开头。

{ * 90, 13, 5, 12 }

在指针之后找到最小的成员并将其移动到指针之前。

{ 5, * 90, 13, 12 }

在指针之后找到最小的成员并将其移动到指针之前。

{ 5, 12, * 90, 13 }

再次。

{ 5, 12, 13, * 90 }

再次。

{ 5, 12, 13, 90 }

指针超出列表的末尾,我们完成了,列表已排序。

【讨论】:

    【解决方案2】:

    这是没有 STL 的链表上选择排序的 C++ 实现。 为了方便对不同情况的程序进行测试,该程序使用随机数创建给定大小的链表。

        //Program to sort a linked list using selection sort.
        #include<stdio.h> 
        #include<time.h>
        #include<cstdlib>
        #define size 10 //Size of linked list.
        struct node
        {
             int info;
             struct node *next;
        }*start=NULL;
        typedef struct node * Node;
        int main()
        {
             int i;
             Node ptr;
             void selectionsort(Node);
             srand(time(NULL));
             for(i=0;i<size;i++)
             {
                  Node ptr,temp;
                  ptr=(Node)malloc(sizeof(node));
                  ptr->info=rand()%100; //Random linked list of given size is created.
                  ptr->next=NULL;
                  if(start==NULL)
                  {
                       start=ptr;
                  }
                  else
                 {     temp=start;
                       while(temp->next!=NULL)
                             temp=temp->next;
                       temp->next=ptr;
                 }          
             }
             printf(" Linked List Before Sorting Is -\n");
             ptr=start;
             while(ptr!=NULL)
             {
                  printf(" %d ",ptr->info);
                  ptr=ptr->next;
             }
             printf("\n Linked List After Sorting Is -\n");
             selectionsort(start);
             ptr=start;
             while(ptr!=NULL)
             {
                  printf(" %d ",ptr->info);
                  ptr=ptr->next;
             }
             return 0;
        }
    
        void selectionsort(Node start)
        {
                 Node ptr=start,temp,address;
                 int var;
                 while(ptr->next!=NULL)
                 {
                     temp=ptr;
                     address=temp;
                      while(temp!=NULL)
                      {
                            if((address->info)>(temp->info))
                            address=temp;
                            temp=temp->next;
                      }
    
                      var=address->info;
                      address->info=ptr->info;
                      ptr->info=var;
                      ptr=ptr->next;
                }
    
        }
    

    更多详情请访问- https://github.com/SahdevKansal02/Data-Structures-And-Algorithms.git

    【讨论】:

      猜你喜欢
      • 2015-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2016-02-21
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多