Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

COE116L - B2

Group 2

Pangilinan, Ricardo Geronimo L.

LAB 6: Sorting Algorithms


Question 1: Answer:
Assume the following keys: 7, 28, 31, 7, 28, 31, 40, 5, 20
40, 5, 20
7 28 28 30 40 5 *temp 20
The first four keys are in order. To move 7 *20 28 30 40 5
5 to its proper position using insertion
sort, exactly 7 7 20 28 30 40 *temp 5
How many key comparisons are *5 7 20 28 30 40
executed?
4 key comparison are executed.

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.

Lab Assignment # 1: Code:


Revise ProgramEntryPoint.cpp to
include a function named Search to
determine whether the item to be
added is already in the list.
Lab Assignment # 2: Code:
Make a Linked-List version of Program
Listing 6.1.
Lab Assignment # 3: Code:
Write a C++ program that implements
quicksort algorithm. Assume the
following list of keys: 16, 38, 54, 80, 22,
65, 55, 48, 64, 95, 5, 100, 58, 25, 36.
Use pivot as the middle element of the
list. Display the resulting list after one
call to the partition procedure and the

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.

Lab Assignment # 4: Code:


Write a program to test mergesort for #include<stdio.h>
linked lists. #include<stdlib.h>

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);

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);
}

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;
}
}

*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;
}
}

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;

push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 2);

MergeSort(&a);

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


printList(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;
};

void insertAtTheBegin(struct node **start_ref, int


data);

void bubbleSort(struct node *a, struct node *b);

void printList(struct node *start);

int main()
{
int arr [] = {12, 56, 2, 11, 90,};
int list_size, i;

struct node *start = NULL;

for (i = 0; i < 6; i++0)


insertAtTheBegin(&start, arr[i];

printf("\n Linked List before sorting ");


printList(start);

bubbleSort(start);

printf("\n Linked List after sorting ");


printList(start);

getchar();
return 0;
}

void insertAtTheBegin(struct node **start_ref, int


data)
{
stuct node *ptrl = (struct
node*)malloc(sizeof(struct node));
ptrl ->data = data;
ptrl ->next = *start_ref;
*start_ref = ptrl;
}
void printList(struct node *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/
{
struct node *temp =start;
printf("\n");
while (temp!=NULL)
{
printf("%d", temp->data);
temp = temp -> next;
}
}

void bubbleSort (struct node *start)


{
int swapped, i;
struct node *ptr1;
struct node *1ptr = NULL;

if (ptr1 ==NULL);
return;
do
{
swapped = 0;
ptrl = start;

while (ptr1->data > ptr1 -> next -> data)


{
swap(ptr1, ptr1->next);
swapped =1;
}
ptr1 = ptr1 ->next;
1ptr = ptr1;
while(swapped);
}
}

void swap (struct node *a, sturct node *b)


{
int temp = a->data;
a->data = b->data;
b->data = temp;
}

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)

You might also like