简体   繁体   English

如何在链表上执行选择排序?

[英]How to execute a Selection Sort on a linked list?

I am trying to implement a selection sort on linked list. 我正在尝试对链表进行选择排序。 I want it to be performed directly on the linked list, not a copy, using node pointers instead of the array index method that I have pasted here: 我希望使用节点指针而不是我在此处粘贴的数组索引方法直接在链表(而不是副本)上执行:

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;
}
}

How would I tweak this function to accept my linked list? 我将如何调整此功能以接受我的链表? and sort it? 排序吗? I also don't want to use any sort of STL container. 我也不想使用任何STL容器。

Say the list is { 90, 13, 5, 12 }. 假设列表为{90,13,5,12}。 Start a pointer at the beginning. 在开始处启动一个指针。

{ * 90, 13, 5, 12 } { * 90、13、5、12}

Find the smallest member after the pointer and move it just before the pointer. 在指针之后找到最小的成员,并将其移动到指针之前。

{ 5, * 90, 13, 12 } {5, * 90,13,12}

Find the smallest member after the pointer and move it just before the pointer. 在指针之后找到最小的成员,并将其移动到指针之前。

{ 5, 12, * 90, 13 } {5,12, * 90,13}

Again. 再次。

{ 5, 12, 13, * 90 } {5,12,13, * 90}

Again. 再次。

{ 5, 12, 13, 90 } {5,12,13,90}

Pointer runs off the end of the list, we're done, list is sorted. 指针从列表末尾运行,我们完成了,列表已排序。

Here is C++ implementation of selection sort on linked list without STL. 这是不带STL的链表上选择排序的C ++实现。 To ease testing of program for different cases , this program create linked list of given size with random numbers. 为了简化程序在不同情况下的测试,该程序使用随机数创建给定大小的链表。

    //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;
            }

    }

For more details visit- https://github.com/SahdevKansal02/Data-Structures-And-Algorithms.git 有关更多详细信息,请访问-https ://github.com/SahdevKansal02/Data-Structures-And-Algorithms.git

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

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