Exemple #1
0
void FrontBackSplitTest() {
    struct node* empty_list = BuildEmpty();
    struct node* list1 = BuildOne();
    struct node* list2 = BuildOneTwo();
    struct node* list3 = BuildOneThree();
    struct node* list4 = BuildOneThreeFour();
    struct node* front = NULL;
    struct node* back = NULL;

    printf("** test case 1 **\n");
    printf("origin list:\n");
    Print(empty_list);
    FrontBackSplit(empty_list, front, back);
    printf("After split:\n");
    Print(front);
    Print(back);

    printf("** test case 2 **\n");
    printf("origin list:\n");
    Print(list1);
    FrontBackSplit(list1, front, back);
    printf("After split:\n");
    Print(front);
    Print(back);

    printf("** test case 3 **\n");
    printf("origin list:\n");
    Print(list2);
    FrontBackSplit(list2, front, back);
    printf("After split:\n");
    Print(front);
    Print(back);

    printf("** test case 4 **\n");
    printf("origin list:\n");
    Print(list3);
    FrontBackSplit(list3, front, back);
    printf("After split:\n");
    Print(front);
    Print(back);

    printf("** test case 5 **\n");
    printf("origin list:\n");
    Print(list4);
    FrontBackSplit(list4, front, back);
    printf("After split:\n");
    Print(front);
    Print(back);
}
void MergeSort(struct node** headRef)
{

  struct node *head = *headRef;
  struct node *head_front = NULL;
  struct node *head_back = NULL;

  if(head == NULL || head->next == NULL)
  {
    printf("return\n");
    return;
  }

  FrontBackSplit(head, &head_front, &head_back);


  print_linked_list("&head_front", &head_front);
  MergeSort(&head_front);
  print_linked_list("&head_back", &head_back);
  MergeSort(&head_back);

  *headRef = SortedMerge(head_front, head_back);

  print_linked_list("headRef", headRef);

}
Exemple #3
0
/*
 * @func: merge sort for link list
 */
struct node* MergeSortRecur(struct node* head){
    if (head == NULL)
            return NULL;
    struct node* pfront = NULL;
    struct node* pback = NULL;
    FrontBackSplit(head, &pfront, &pback);
    return SortedMerge(MergeSortRecur(pfront), MergeSortRecur(pback));
}
Exemple #4
0
 ListNode* MergeSortRecur(ListNode* head){
     if (head == NULL)
         return NULL;
     else if (head->next == NULL)
         return head;
         
     ListNode* pfront = NULL;
     ListNode* pback = NULL;
     FrontBackSplit(head, &pfront, &pback);
     
     return SortedMerge(MergeSortRecur(pfront), MergeSortRecur(pback));
 }
void sort_list_using_Merge_sort(struct SLList **head_ref)
{
	struct SLList *head=*head_ref;
	struct SLList *a;
	struct SLList *b;
	if((head==NULL)||(head->next==NULL))
		return;
	FrontBackSplit(head, &a, &b);
	sort_list_using_Merge_sort(&a);
	sort_list_using_Merge_sort(&b);
	*head_ref = merge(a,b);
}
// 15 — MergeSort()
void MergeSort(struct node** headRef) {
	struct node* head = *headRef;
	struct node* a;
	struct node* b;
	// Base case -- length 0 or 1
	if ((head == NULL) || (head->next == NULL)) {
		return;
	}
	FrontBackSplit(head, &a, &b); // Split head into 'a' and 'b' sublists
	// We could just as well use AlternatingSplit()
	MergeSort(&a); // Recursively sort the sublists
	MergeSort(&b);
	*headRef = SortedMerge(a, b); // answer = merge the two sorted lists together
}
Exemple #7
0
//better code...
void MergeSort(struct node** headRef) {
	struct node* head = *headRef;
	struct node* a;
	struct node* b;
	// Base case -- length 0 or 1
	if ((head == NULL) || (head->next == NULL)) {
		return;
	}
	FrontBackSplit(head, &a, &b); // 
	
	MergeSort(&a); // 
	MergeSort(&b);
	*headRef = SortedMerge(a, b); //
}
Exemple #8
0
void mergesort(List** headRef) {
  List* head = *headRef;
  List* a;
  List* b;
 
  if ((head == NULL) || (head->next == NULL))
  {
    return;
  }
 
  FrontBackSplit(head, &a, &b); 
 
  mergesort(&a);
  mergesort(&b);
  *headRef = sort(a, b);
}
void FileIconCollection::MergeSort(FileIcon *&headRef)
// sorts the linked list by changing next pointers (not data)
{
	// Base case -- length 0 or 1
	if (!headRef || !headRef->pNext) return;

	// Split head into 'a' and 'b' sublists
	FileIcon *a, *b;
	FrontBackSplit(headRef, a, b);

	// Recursively sort the sublists
	MergeSort(a);
	MergeSort(b);

	// Merge the two sorted lists together
	headRef = SortedMerge(a, b);
}
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);
}
Exemple #11
0
void mergesort(struct node **head_ref)
{
	struct node *head = *head_ref;
	struct node *a;
	struct node *b;
	
	if(head == NULL || head->next == NULL)
		return ;
	
	
		FrontBackSplit(head,&a,&b);
		
		mergesort(&a);
		mergesort(&b);
		
		*head_ref = sortedMerge(a,b);
	
}
Exemple #12
0
/* sorts the linked list formed by arbit pointers
  (does not change next pointer or data) */
