我们讨论过 数组的插入排序 .在本文中,我们将讨论链表的插入排序。 下面是一个简单的链表插入排序算法。
null
1) Create an empty sorted (or result) list2) Traverse the given list, do following for every node.......a) Insert current node in sorted way in sorted or result list.3) Change head of given linked list to head of sorted (or result) list.
主要步骤是(2.a),这已在下面的帖子中介绍。 单链表的排序插入
下面是上述算法的实现
C
// C program to sort link list // using insertion sort #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; struct node* head = NULL; struct node* sorted = NULL; void push( int val) { /* allocate node */ struct node* newnode = ( struct node*) malloc ( sizeof ( struct node)); newnode->data = val; /* link the old list off the new node */ newnode->next = head; /* move the head to point to the new node */ head = newnode; } /* * function to insert a new_node in a list. Note that * this function expects a pointer to head_ref as this * can modify the head of the input linked list * (similar to push()) */ void sortedInsert( struct node* newnode) { /* Special case for the head end */ if (sorted == NULL || sorted->data >= newnode->data) { newnode->next = sorted; sorted = newnode; } else { struct node* current = sorted; /* Locate the node before the point of insertion */ while (current->next != NULL && current->next->data < newnode->data) { current = current->next; } newnode->next = current->next; current->next = newnode; } } // function to sort a singly linked list // using insertion sort void insertionsort() { struct node* current = head; // Traverse the given linked list and insert every // node to sorted while (current != NULL) { // Store next for next iteration struct node* next = current->next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head to point to sorted linked list head = sorted; } /* Function to print linked list */ void printlist( struct node* head) { while (head != NULL) { printf ( "%d->" , head->data); head = head->next; } printf ( "NULL" ); } // Driver program to test above functions int main() { push(5); push(20); push(4); push(3); push(30); printf ( "Linked List before sorting:" ); printlist(head); printf ( "" ); insertionsort(head); printf ( "Linked List after sorting:" ); printlist(head); } // This code is contributed by Sornodeep Chandra |
C++
// C++ program to sort link list // using insertion sort #include <bits/stdc++.h> using namespace std; struct Node { int val; struct Node* next; Node( int x) { val = x; next = NULL; } }; class LinkedlistIS { public : Node* head; Node* sorted; void push( int val) { /* allocate node */ Node* newnode = new Node(val); /* link the old list off the new node */ newnode->next = head; /* move the head to point to the new node */ head = newnode; } // function to sort a singly linked list using insertion // sort void insertionSort(Node* headref) { // Initialize sorted linked list sorted = NULL; Node* current = headref; // Traverse the given linked list and insert every // node to sorted while (current != NULL) { // Store next for next iteration Node* next = current->next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to sorted linked list head = sorted; } /* * function to insert a new_node in a list. Note that * this function expects a pointer to head_ref as this * can modify the head of the input linked list * (similar to push()) */ void sortedInsert(Node* newnode) { /* Special case for the head end */ if (sorted == NULL || sorted->val >= newnode->val) { newnode->next = sorted; sorted = newnode; } else { Node* current = sorted; /* Locate the node before the point of insertion */ while (current->next != NULL && current->next->val < newnode->val) { current = current->next; } newnode->next = current->next; current->next = newnode; } } /* Function to print linked list */ void printlist(Node* head) { while (head != NULL) { cout << head->val << " " ; head = head->next; } } }; // Driver program to test above functions int main() { LinkedlistIS list; list.head = NULL; list.push(5); list.push(20); list.push(4); list.push(3); list.push(30); cout << "Linked List before sorting" << endl; list.printlist(list.head); cout << endl; list.insertionSort(list.head); cout << "Linked List After sorting" << endl; list.printlist(list.head); } // This code is contributed by nirajgusain5 |
JAVA
// Java program to sort link list // using insertion sort public class LinkedlistIS { node head; node sorted; class node { int val; node next; public node( int val) { this .val = val; } } void push( int val) { /* allocate node */ node newnode = new node(val); /* link the old list off the new node */ newnode.next = head; /* move the head to point to the new node */ head = newnode; } // function to sort a singly linked list using insertion sort void insertionSort(node headref) { // Initialize sorted linked list sorted = null ; node current = headref; // Traverse the given linked list and insert every // node to sorted while (current != null ) { // Store next for next iteration node next = current.next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to sorted linked list head = sorted; } /* * function to insert a new_node in a list. Note that * this function expects a pointer to head_ref as this * can modify the head of the input linked list * (similar to push()) */ void sortedInsert(node newnode) { /* Special case for the head end */ if (sorted == null || sorted.val >= newnode.val) { newnode.next = sorted; sorted = newnode; } else { node current = sorted; /* Locate the node before the point of insertion */ while (current.next != null && current.next.val < newnode.val) { current = current.next; } newnode.next = current.next; current.next = newnode; } } /* Function to print linked list */ void printlist(node head) { while (head != null ) { System.out.print(head.val + " " ); head = head.next; } } // Driver program to test above functions public static void main(String[] args) { LinkedlistIS list = new LinkedlistIS(); list.push( 5 ); list.push( 20 ); list.push( 4 ); list.push( 3 ); list.push( 30 ); System.out.println( "Linked List before Sorting.." ); list.printlist(list.head); list.insertionSort(list.head); System.out.println( "LinkedList After sorting" ); list.printlist(list.head); } } // This code is contributed by Rishabh Mahrsee |
python
# Python implementation of above algorithm # Node class class Node: # Constructor to initialize the node object def __init__( self , data): self .data = data self . next = None # function to sort a singly linked list using insertion sort def insertionSort(head_ref): # Initialize sorted linked list sorted = None # Traverse the given linked list and insert every # node to sorted current = head_ref while (current ! = None ): # Store next for next iteration next = current. next # insert current in sorted linked list sorted = sortedInsert( sorted , current) # Update current current = next # Update head_ref to point to sorted linked list head_ref = sorted return head_ref # function to insert a new_node in a list. Note that this # function expects a pointer to head_ref as this can modify the # head of the input linked list (similar to push()) def sortedInsert(head_ref, new_node): current = None # Special case for the head end */ if (head_ref = = None or (head_ref).data > = new_node.data): new_node. next = head_ref head_ref = new_node else : # Locate the node before the point of insertion current = head_ref while (current. next ! = None and current. next .data < new_node.data): current = current. next new_node. next = current. next current. next = new_node return head_ref # BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert # Function to print linked list */ def printList(head): temp = head while (temp ! = None ): print ( temp.data, end = " " ) temp = temp. next # A utility function to insert a node # at the beginning of linked list def push( head_ref, new_data): # allocate node new_node = Node( 0 ) # put in the data new_node.data = new_data # link the old list off the new node new_node. next = (head_ref) # move the head to point to the new node (head_ref) = new_node return head_ref # Driver program to test above functions a = None a = push(a, 5 ) a = push(a, 20 ) a = push(a, 4 ) a = push(a, 3 ) a = push(a, 30 ) print ( "Linked List before sorting " ) printList(a) a = insertionSort(a) print ( "Linked List after sorting " ) printList(a) # This code is contributed by Arnab Kundu |
C#
// C# program to sort link list // using insertion sort using System; public class LinkedlistIS { public node head; public node sorted; public class node { public int val; public node next; public node( int val) { this .val = val; } } void push( int val) { /* allocate node */ node newnode = new node(val); /* link the old list off the new node */ newnode.next = head; /* move the head to point to the new node */ head = newnode; } // function to sort a singly // linked list using insertion sort void insertionSort(node headref) { // Initialize sorted linked list sorted = null ; node current = headref; // Traverse the given // linked list and insert every // node to sorted while (current != null ) { // Store next for next iteration node next = current.next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to sorted linked list head = sorted; } /* * function to insert a new_node in a list. Note that * this function expects a pointer to head_ref as this * can modify the head of the input linked list * (similar to push()) */ void sortedInsert(node newnode) { /* Special case for the head end */ if (sorted == null || sorted.val >= newnode.val) { newnode.next = sorted; sorted = newnode; } else { node current = sorted; /* Locate the node before the point of insertion */ while (current.next != null && current.next.val < newnode.val) { current = current.next; } newnode.next = current.next; current.next = newnode; } } /* Function to print linked list */ void printlist(node head) { while (head != null ) { Console.Write(head.val + " " ); head = head.next; } } // Driver code public static void Main(String[] args) { LinkedlistIS list = new LinkedlistIS(); list.push(5); list.push(20); list.push(4); list.push(3); list.push(30); Console.WriteLine( "Linked List before Sorting.." ); list.printlist(list.head); list.insertionSort(list.head); Console.WriteLine( "LinkedList After sorting" ); list.printlist(list.head); } } // This code contributed by Rajput-Ji |
Javascript
<script> // javascript program to sort link list // using insertion sort var head = null ; var sorted = null ; class node { constructor(val) { this .val = val; this .next = null ; } } function push(val) { /* allocate node */ var newnode = new node(val); /* link the old list off the new node */ newnode.next = head; /* move the head to point to the new node */ head = newnode; } // function to sort a singly linked list using insertion sort function insertionSort( headref) { // Initialize sorted linked list var sorted = null ; var current = headref; // Traverse the given linked list and insert every // node to sorted while (current != null ) { // Store next for next iteration var next = current.next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to sorted linked list head = sorted; } /* * function to insert a new_node in a Note that this function expects a * pointer to head_ref as this can modify the head of the input linked list * (similar to push()) */ function sortedInsert( newnode) { /* Special case for the head end */ if (sorted == null || sorted.val >= newnode.val) { newnode.next = sorted; sorted = newnode; } else { var current = sorted; /* Locate the node before the point of insertion */ while (current.next != null && current.next.val < newnode.val) { current = current.next; } newnode.next = current.next; current.next = newnode; } } /* Function to print linked list */ function printlist( head) { while (head != null ) { document.write(head.val + " " ); head = head.next; } } // Driver program to test above functions push(5); push(20); push(4); push(3); push(30); document.write( "Linked List before Sorting..<br/>" ); printlist(head); insertionSort(head); document.write( "<br/>LinkedList After sorting<br/>" ); printlist(sorted); // This code contributed by aashish1995 </script> |
输出:
Linked List before sorting30 3 4 20 5Linked List after sorting3 4 5 20 30
时间和空间复杂性分析:
在最坏的情况下,我们可能必须遍历排序列表中的所有节点才能插入节点。有n个这样的节点。
因此,时间复杂性: O(n)*O(n)=O(n^2)
空间复杂度:根据输入的大小,不需要额外的空间。因此,空间复杂性是恒定的- O(1)。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END