Question

In: Computer Science

We saw in class different sorting algorithms, and we generally assumed the input to be sorted...

We saw in class different sorting algorithms, and we generally assumed the input to be sorted wasj an array. Here, the provided input is an unordered linked list of n elements with integer values. We are interested in sorting this list in increasing order (smallest first), in O(n log n) worst case time, while using constant space (also called in-place sorting). Note that recursion requires additional space on the stack, so should EC330 Applied Algorithms and Data Structures for Engineers, Fall 2020 be avoided in this case.

You are provided with the following files: LNode.h, LNode.cpp, LSorter.h, LSorter.cpp, and main.cpp.

A linked list node, LNode, is implemented in LNode.h and LNode.cpp. The LSorter class with the sortList method are declared in LSorter.h. The sortList method, which you need to implement, is declared as follows:

class LSorter { public: LNode* sortList(LNode* head); };

This method accepts as input the head node of the list to be sorted, and returns the head node of the sorted linked list. Your implementation should be included in the LSorter.cpp file. You may add additional classes and/or methods as you see fit, but everything should be included in this file, and none of the other files may be modified.

Finally, the provided main.cpp file may be used to test your implementation. You may assume that your input consists of non-negative integers with a maximal value of 1,000,000. Modify and submit LSorter.cpp only. The submitted file should compile and run with the rest of the files. We will run it against large linked list and measure the run time.

Partial credit will be given for an in-place solution with a runtime that is worse than O(n log n).

LNode.h:

#ifndef LNode_h
#define LNode_h

#include <stdio.h>

class LNode {
public:
    int val;
    LNode* next;
    LNode(int x=0);
};

#endif /* LNode_h */

LNode.cpp:

#include "LNode.h"

LNode::LNode(int x) {
        val = x;
        next = nullptr;
    }

Lsorter.h:

#ifndef LSorter_h
#define LSorter_h

#include <stdio.h>
#include "LNode.h"

class LSorter {
public:
    LNode* sortList(LNode* head);
};

#endif /* LSorter_h */

Lsorter.cpp:

#include <algorithm>
#include "LSorter.h"
using namespace std;

LNode* LSorter::sortList(LNode* head){
        // Your code goes in here...
    
}

main.cpp:

#include <iostream>
#include "LNode.h"
#include "LSorter.h"

using namespace std;

int main(int argc, const char * argv[]) {
    
    LNode LNode1(5);
    LNode LNode2(1);
    LNode LNode3(7);
    LNode LNode4(13);
    LNode LNode5(2);
    LNode LNode6(9);
    
    LNode1.next = &LNode2;
    LNode2.next = &LNode3;
    LNode3.next = &LNode4;
    LNode4.next = &LNode5;
    LNode5.next = &LNode6;
    
    LSorter solution;
    LNode* head = solution.sortList(&LNode1);
    
    LNode* currnode = head;
    while (currnode != nullptr){
        cout << currnode->val << endl;
        currnode = currnode->next;
    }           

    return 0;
}

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • LNode.cpp
  • LNode.hpp
  • LSorter.cpp
  • LSorter.hpp
  • main.cpp

LNode.cpp:

#include "LNode.hpp"

LNode::LNode(int x)

{

val = x;

next = 0; //nullptr;

}



LNode.hpp:

#ifndef LNode_hpp

#define LNode_hpp

#include <stdio.h>

class LNode {

public:

int val;

LNode* next;

LNode(int x=0);

};

#endif /* LNode_hpp */


LSorter.cpp:

#include "LSorter.hpp"

#include "LNode.hpp"

#include <iostream>

LNode* LSorter::sortList(LNode* head)

{

int size=1;//size of list

int count = 0;//number of mergeSorts executed

int first_size;//size of first half

int second_size;//size of second half

LNode *end;

LNode *left;

LNode *right;

LNode *next;

//if the list has 0 or 1 elements, return the list

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

{

return head;

}

while (count == 0 || count > 2)//if program doesn't enter second loop-- list sorted

{

count=1;

left=head;

end=0;

head=0;

//loop through array until end

while (left != NULL)

{

right=left;

second_size=size;

first_size=0;

count++;//keep track of number of Merge Sorts

//change right LNode and size of first half of list to split list

while (right != NULL && first_size < size)

{

first_size++;

right=right->next;

}

//move through both first list and second list, changing next LNode based on sorting conditions

while ((second_size > 0 && right != NULL) || first_size > 0)

{

//if first half empty, use next right value

if (first_size == 0)

{

next=right;

right=right->next;

second_size--;

}

//if second half empty, use next left value

else if (second_size == 0 || right == NULL)

{

next=left;

left=left->next;

first_size--;

}

//compare actual values of the LNodes

else if (right->val > left->val)

{

next=left;

left=left->next;

first_size--;

}

//move through

else

{

next=right;

right=right->next;

second_size--;

}

//update class variables of end

if (end != NULL)

{

end->next=next;

}

else

{

head=next;

}

//keep track of end of list by setting to next value

end=next;

}

//set values for next iteration of mergesort

left=right;

}

//when finished,set pointer to next value to NULL and update size of list

end->next=0;

size = size * 2;

}

return head;

}