void MergeSort(struct node** headRef)
{
    struct node* head = *headRef;
    struct node* a, *b;
 
    /* Base case -- length 0 or 1 */
    if ((head == NULL) || (head->arbit == NULL))
        return;
 
    /* Split head into 'a' and 'b' sublists */
    FrontBackSplit(head, &a, &b);
 
    /* Recursively sort the sublists */
    MergeSort(&a);
    MergeSort(&b);
 
    /* answer = merge the two sorted lists together */
    *headRef = SortedMerge(a, b);
}
Exemple #13
0
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct node** headRef, int columnNumber[], char order[], int numberOfColumnsSort)
{
    struct node* head = *headRef;
    struct node* a;
    struct node* b;

    /* Base case -- length 0 or 1 */
    if ((head == NULL) || (head->column == NULL))
    {
        return;
    }

    /* Split head into 'a' and 'b' sublists */
    FrontBackSplit(head, &a, &b);

    /* Recursively sort the sublists */
    MergeSort(&a, columnNumber, order, numberOfColumnsSort);
    MergeSort(&b, columnNumber, order, numberOfColumnsSort);

    /* answer = merge the two sorted lists together */
    *headRef = SortedMerge(a, b, columnNumber, order, numberOfColumnsSort);
}
Exemple #14
0
/* sorts the linked list by changing next pointers (not data) */
void MergeSort(struct Link** headRef)
{
  struct Link* head = *headRef; // deref that pointer to get the address of sentinel link
  struct Link* a;
  struct Link* b;
 
  //printf("%s is the head node's name\n", ((Node*)(head))->p->name);
  //printf("%s is the head.next node's name\n", ((Node*)(head->next))->p->name);
  //printf("%s is the head.next node's name\n", ((Node*)(head))->p->name);
  // the above works b.c. address of head is address of head->next
  //printf("%s is the head.prev node's name field====================\n", ((Node*)(head->prev))->p->name);
  // this last stmt designed to fail: it should point to sentinel, which has no data

  //1) If head is NULL or there is only one element 
  //in the Linked List, then return.
  // Base case -- length 0 or 1 
  if ((head == NULL) || (head->next == NULL))
  {
    return;
  }
 
  //2) Else divide the linked list into two halves.
  // a and b are two halves 
  /* Split head into 'a' and 'b' sublists 
     --a and b are empty when passed to FrontBackSplit, 
     and get their addresses populated when in that function.*/
  printf("calling FrontBackSplit(head, &a, &b); \n");
  FrontBackSplit(head, &a, &b); 

                        //printf("calling   MergeSort(&a); \n");
  // Recursively sort the sublists
  MergeSort(&a);
                        //printf("calling   MergeSort(&b); \n");
  MergeSort(&b);
 
  // merge the two sorted lists together 
  *headRef = SortedMerge(a, b);
}
int main()
{
	struct node* b =NULL;
	struct node* a = BuildListOfLength(5);
	printf("list ");
	printlinkedlist(a);
	FrontBackSplit(a, &a, &b);
	printf("splited to ");
	printlinkedlist(a);
	printf(",");
	printlinkedlist(b);
	printf("\n");

	a = BuildListOfLength(4);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

	a = BuildListOfLength(3);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

	a = BuildListOfLength(2);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

        a = BuildListOfLength(1);
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");

        a = NULL;
        printf("list ");
        printlinkedlist(a);
        FrontBackSplit(a, &a, &b);
        printf("splited to ");
        printlinkedlist(a);
        printf(",");
        printlinkedlist(b);
        printf("\n");


}