C++ 如何在链表上执行选择排序?
我正在尝试在链表上实现选择排序。我希望它直接在链表上执行,而不是在副本上执行,使用节点指针,而不是我粘贴在此处的数组索引方法:C++ 如何在链表上执行选择排序?,c++,sorting,linked-list,selection,C++,Sorting,Linked List,Selection,我正在尝试在链表上实现选择排序。我希望它直接在链表上执行,而不是在副本上执行,使用节点指针,而不是我粘贴在此处的数组索引方法: void listClass::selectionsort(int array[], int size) { int startscan, minIndex, minValue; for (startscan = 0; startscan < (size - 1); startscan++) { minIndex = startscan;
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;
}
}
void listClass::selectionsort(int数组[],int大小)
{
int startscan、minIndex、minValue;
对于(startscan=0;startscan<(大小-1);startscan++)
{
minIndex=startscan;
minValue=数组[startscan];
对于(int index=startscan+1;index
如何调整此函数以接受我的链接列表?然后分类?我也不想使用任何类型的STL容器。假设列表是{90,13,5,12}。从开头开始一个指针 {*90,13,5,12} 找到指针后的最小成员,并将其移动到指针前 {5,*90,13,12} 找到指针后的最小成员,并将其移动到指针前 {5,12,*90,13} 再说一遍 {5,12,13,*90} 再说一遍 {5,12,13,90}
指针从列表的末尾开始,我们完成了,列表被排序。
< P>这里是在没有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;
}
}
//使用选择排序对链表进行排序的程序。
#包括
#包括
#包括
#定义大小10//链表的大小。
结构节点
{
国际信息;
结构节点*下一步;
}*start=NULL;
类型定义结构节点*节点;
int main()
{
int i;
节点ptr;
void selectionsort(节点);
srand(时间(空));
对于(i=0;iinfo=rand()%100;//将创建给定大小的随机链表。
ptr->next=NULL;
if(start==NULL)
{
启动=ptr;
}
其他的
{temp=开始;
while(临时->下一步!=NULL)
温度=温度->下一步;
温度->下一步=ptr;
}
}
printf(“排序前的链表为-\n”);
ptr=启动;
while(ptr!=NULL)
{
printf(“%d”,ptr->info);
ptr=ptr->next;
}
printf(“\n排序后的链表为-\n”);
选择排序(开始);
ptr=启动;
while(ptr!=NULL)
{
printf(“%d”,ptr->info);
ptr=ptr->next;
}
返回0;
}
void selectionsort(节点开始)
{
节点ptr=开始、温度、地址;
int-var;
while(ptr->next!=NULL)
{
温度=ptr;
地址=临时;
while(temp!=NULL)
{
如果((地址->信息)>(临时->信息))
地址=临时;
温度=温度->下一步;
}
var=地址->信息;
地址->信息=ptr->信息;
ptr->info=var;
ptr=ptr->next;
}
}
欲了解更多详情,请访问-
否定的,我正在为一个软件工程实习做实践面试问题