Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sorting given character Array using Linked List

  • Difficulty Level : Hard
  • Last Updated : 29 Dec, 2021

Given an array arr[] containing N lowercase English alphabets, the task is to sort this array arr[] using a linked list.

Examples:  

Input: arr[] = [‘b’, ‘b’, ‘c’, ‘c’, ‘d’, ‘e’, ‘f’, ‘b’, ‘b’, ‘a’, ‘a’] 
Output: a->a->b->b->b->b->c->c->d->e->f->NULL

Input: arr[] = [‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’]
Output: e->e->e->e->f->g->g->k->k->o->r->s->s->NULL

 

Approach: To solve this problem, first create the linked list from the character array. After the linked list has been formed, sort it using bubble sort.

Below is the implementation of the above approach: 

C++




#include <iostream>
using namespace std;
  
// Structure for a node
struct Node {
    int data;
    Node* next;
} Node;
  
// Function to swap the nodes
struct Node* swap(struct Node* ptr1, struct Node* ptr2)
{
    struct Node* tmp = ptr2->next;
    ptr2->next = ptr1;
    ptr1->next = tmp;
    return ptr2;
}
  
// Function to sort the list
int bubbleSort(struct Node** head, int count)
{
    struct Node** h;
    int i, j, swapped;
  
    for (i = 0; i <= count; i++) {
  
        h = head;
        swapped = 0;
  
        for (j = 0; j < count - i - 1; j++) {
  
            struct Node* p1 = *h;
            struct Node* p2 = p1->next;
  
            if (p1->data > p2->data) {
  
                // Update the link after swapping
                *h = swap(p1, p2);
                swapped = 1;
            }
  
            h = &(*h)->next;
        }
  
        // Break if the loop ended
        // without any swap
        if (swapped == 0)
            break;
    }
}
  
// Function to print the list
void printList(struct Node* n)
{
    while (n != NULL) {
        cout << char(n->data) << " -> ";
        n = n->next;
    }
    cout << endl;
}
  
// Function to insert a struct Node
// at the end of a linked list
void insert(struct Node** head, int data)
{
    struct Node* ptr = new struct Node();
  
    ptr->data = data;
    ptr->next = NULL;
    if (*head == NULL) {
        *head = ptr;
    }
    else {
        struct Node* ptr1 = *head;
        while (ptr1->next != NULL) {
            ptr1 = ptr1->next;
        }
        ptr1->next = ptr;
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 'b', 'b', 'c', 'c', 'd', 'e',
                  'f', 'b', 'b', 'a', 'a' };
    int list_size, i;
  
    // start with empty linked list
    struct Node* start = NULL;
    list_size = sizeof(arr) / sizeof(arr[0]);
  
    // Create linked list from the array arr[]
    for (i = 0; i < list_size; i++)
        insert(&start, arr[i]);
  
    // sort the linked list
    bubbleSort(&start, list_size);
  
    // Print list after sorting
    printList(start);
  
    return 0;
}
Output
a -> a -> b -> b -> b -> b -> c -> c -> d -> e -> f -> 

Time Complexity: O(N2)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!