C++ 在单链表上使用插入排序
所以我有一个赋值,在这里我给出了一个随机的数字列表,我需要使用插入排序对它们进行排序。我必须使用单链接列表。我环顾了一下其他的帖子,但似乎都没有帮助。我知道插入排序是什么,但我只是不知道如何用代码编写它C++ 在单链表上使用插入排序,c++,list,sorting,linked-list,insertion-sort,C++,List,Sorting,Linked List,Insertion Sort,所以我有一个赋值,在这里我给出了一个随机的数字列表,我需要使用插入排序对它们进行排序。我必须使用单链接列表。我环顾了一下其他的帖子,但似乎都没有帮助。我知道插入排序是什么,但我只是不知道如何用代码编写它 Node* insertion_sort(Node* head) { Node* temp = head_ptr; while((head->n < temp->n) && (temp != NULL)) temp = temp->next
Node* insertion_sort(Node* head) {
Node* temp = head_ptr;
while((head->n < temp->n) && (temp != NULL))
temp = temp->next;
head->next = temp->next;
temp->next = head;
head->prev = temp;
}
节点*插入\排序(节点*头){
节点*温度=头部温度;
while((head->nn)和&(temp!=NULL))
温度=温度->下一步;
头部->下一步=临时->下一步;
温度->下一步=头部;
压头->上一个=温度;
}
我不知道这是否正确,或者现在该怎么做,想想看——如果列表是空的,
temp
最初将是NULL
,所以当你做temp->next=head时,你会得到未定义的行为代码>
尝试一些调试,它肯定会有帮助。您可能还想保留上一个节点,以便以后插入,或者向前看两个节点。让我们考虑一下插入排序的工作原理:它将列表“拆分”(理论上)为三组:已排序的子集(可能为空)、当前项和未排序的子集(可能为空)。排序当前项之前的所有内容。当前项之后的所有内容都可以排序,也可以不排序。算法检查当前项,并将其与下一项进行比较。请记住,当前项之后的第一项属于未排序的子集
让我们假设您正在按递增顺序对整数排序(因此给定“3,1,5,2,4”,您希望得到“1,2,3,4,5”)。将当前项设置为列表中的第一项。现在开始排序:
如果下一项大于当前项,则不需要对该项进行排序。只需将其设置为“当前项”并继续
如果下一项小于当前项,则您有一些工作要做。首先,将下一个项目保存在某个地方(比如在一个名为temp的指针中),然后通过设置current->next=current->next->next从列表中“删除”下一个项目。现在,您需要为移除的项目找到正确的位置。您可以通过两种方式执行此操作:
或者从列表的开头开始,一直向前,直到找到正确的位置。一旦您这样做了,您就可以在那里插入项目并继续插入排序。如果你有一个单链表,这是最简单的解决方案
你向后走,直到找到物品的正确位置。一旦您这样做了,您就可以在那里插入项目并继续插入排序。这有点复杂,但如果您有一个双链接列表,它可以很好地工作
继续此过程,直到到达列表的末尾。到达后,您就知道已完成插入排序,并且列表的排序顺序正确
我希望这能有所帮助。作废链接列表::插入排序(){
struct node {
int data;
struct node *next;
};
void insertion(struct node **head) {
if((*head)== NULL || (*head)->next == NULL) {
return;
}
struct node *t1 = (*head)->next;
while(t1 != NULL) {
int sec_data = t1->data;
int found = 0;
struct node *t2 = *head;
while(t2 != t1) {
if(t2->data > t1->data && found == 0) {
sec_data = t2->data;
t2->data = t1->data;
found = 1;
t2 = t2->next;
} else {
if(found == 1) {
int temp = sec_data;
sec_data = t2->data;
t2->data = temp;
}
t2 = t2->next;
}
}
t2->data = sec_data;
t1 = t1->next;
}
}
节点*p=头部;
node*currentNode=head->next;//当前正在比较的节点。
node*previousNode=head;//当前正在比较的节点之前的节点。
//如果链表的长度小于2,我们可以从排序返回。
如果(p==nullptr | | p->next==nullptr){
返回;
}
while(currentNode!=nullptr){
//如果当前节点大于或等于左侧已排序链表的最大元素,则可以移动到下一个元素。
//对于已排序的数组很有用。
如果(上一个节点->值){
currentNode=currentNode->next;
previousNode=previousNode->next;
}
否则{
//如果元素比头部元素小,我们需要注意头部元素。
如果(头->值->当前节点->值){
上一个节点->下一个=当前节点->下一个;
当前节点->下一步=头部;
head=当前节点;
}否则{
p=水头;
而(p->next!=NULL&&p->next->valuevalue){
p=p->next;
}
上一个节点->下一个=当前节点->下一个;
currentNode->next=p->next;
p->next=currentNode;
}
}
currentNode=previousNode->next;
}
}
以下是链表插入排序的Java实现:
- 时间复杂度:O(n^2)
- 空间复杂度:O(1)-插入排序是就地排序算法
类解决方案
{
公共ListNode insertionSortList(ListNode头)
{
//初始化部分排序的列表
ListNode虚拟=新ListNode(0),prev=虚拟,current=头部;
while(当前!=null)
{
如果(上一个值>当前值)
prev=虚拟;
//找到插入当前节点的正确位置
while(prev.next!=null&&prev.next.val
哇,我肯定晚会迟到了,是吗?不管怎样,我想在回答这个问题时投入我的两分钱。根据您提供示例代码的方式,我将假设如下:
您没有使用OOP原则
您已经正确设置了单链接列表(我将其称为转发列表)
您希望用户在调用实现函数时使用运算符“=”
首先,我要给你们的答案使用了我喜欢称之为双指针技术的东西!您可以使用双指针来帮助您同时引用两个变量,以便在到达nullptr时避免复杂的测试用例
好吧,好吧。。。我在输的时候打得很快,把指针指向双指针,我可能在拉你
void linked_list::insertion_sort() {
node * p = head;
node * currentNode = head->next; // The node that is being compared at the moment.
node * previousNode = head; // The node previous to the node being compared at the moment.
//We can return from the sorting if the length of the linked list is less than 2.
if (p == nullptr || p->next == nullptr) {
return;
}
while (currentNode != nullptr) {
//If the current node is larger than or equal to the largest element of the sorted linked list on the left, we can move to the next element.
//Helpful for an already sorted array.
if(previousNode->value<=currentNode->value){
currentNode = currentNode->next;
previousNode = previousNode->next;
}
else{
//If the element is the smaller than the head element we need to take care of the head element.
if (head->value > currentNode->value) {
previousNode->next = currentNode->next;
currentNode->next = head;
head = currentNode;
}else {
p = head;
while (p->next != NULL && p->next->value < currentNode->value) {
p = p->next;
}
previousNode->next = currentNode->next;
currentNode->next = p->next;
p->next = currentNode;
}
}
currentNode = previousNode->next;
}
}
class Solution
{
public ListNode insertionSortList(ListNode head)
{
// Initialize partially sorted list
ListNode dummy = new ListNode(0), prev = dummy, current = head;
while(current != null)
{
if(prev.val > current.val)
prev = dummy;
// Find the right place to insert current node
while(prev.next != null && prev.next.val < current.val)
prev = prev.next;
// Insert current between prev and prev.next
ListNode nextNode = current.next;
current.next = prev.next;
prev.next = current;
current = nextNode;
}
return dummy.next;
}
}
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
};
class list{
node* head;
public:
list() {
head=NULL;
}
void add(int el) {
node* newNode=new node();
newNode->data=el;
newNode->next=NULL;
if(head==NULL) {
head=newNode;
} else {
node* temp=head;
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=newNode;
}
}
//Insertion sort code
void insertionSort() {
node* i=head->next;
while (i!=NULL)
{
node* key=i;
node* j=head;
while (j!=i)
{
if (key->data<j->data)
{
int temp=key->data;
key->data=j->data;
j->data=temp;
}
j=j->next;
}
i=i->next;
}
}
void display() {
node* temp=head;
while (temp!=NULL) {
cout<<temp->data<<" ";
temp=temp->next;
}
}
};
int main()
{
list l;
l.add(2);
l.add(6);
l.add(0);
l.add(3);
l.add(7);
l.add(4);
l.insertionSort();
l.display();
}