4608690_1712792546_FA2020COP3502ProgrammingAssign.pdf Programming Assignment 3 COP3502 Computer Science I – Fall 2020 Overview This assignment is intended to make you implement several different...

1 answer below »
Implementation of Insertion and Merge sorting


4608690_1712792546_FA2020COP3502ProgrammingAssign.pdf Programming Assignment 3 COP3502 Computer Science I – Fall 2020 Overview This assignment is intended to make you implement several different sorting algorithms, and the means to analyze their performance. This assignment is in two parts: a program, and an analysis. Details You’ve been given a scattered list of small monsters present in the Knightrola region. You need to evaluate six means of comparison-based sort to get these monsters in order. You need to implement six sorts for the monster structure: • Bubble sort • Selection sort • Insertion sort • Quick sort • Merge sort • Merge sort, switching to insertion sort at ? ≤ 25 Data Structures and Prototypes We’ve given you two template C files that will help you with implementation. • integer-sorts-template.c: Contains implementation code for bubble, selection, and quick sorts against integer arrays. IMPLEMENT MERGE SORT AND INSERTION SORT HERE FIRST, AND GET THEM WORKING, TO DEVELOP YOUR UNDERSTANDING OF THE ALGORITHMS. • monster-sorts-template.c: Contains the shell for sorting monsters via all six sorts. You may use your code from integer-sorts-template.c directly, modifying it as necessary to work on the more complex data structures. Don’t change the structures, the function headers, or any of the helper functions. So your order of operations should look like this: • Read and understand the attached documentation for insertion sort and merge sort. • Implement insertion sort, merge sort, and merge-insertion sort against integers, within your own copy of integer-sorts-template.c. • Implement the six sorts against monsters, within your own copy of monster-sorts-template.c. o Implement compare_monsters() first to avoid implementing everything twice! o Remember to use memmove() instead of memcpy() when structures can overlap. (This will come up in insertion sort.) You aren’t required to deal with file input, file output or complex memory management in this assignment (though you will need to do some minor memory allocation for merge sort). This is intentional. Once you have everything in monster-sorts-template working, the output to the terminal should look something like the sample output you’ve been provided. The list is randomized, so it will not be exactly like what you see there. The New Sorts Merge Sort In merge sort, you repeatedly sort and merge the smallest sublists of a list, working your way up to sorting and merging the entire list. You’ll implement the basic recursive version, which works as follows: Merge sort: • Recursive merge sort the entire list. Recursive merge sort: • If the size of the list is one, it’s already sorted. • Otherwise: o Recursive merge sort the front half of the list and the back half of the list. o Merge the now-sorted front and back halves of the list. Merge adjacent sub-lists: • Create a temporary list large enough to hold all the elements in both lists. Set a pointer to the first element in both lists, and to the first space in the temporary list. • Iterate until you run out of elements in both lists: o Look at the current element in both lists. o Copy the smaller one into the temporary list. (If you’re out of elements in one list, use the other.) o Increment the temporary list pointer, and the list pointer for the list you copied from. • Copy the temporary list back into the lists you were merging, writing over both. (If the lists you were merging were not adjacent, this will cause bad things to happen.) Insertion Sort In insertion sort, you divide the list into the elements you’ve already sorted and the elements you haven’t yet, iterate over the elements you haven’t yet, and insert them into the sorted sub-list. The basic array version, which you’ll implement, works as follows: • Iterate ? from the second to the last element of the list. ? is always the first element you have not yet sorted. o Grab element ?. o Iterate ? from the front of the list, until you either reach ? or find an element with a value higher than element ?. You are using ? to look through your already-sorted list. o If you reached ?, do nothing. Otherwise: ▪ Move every element to the right (inclusive) of your final element ? and to the left (exclusive) of element ? right by one, “squashing” element ? and creating an empty space before element ?. ▪ Put your copied element ? in the empty space. o Increment ? and keep going. Cleanup Specific Requirements • You do not need to comment line by line, but comment every function and every “paragraph” of code. • You don’t have to hold to any particular indentation standard, but you must indent and you must do so consistently within your own code. • You may not use global variables. Analysis, Reflection and Submission Name your file cop3502-as3--.c. For example, mine will be named cop3502-as3-gerber- matthew.c. Along with your C file, submit a short report in PDF format answering: • Did the results of your program confirm what you already knew or suspected about these sorting algorithms? How? • What differences did you see between and among the slow (bubble, selection, insertion) and fast (quick, merge, merge-insertion) algorithms? 628444361_MONSTER.pdf // all-monster-sorts.c - Sort monsters by name and weight. /* The idea of sorting is simple: take unordered objects, and arrange them in an order. It has a lot of uses, so there's been a lot of work done with it. Here, we're going to demonstrate a few of the simpler, more classic sorting techniques. */ #include #include #include #include #include #include /* Monster structure and helper functions - DO NOT MODIFY THESE. */ typedef struct monster { int id; char name[64]; char element[64]; int population; double weight; } monster; monster *make_some_monsters(int n) { monster *monsters = malloc(sizeof(monster) * n); time_t t; srand((unsigned) time(&t)); for(int i = 0; i < n;="" i++)="" {="" monsters[i].id="i;" sprintf(monsters[i].name,="" "monster="" #%d",="" rand());="" sprintf(monsters[i].element,="" "element="" #%d",="" rand());="" monsters[i].population="rand();" monsters[i].weight="500.0" *="" ((double)="" rand()="" (double)="" rand_max);="" }="" return="" monsters;="" }="" void="" output_monster_list(monster="" *list,="" int="" n,="" char="" *title)="" {="" printf("list="" %s:\n",="" title);="" for(int="" i="0;" i="">< n;="" i++)="" {="" printf("="" monster="" %d:="" %s="" %s="" %d="" %lf\n",="" i,="" list[i].name,="" list[i].element,="" list[i].population,="" list[i].weight);="" }="" printf("\n");="" }="" void="" print_clocks(clock_t="" clocks)="" {="" printf("="" %lfs="" cpu="" time="" used\n",="" ((double)="" clocks)="" clocks_per_sec);="" }="" void="" swap_monsters(monster="" *list,="" int="" i,="" int="" j)="" {="" monster="" temp;="" memcpy(&temp,="" list="" +="" i,="" sizeof(monster));="" memcpy(list="" +="" i,="" list="" +="" j,="" sizeof(monster));="" memcpy(list="" +="" j,="" &temp,="" sizeof(monster));="" }="" void="" check_monster_sort(monster="" *list,="" int="" n,="" int="" use_name,="" int="" use_weight)="" {="" for(int="" i="1;" i="">< n;="" i++)="" {="" if(compare_monsters(list="" +="" i="" -="" 1,="" list="" +="" i,="" use_name,="" use_weight)=""> 0) { printf("*** The list is NOT sorted.\n\n"); return; } } printf("The list is sorted.\n\n"); } /* The core comparison function. */ int compare_monsters(monster *m1, monster *m2, int use_name, int use_weight) { // YOUR CODE GOES HERE. } /* Implement ascending quick sort. */ int repartition(monster *list, int low_index, int high_index, int *comparisons, int *swaps, int use_name, int use_weight) { // YOUR CODE GOES HERE. } /* Recursive function for quick sort. */ void quick_sort_recursive(monster *list, int low_index, int high_index, int *comparisons, int *swaps, int use_name, int use_weight) { // YOUR CODE GOES HERE. } /* Shell function for quick sort. */ void quick_sort(monster *list, int n, int use_name, int use_weight) { int comparisons = 0; int swaps = 0; clock_t start_cpu, end_cpu; printf("Quick sort %d monsters by %s...\n", n, use_name ? "name" : "weight"); start_cpu = clock(); quick_sort_recursive(list, 0, n-1, &comparisons, &swaps, use_name, use_weight); end_cpu = clock(); printf("Sort complete with %d comparisons and %d swaps.\n", comparisons, swaps); print_clocks(end_cpu - start_cpu); } /* Implement ascending bubble sort. */ void bubble_sort(monster *list, int n, int use_name, int use_weight) { int i; int j; int temp; int comparisons = 0; int swaps = 0; clock_t start_cpu, end_cpu; printf("Bubble sort %d monsters by %s...\n", n, use_name ? "name" : "weight"); start_cpu = clock(); // YOUR CODE GOES HERE. end_cpu = clock(); printf("Sort complete with %d comparisons and %d swaps.\n", comparisons, swaps); print_clocks(end_cpu - start_cpu); } /* Highest-value finder for selection sort. */ int find_highest(monster *list, int n, int *comparisons, int use_name, int use_weight) { // YOUR CODE GOES HERE. } /* Implement ascending selection sort. */ void selection_sort(monster *list, int n, int use_name, int use_weight) { int i; int highest; int comparisons = 0; int swaps = 0;
Answered Same DayNov 06, 2021

