Shuffling a linked list Design a divide and conquer algorith

Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.

Solution

#include<stdio.h>

#include<stdlib.h>

  

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

struct node* SortedMerge(struct node* a, struct node* b);

void FrontBackSplit(struct node* source,

          struct node** frontRef, struct node** backRef);

void MergeSort(struct node** headRef)

{

  struct node* head = *headRef;

  struct node* a;

  struct node* b;

  if ((head == NULL) || (head->next == NULL))

  {

    return;

  }

  FrontBackSplit(head, &a, &b);

  MergeSort(&a);

  MergeSort(&b);

  *headRef = SortedMerge(a, b);

}

struct node* SortedMerge(struct node* a, struct node* b)

{

  struct node* result = NULL;

  

  if (a == NULL)

     return(b);

  else if (b==NULL)

     return(a);

  if (a->data <= b->data)

  {

     result = a;

     result->next = SortedMerge(a->next, b);

  }

  else

  {

     result = b;

     result->next = SortedMerge(a, b->next);

  }

  return(result);

}

/* Split the nodes of the given list into front and back halves,

     and return the two lists using the reference parameters.

     If the length is odd, the extra node should go in the front list.

     Uses the fast/slow pointer strategy. */

void FrontBackSplit(struct node* source,

          struct node** frontRef, struct node** backRef)

{

  struct node* fast;

  struct node* slow;

  if (source==NULL || source->next==NULL)

  {

    *frontRef = source;

    *backRef = NULL;

  }

  else

  {

    slow = source;

    fast = source->next;

    while (fast != NULL)

    {

      fast = fast->next;

      if (fast != NULL)

      {

        slow = slow->next;

        fast = fast->next;

      }

    }

    *backRef = slow->next;

    slow->next = NULL;

  }

}

/* Functio

void printList(struct node *node)

{

  while(node!=NULL)

  {

   printf(\"%d \", node->data);

   node = node->next;

  }

}

void push(struct node** head_ref, int new_data)

{

  struct node* new_node =

            (struct node*) malloc(sizeof(struct node));

  

  

  new_node->data = new_data;

  

  

  new_node->next = (*head_ref);   

  

  

  (*head_ref)    = new_node;

}

  

int main()

{

  struct node* res = NULL;

  struct node* a = NULL;

  

  

   Created lists shall be a: 2->3->20->5->10->15 */

  push(&a, 15);

  push(&a, 10);

  push(&a, 5);

  push(&a, 20);

  push(&a, 3);

  push(&a, 2);

  

  /* Sort the above created Linked List */

  MergeSort(&a);

  

  printf(\"\ Sorted Linked List is: \ \");

  printList(a);          

  

  getchar();

  return 0;

}

Run on IDE

Time Complexity: O(nLogn)

#include<stdio.h>

#include<stdlib.h>

  

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

struct node* SortedMerge(struct node* a, struct node* b);

void FrontBackSplit(struct node* source,

          struct node** frontRef, struct node** backRef);

void MergeSort(struct node** headRef)

{

  struct node* head = *headRef;

  struct node* a;

  struct node* b;

  if ((head == NULL) || (head->next == NULL))

  {

    return;

  }

  FrontBackSplit(head, &a, &b);

  MergeSort(&a);

  MergeSort(&b);

  *headRef = SortedMerge(a, b);

}

struct node* SortedMerge(struct node* a, struct node* b)

{

  struct node* result = NULL;

  

  if (a == NULL)

     return(b);

  else if (b==NULL)

     return(a);

  if (a->data <= b->data)

  {

     result = a;

     result->next = SortedMerge(a->next, b);

  }

  else

  {

     result = b;

     result->next = SortedMerge(a, b->next);

  }

  return(result);

}

/* Split the nodes of the given list into front and back halves,

     and return the two lists using the reference parameters.

     If the length is odd, the extra node should go in the front list.

     Uses the fast/slow pointer strategy. */

void FrontBackSplit(struct node* source,

          struct node** frontRef, struct node** backRef)

{

  struct node* fast;

  struct node* slow;

  if (source==NULL || source->next==NULL)

  {

    *frontRef = source;

    *backRef = NULL;

  }

  else

  {

    slow = source;

    fast = source->next;

    while (fast != NULL)

    {

      fast = fast->next;

      if (fast != NULL)

      {

        slow = slow->next;

        fast = fast->next;

      }

    }

    *backRef = slow->next;

    slow->next = NULL;

  }

}

/* Functio

void printList(struct node *node)

{

  while(node!=NULL)

  {

   printf(\"%d \", node->data);

   node = node->next;

  }

}

void push(struct node** head_ref, int new_data)

{

  struct node* new_node =

            (struct node*) malloc(sizeof(struct node));

  

  

  new_node->data = new_data;

  

  

  new_node->next = (*head_ref);   

  

  

  (*head_ref)    = new_node;

}

  

int main()

{

  struct node* res = NULL;

  struct node* a = NULL;

  

  

   Created lists shall be a: 2->3->20->5->10->15 */

  push(&a, 15);

  push(&a, 10);

  push(&a, 5);

  push(&a, 20);

  push(&a, 3);

  push(&a, 2);

  

  /* Sort the above created Linked List */

  MergeSort(&a);

  

  printf(\"\ Sorted Linked List is: \ \");

  printList(a);          

  

  getchar();

  return 0;

}

Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl
Shuffling a linked list. Design a divide and conquer algorithm that randomly shuffles a linked list in O(nlog(n)) time and logarithmic extra space.Solution#incl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site