Question
// File: List.cpp #include #include #include List.h using namespace std; template // insertion sort using vector void insertionSort(vector& vec) { for(j=1; j { T key=vec[j]
// File: List.cpp
#include
#include
#include "List.h"
using namespace std;
template
// insertion sort using vector
void insertionSort(vector& vec)
{
for(j=1; j
{
T key=vec[j]
int i= j-1
while( i > -1 && vec[i] > key)
{
vec[i+1]=vec[i];
--i;
}
vec[i+1] = key;
}
}
template
void insertionSort(List& list){
for(auto j = ++list.begin(); j != list.end(); ++j){
//cout << *j++ << endl;
T key= *j;
auto i = list.begin();
while (i >-1 && *i> key){
*i++ = *i; --i;
}
*i++=key;
}
}
int main()
{
List theList{};
for(int i=0; i<100; 1++)
theList.push_back(rand()%100);
insertionSort(theList);
for(float elem: theList)
cout<< elem <<", ";
cout<
auto itr = theList.begin();
while (itr != theList.end())
cout << *itr++ <
T key = *j;
// manipulate iterator to sort the LinkedList directly use the same algorithm that we use for vectors. then pass your list to the insertionSort method and when the sorting completes, print the result to display the sorted elements
// apply list operations, like insert, push_back, push_front, pop_back, pop_front, void clear() and other.
// call sort here insertionSort
// print out the elements again to prove they are sorted
}
Compare the running time vector vs linked list using the chrono library. Which implementation takes longer linked list or vector? Why? Write a small paragraph comment at the top of your main explaining why you got the result you got when you timed the two implementations. Discuss time complexity.
Thanks
you will implement the insertion sort algorithm (see pseudocode below) on a doubly-linked list (instead of an array).
A doubly-linked list is just a linked list where each node in the list has both a pointer to the next node and a pointer to the previous node. The first node in the list has a nullptr to its previous node. The last node in the list has a nullptr to its next node.
Step 1: create your own doubly-linked list class or check lecture 7 (code taken from Weiss textbook) on how to create one in C++. You may use the implementation from lecture 7, if you want.
Step 2: implement insertion sort using Linked List as input. The function signature should look something like this:
template
LinkedList
//your code here, eventually returns a sorted
}
Note the use of pass-by-reference and the use of templates.
Do not simply convert the LinkedList to an array, sort using an array, and then convert it back! You must sort the LinkedList directly, otherwise the exercise loses its value. The purpose of the assignment is to get familiar with pointer manipulation and linked list operations.
In your main, you should initialize a linked list and use a for loop combined with a random number generator to fill it with some random elements. Print the initial random list to display the elements. Then, pass your list to the insertionSort method (as above) and when the sorting completes, print the result to display the sorted elements.
Re-code insertion sort using C++s vector class instead of a linked list as the underlying container (vector is basically a fancy version of array). Compare the running time (vector vs linked list) using the chrono library. Which implementation takes longer (linked list or vector)? Why? Write a small paragraph comment at the top of your main explaining why you got the result you got when you timed the two implementations. Discuss time complexity.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started