
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Character Array Using Linked List
In this problem, we need to sort the given array of characters using a linked list. We can use bubble sort, selection sort, merger sort, etc. techniques to sort the array.
Here, we will convert the array into the linked list first and then use the selection sort and bubble sort techniques to sort the array.
Problem statement ? We have given an array arr[] of length N. The array contains lowercase alphabetical characters. We need to sort the array using the linked list.
Sample examples
Input
arr[] = {'e', 's', 'a', 'x', 'c', 'e','f', 'p', 'b', 'n', 'a'};
Output
a -> a -> b -> c -> e -> e -> f -> n -> p -> s -> x -> NULL
Explanation ? We have sorted the array in ascending order.
Input
arr[] = {'g', 'g', 'h', 'i', 'j', 'k', 'k', 'l', 'm', 'n', 'o'};
Output
g -> g -> h -> i -> j -> k -> k -> l -> m -> n -> o -> NULL
Explanation ? The array arr[] and output are the same as the array is already sorted.
Approach 1
In this approach, we will use the bubble sort algorithm to sort the linked list. First, we will convert the array to a linked list and then use the bubble sort technique. In the bubble sort technique, we make total N iterations for swapping all list elements that are greater than the next element.
Algorithm
Step 1 ? The sortLinkedList() function is used to sort the linked list, which takes the start node and length of the list as a parameter.
Step 2 ? Define the ?curr' node and ?swapped' boolean variable to keep track in single iterations any swapping occurred or not.
Step 3 ? Use the loop to traverse the linked list. Assign the ?start' node to ?curr' and initialize the ?swapped' with false.
Step 4 ? Now, use another nested loop to make iterations until len ? p ?1. Define ?temp1' and ?temp2' variables and initialize them with the ?curr' node and its next node, respectively.
Step 5 ? If temp1 ?> ch is greater than temp2 ?> ch, swap them and update the value of the ?curr' node and ?swapped' variable.
Step 6 ? Move ?curr' to the next node.
Step 7 ? If ?swapped == false' and not updated while making the iterations using a nested loop, break the outer loop as the list is already sorted.
Example
In this example, we used the structure to create a linked list. Also, we defined the addNode() function to insert the node into the current list. We take each element of the array and use the addNode() function to add a character to the list.
The addNode() function traverses the list until it gets the last node and adds the new node at last.
#include <iostream> using namespace std; // creating the struct listNode struct listNode { int ch; listNode *next; } listNode; // Swapping the nodes of the linked list struct listNode *swap(struct listNode *pointer1, struct listNode *pointer2) { struct listNode *tmp = pointer2->next; pointer2->next = pointer1; pointer1->next = tmp; return pointer2; } // Sorting the linked list void sortLinkedList(struct listNode **start, int len) { // storing the current node struct listNode **curr; int p, q; bool swapped = false; for (p = 0; p <= len; p++) { // store the current node address in curr curr = start; swapped = false; // traverse the list for (q = 0; q < len - p - 1; q++) { struct listNode *temp1 = *curr; struct listNode *temp2 = temp1->next; // If temp1 > temp2 swap them if (temp1->ch > temp2->ch) { // Update the link after swapping *curr = swap(temp1, temp2); swapped = true; } // Move the current pointer to the next node curr = &(*curr)->next; } // If any pair of elements is not swapped, break the loop. if (swapped == false) break; } } // adding nodes to the linked list void addNode(struct listNode **start, int ch) { // creating a new node struct listNode *temp = new struct listNode(); // add ch to the node temp->ch = ch; temp->next = NULL; // If the list is empty, add a node to the list if (*start == NULL) { *start = temp; } else { // If the list has some nodes, append the node at the end of the list struct listNode *pointer1 = *start; while (pointer1->next != NULL) { pointer1 = pointer1->next; } pointer1->next = temp; } } // print nodes void printLinkedList(struct listNode *head) { cout << "The sorted list is " << endl; while (head != NULL) { cout << char(head->ch) << " -> "; head = head->next; } cout << "NULL" << endl; } int main() { int arr[] = {'e', 's', 'a', 'x', 'c', 'e', 'f', 'p', 'b', 'n', 'a'}; int len, p; // create an empty linked list struct listNode *start = NULL; len = sizeof(arr) / sizeof(arr[0]); // inserting characters of the array to a linked list for (p = 0; p < len; p++) addNode(&start, arr[p]); // Sort the list sortLinkedList(&start, len); printLinkedList(start); return 0; }
Output
The sorted list is a -> a -> b -> c -> e -> e -> f -> n -> p -> s -> x -> NULL
Time complexity? O(N^2) as we use two nested loops.
Space complexity ? O(1) as we don't use dynamic space in the sortLinkedList() function. However, we use O(N) space when we convert an array to a list, but it is not a part of sorting.
Approach 2
We will use the selection sort algorithm to sort the linked list in this approach. In the selection sort algorithm, we find the node containing the minimum value from the suffix of len ? p. After that, we swap the minimum node with the node at the pth index.
Algorithm
Step 1 ? Define and initialize the ?current' node with the ?start' node.
Step 2 ? Use the while loop to make iterations until the ?current' node is not null.
Step 3 ? Initialize the ?minNode' with the ?current' node and ?traverse' with the next current, as we need to find the minimum node.
Step 4 ? Use the nested while loop to find a minimum node and swap it with the ?current' node.
Step 5 ? Move the current node to the next.
Example
#include <iostream> using namespace std; struct listNode { int ch; listNode* next; }; void swapNodes(struct listNode* node1, struct listNode* node2) { int temp = node1->ch; node1->ch = node2->ch; node2->ch = temp; } void sortLinkedList(struct listNode* start) { struct listNode* current = start; while (current != nullptr) { struct listNode* minNode = current; struct listNode* traverse = current->next; while (traverse != nullptr) { if (traverse->ch < minNode->ch) { minNode = traverse; } traverse = traverse->next; } swapNodes(current, minNode); current = current->next; } } void addNode(struct listNode** start, int ch) { struct listNode* newNode = new struct listNode(); newNode->ch = ch; newNode->next = nullptr; if (*start == nullptr) { *start = newNode; } else { struct listNode* current = *start; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } void printLinkedList(struct listNode* head) { cout << "The sorted list is " << endl; while (head != nullptr) { cout << char(head->ch) << " -> "; head = head->next; } cout << "NULL" << endl; } int main() { int arr[] = {'e', 's', 'a', 'x', 'c', 'e', 'f', 'p', 'b', 'n', 'a'}; int len = sizeof(arr) / sizeof(arr[0]); struct listNode* start = nullptr; for (int p = 0; p < len; p++) { addNode(&start, arr[p]); } sortLinkedList(start); printLinkedList(start); return 0; }
Output
The sorted list is a -> a -> b -> c -> e -> e -> f -> n -> p -> s -> x -> NULL
Time complexity? O(N^2) as we use two nested while loops.
Space complexity ? O(1) as we don't use dynamic space.