Professional Documents
Culture Documents
LAB6
LAB6
Group 2
Question 2: Answer:
Both mergesort and quicksort sort a list Mergesort and Quicksort differ in how they
by partitioning the list. Explain how partition the list. Quicksort first selects an
mergesort element in the list, called pivot, and then
Differs from quicksort in partitioning the partitions the list so that the elements in one
list. sublist are less than pivot and the elements in the
other sublist are greater than or equal to pivot.
By contrast, mergesort divides the list into two
sublists of nearly equal size.
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
resulting list after two calls to the
partition procedure.
struct node
{
int data;
struct node* next;
};
MergeSort(&a);
MergeSort(&b);
if (a == NULL)
return(b);
else if (b==NULL)
return(a);
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
void printList(struct node *node)
{
while(node!=NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 2);
MergeSort(&a);
getchar();
return 0;
}
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
Lab Assignment # 5: Code:
Write a program to test bubble sort for #include<stdio.h>
linked lists. #include<stdlib.h>
struct node
{
int data;
struct node* next;
};
int main()
{
int arr [] = {12, 56, 2, 11, 90,};
int list_size, i;
bubbleSort(start);
getchar();
return 0;
}
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
{
struct node *temp =start;
printf("\n");
while (temp!=NULL)
{
printf("%d", temp->data);
temp = temp -> next;
}
}
if (ptr1 ==NULL);
return;
do
{
swapped = 0;
ptrl = start;
This study source was downloaded by 100000853287010 from CourseHero.com on 10-16-2022 21:58:34 GMT -05:00
https://www.coursehero.com/file/38266157/LAB6docx/
Powered by TCPDF (www.tcpdf.org)