Answer To: 4608690_1712792546_FA2020COP3502ProgrammingAssign.pdf Programming Assignment 3 COP3502 Computer...

Ria answered on Nov 07 2021
152 Votes
monster-sorts-template.c
// all-monster-sorts.c - Sort monsters by name and weight.
/* The idea of sorting is simple: take unordered objects, and arrange them in an
order. It has a lot of uses, so there's been a lot of work done with it. Here,
we're going to demonstrate a few of the simpler, more classic sorting techniques.
*/
#include
#include
#include
#include
#include
#include
#pragma warning (disable: 4996)
/* Monster structure and helper functions - DO NOT MODIFY THESE. */
typedef struct monster {
int id;
char name[64];
char element[64];
int population;
double weight;
} monster;
monster* make_some_monsters(int n)
{
monster* monsters = malloc(sizeof(monster) * n);
time_t t;
srand((unsigned)time(&t));
for (int i = 0; i < n; i++)
{
monsters[i].id = i;
sprintf(monsters[i].name, "Monster #%d", rand());
sprintf(monsters[i].element, "Element #%d", rand());
monsters[i].population = rand();
monsters[i].weight = 500.0 * ((double)rand() / (double)RAND_MAX);
}
return monsters;
}
void output_monster_list(monster* list, int n, char* title) {
printf("List %s:\n", title);
for (int i = 0; i < n; i++) {
printf(" Monster %d: %s %s %d %lf\n", i, list[i].name, list[i].element, list[i].population, list[i].weight);
}
printf("\n");
}
void print_clocks(clock_t clocks) {
printf(" %lfs CPU time used\n", ((double)clocks) / CLOCKS_PER_SEC);
}
void swap_monsters(monster* list, int i, int j)
{
monster temp;
memmove(&temp, list + i, sizeof(monster));
memmove(list + i, list + j, sizeof(monster));
memmove(list + j, &temp, sizeof(monster));
}
void check_monster_sort(monster* list, int n, int use_name, int use_weight)
{
for (int i = 1; i < n; i++) {
if (compare_monsters(list + i - 1, list + i, use_name, use_weight) > 0) {
printf("*** The list is NOT sorted.\n\n");
return;
}
}
printf("The list is sorted.\n\n");
}
/* The core comparison function. */
int compare_monsters(monster* m1, monster* m2, int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
if (use_name)
{
if (m1->name < m2->name)
{
return 0;
}
}
if (use_weight)
{
if (m1->weight < m2->weight)
{
return 0;
}
}
return 0;
}
/* Implement ascending quick sort. */
int repartition(monster* list, int low_index, int high_index, int* comparisons, int* swaps,
int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
char* pivot_char = list[high_index].name;
int pivot_value = list[high_index].weight;
int i = low_index;
for (int j = low_index; j < high_index; j++)
{
if (use_name)
{
(*comparisons)++;
if (list[j].name < pivot_value)
{
(*swaps)++;
swap_monsters(list, i, j);
i++;
}
}
if (use_weight)
{
(*comparisons)++;
if (list[j].weight < pivot_value)
{
(*swaps)++;
swap_monsters(list, i, j);
i++;
}
}
}
/* We've now placed everything below pivot_value in list[low_index..i-1] - and that
means we can just put our pivot value in list[i]! */
swaps++;
swap_monsters(list, i, high_index);
return i;
}
/* Recursive function for quick sort. */
void quick_sort_recursive(monster* list, int low_index, int high_index, int* comparisons, int* swaps,
int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
int pivot_index = repartition(list, low_index, high_index, comparisons, swaps, use_name, use_weight);
if (pivot_index - 1 > low_index)
{
quick_sort_recursive(list, low_index, pivot_index - 1, comparisons, swaps, use_name, use_weight);
}
if (high_index > pivot_index + 1)
{
quick_sort_recursive(list, pivot_index + 1, high_index, comparisons, swaps, use_name, use_weight);
}
}
/* Shell function for quick sort. */
void quick_sort(monster* list, int n, int use_name, int use_weight)
{
int comparisons = 0;
int swaps = 0;
clock_t start_cpu, end_cpu;
printf("Quick sort %d monsters by %s...\n", n, use_name ? "name" : "weight");
start_cpu = clock();
quick_sort_recursive(list, 0, n - 1, &comparisons, &swaps, use_name, use_weight);
end_cpu = clock();
printf("Sort complete with %d comparisons and %d swaps.\n", comparisons, swaps);
print_clocks(end_cpu - start_cpu);
}
/* Implement ascending bubble sort. */
void bubble_sort(monster* list, int n, int use_name, int use_weight)
{
int i;
int j;
int temp;
int comparisons = 0;
int swaps = 0;
clock_t start_cpu, end_cpu;
printf("Bubble sort %d monsters by %s...\n", n, use_name ? "name" : "weight");
start_cpu = clock();
// YOUR CODE GOES HERE.
for (i = n - 1; i >= 0; i--)
{
for (j = 0; j < i; j++)
{
comparisons++;
if (use_name) //sort by name
{
if (list[j].name > list[j + 1].name)
{
swaps++;
swap_monsters(list, j, j + 1);
}
}
comparisons++;
if (use_weight) //sort by weight
{
if (list[j].weight > list[j + 1].weight)
{
swaps++;
swap_monsters(list, j, j + 1);
}
}
}
}
end_cpu = clock();
printf("Sort complete with %d comparisons and %d swaps.\n", comparisons, swaps);
print_clocks(end_cpu - start_cpu);
}
/* Highest-value finder for selection sort. */
int find_highest(monster* list, int n, int* comparisons, int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
char* highest_char = CHAR_MIN;
double highest_val = INT_MIN;
int highest_loc = -1;
int i;
for (i = 0; i <= n; i++)
{
(*comparisons)++;
if (use_name)
{
if (list[i].name > highest_char)
{
highest_loc = i;
strcpy(highest_char, list[i].name);
}
}
(*comparisons)++;
if (use_weight)
{
if (list[i].weight > highest_val)
{
highest_loc = i;
highest_val = list[i].weight;
}
}
}
return highest_loc;
}
/* Implement ascending selection sort. */
void selection_sort(monster* list, int n, int use_name, int use_weight)
{
int i;
int highest;
int comparisons = 0;
int swaps = 0;
clock_t start_cpu, end_cpu;
printf("Selection sort %d monsters by %s...\n", n, use_name ? "name" : "weight");
start_cpu = clock();
// YOUR CODE GOES HERE.
for (i = n - 1; i > 0; i--)
{
highest = find_highest(list, i, &comparisons, use_name, use_weight);
if (highest != i)
{
swaps++;
swap_monsters(list, highest, i);
}
}
end_cpu = clock();
printf("Sort complete with %d comparisons and %d swaps.\n", comparisons, swaps);
print_clocks(end_cpu - start_cpu);
}
/* Find position for insertion sort. */
int insertion_sort_find_position(monster* list, int low_index, int high_index, monster* k, int* comparisons, int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
if (use_name)
{
while (high_index >= low_index && list[high_index].name > k->name)
{
list[high_index + 1] = list[high_index];
high_index--;
(*comparisons)++;
}
}
if (use_weight)
{
while (high_index >= low_index && list[high_index].weight > k->weight)
{
list[high_index + 1] = list[high_index];
high_index--;
(*comparisons)++;
}
}
(*comparisons)++;
}
/* Implement insertion sort. */
void insertion_sort_internal(monster* list, int n, int* comparisons, int* copies, int* block_copies, int use_name, int use_weight)
{
// YOUR CODE GOES HERE.
for (int i = 0; i < n; i++)
{
monster* key = list + i;
int j = i - 1;
insertion_sort_find_position(list, 0, j, key, comparisons, use_name, use_weight);
(*block_copies)++;
//list = key;
(*copies)++;
}
}
/* Shell for insertion sort. */
void insertion_sort(monster* list, int n, int use_name, int...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here