LSorter.hpp:

#ifndef LSorter_hpp

#define LSorter_hpp

#include <stdio.h>

#include "LNode.hpp"

class LSorter {

public:

LNode* sortList(LNode* head);

};

#endif /* LSorter_hpp */


main.cpp:

#include <iostream>

#include "LNode.hpp"

#include "LSorter.hpp"

using namespace std;

int main(int argc, const char * argv[]) {

LNode LNode1(5);

LNode LNode2(1);

LNode LNode3(7);

LNode LNode4(13);

LNode LNode5(2);

LNode LNode6(9);

LNode1.next = &LNode2;

LNode2.next = &LNode3;

LNode3.next = &LNode4;

LNode4.next = &LNode5;

LNode5.next = &LNode6;

LSorter solution;

LNode* head = solution.sortList(&LNode1);

LNode* currnode = head;

while (currnode != nullptr){

cout << currnode->val << endl;

currnode = currnode->next;

}

return 0;

}

Sample Output Screenshots:


Related Solutions

In class, we saw that countries with high levels of schooling attainment generally have higher income...
In class, we saw that countries with high levels of schooling attainment generally have higher income and growth rates than countries with low levels of schooling attainment. Based only on the model of individual schooling decisions discussed in class, what are two characteristics which you would expect to be systematically different between countries with high vs. low schooling attainment?
In class, we saw that countries with high levels of schooling attainment generally have higher income...
In class, we saw that countries with high levels of schooling attainment generally have higher income and growth rates than countries with low levels of schooling attainment. Based only on the model of individual schooling decisions discussed in class, what are two characteristics which you would expect to be systematically different between countries with high vs. low schooling attainment?
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function void sort(int arr[ ], int size) which, when overridden, will sort the array by calling...
Estimating and testing for means We will work with the following (sorted) data, assumed to represent...
Estimating and testing for means We will work with the following (sorted) data, assumed to represent a simple random sample from a normal distribution −1.0173 −0.3243 0.36416 0.96709 −0.7724 −0.1004 0.49382 0.98607 −0.7492 −0.0661 0.49734 1.08517 −0.6438 0.08449 0.51945 1.15909 −0.5727 0.11064 0.64080 1.23061 −0.4562 0.15022 0.66312 1.30328 −0.3986 0.25865 0.84739 1.57912 The usual summaries are Sum 7.83952 Count 28 Sum of squares 16.1280 1. Confidence Intervals Determine a confidence interval for the mean of the normal distribution we sampled...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
We saw in class that each LP can be transformed into an equivalent LP in any...
We saw in class that each LP can be transformed into an equivalent LP in any of the following two forms below: (1) maxcTx: Ax=b, x≥0 (2) maxcTx: Ax≤b. Can we always transform any LP in an LP of the form Prove your answer correct. maxcTx: Ax=b?
1. In the first class we saw that a number of foreign aerospace firms (led by...
1. In the first class we saw that a number of foreign aerospace firms (led by Bombardier of Canada) are producing and exporting aerospace products, including small airplanes and helicopters, from the state of Querétaro in Mexico. a. Why is this surprising? Would traditional theories of trade such as absolute or comparative advantage lead you to expect Mexico to be exporting aerospace products such as airplanes or helicopters? Why or why not? b. What factors or causes could explain why...
Que. As we saw in the class lecture one of the most controversial ideas of Marx...
Que. As we saw in the class lecture one of the most controversial ideas of Marx was his quote: “Religion is the opium of the people”. Many people now discuss the decline of the role of religion in people’s lives and wonder if Marx’s concept may be outdated. Here is your chance to update Marx’s ideas. For this question, you are to substitute the word “media” for the word “religion” in the above quotation. In other words, the quote you...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
Question 1: [55 marks] When we derived money demand function in class, we assumed that money...
Question 1: [55 marks] When we derived money demand function in class, we assumed that money demand depends on income and interest rate. Consider an economy that its money demand does not depend on income and is only a function of interest rate. M d = L ( i ) Suppose that the economy is an open economy that is on a áexible exchange rate system. 1. Draw the money demand and supply curves with money demand and supply on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT