In the following program a sorted vector of strings is maintained. Strings are added and removed from the vector during the execution of the program. When a string is added to the vector, it has to be placed in the proper position to maintain the ordering. When that position is located, the size of the vector must be increased by 1 (using the resize() method) and all elements starting from the located position must be moved up by one to allow the new string to be placed in the vector at that position. This can be done using the following steps……

ENGM2283 Data Structures and Algorithms
ASSIGNMENT # 7, Due date: Tuesday, March 16, 2021, 8:30 AM
1. In the following program a sorted vector of strings is maintained. Strings are added and
removed from the vector during the execution of the program.
When a string is added to the vector, it has to be placed in the proper position to maintain the
ordering. When that position is located, the size of the vector must be increased by 1 (using
the resize() method) and all elements starting from the located position must be moved up by
one to allow the new string to be placed in the vector at that position. This can be done using
the following steps:
Let v be the ve c t o r o f s t r i n g s , l e t count = v . s i z e ( ) and x = the s t r i n g
to be i n s e r t e d .
s t ep 1 : I n c r e a s e the s i z e o f v by 1 using v . r e s i z e ( count+1).
s t ep 2 : i f count == 0 then the c o r r e c t p o s i t i o n i s i = 0
el se s e ar ch for the c o r r e c t po s i t i on , i , using a loop s t a r t i n g at
i = 0 c ont inuing as long as v [ i ] < x .
s t ep 3 : Use a loop to go down the ve c t o r s t a r t i n g from index j = count
u n t i l j = i + 1 , moving e l ement s up by 1 index p o s i t i o n . That i s ,
v [ j ] = v [ j =1] .
s t ep 4 : I n s e r t x using v [ i ] = x .
When a string is removed, the position of the string in the vector must be located, all elements
of the vector after that position must be moved down by one, and the vector size must be
decreased by 1 (using the resize() method). You can assume that there are no duplicate values
in the vector. This can be done using the following steps:
Let v be the ve c t o r o f s t r i n g s , l e t count = v . s i z e ( ) and x = the s t r i n g
to be removed .
s t ep 1 : Use a loop to go up the ve c t o r s t a r t i n g from index i = 0 and break
the loop i f v [ i ] == x .
s t ep 2 : Use another loop to go up the ve c t o r s t a r t i n g from index j = i
moving e l ement s down by 1 index p o s i t i o n : that i s v [ j ] = v [ j +1] .
s t ep 3 : Re s i z e v using v . r e s i z e ( count =1).
For marking purposes add chris, bob, joe, bill, ruth, alan and amanda then print. Remove
joe and print, then remove ruth and print, then remove alan and print.
/* Fi l e : o r d e r e dLi s t . cpp
program to handle an ordered l i s t of s t r i n g s */
#include <s t r ing>
#include <ve c tor>
#include <ios t ream>
#include <f s t ream>
using namespace s td ;
void add ( ve c t o r <s t r ing> & v , s t r i n g x ) ;
void remove ( ve c t o r <s t r ing> & v , s t r i n g x ) ;
void wr i t e ( ostream & out , const ve c t o r <s t r ing> & v ) ;
Assignment # 7 2
int main ( void)
f
of s t r eam f out ( ” orde r edLi s tOut . txt ” ) ;
ve c t o r <s t r ing> v ;
char ch ;
s t r i n g x ;
while ( true ) f
// p r int a l i t t l e menu
cout << endl << endl << “a = add” << endl ;
cout << ” r = remove” << endl ;
cout << “d = d i s pl a y ” << endl ;
cout << ” f = p r i nt to f i l e ” << endl ;
cout << “q = qui t ” << endl << endl ;
c in >> ch ;
i f ( ch == ‘ a ‘ ) f
cout << endl << ” s t r i n g to add : ” ;
c in >> x ;
add (v , x ) ;
g el se i f ( ch == ‘ r ‘ ) f
cout << endl << ” s t r i n g to remove : ” ;
c in >> x ;
remove (v , x ) ;
g el se i f ( ch == ‘d ‘ ) f
wr i t e ( cout , v ) ;
g el se i f ( ch == ‘ f ‘ ) f
wr i t e ( fout , v ) ;
g el se i f ( ch == ‘ q ‘ ) f
break ;
g
g
f out . c l o s e ( ) ;
return 0 ;
g
// ////////////////////// implementat ion of f unc t i ons ///////////////////
Assignment # 7 3
2. Insertion sort is another sorting algorithm which is described as follows for a vector.
Algorithm : Think o f the ve c t o r as c o n s i s t i n g o f a s o r t ed po r t i on s t a r t i n g at
index 0 f o l l owed by an unsor t ed po r t i on . Repeatedly i n s e r t the
f i r s t element in the unsor t ed po r t i on o f the ve c t o r in the proper
p o s i t i o n in the s o r t ed po r t i on o f the ve c t o r .
For a vector v, this algorithm can be carried out with the following steps:
s t ep 1 : The s o r t ed po r t i on o f v i s i n i t i a l l y j u s t v [ 0 ] .
s t ep 2 : Use a for loop s t a r t i n g at i = 1 .
s t ep 3 : the s o r t ed po r t i on o f v i s from index 0 to i =1, and now x = v [ i ] has
to be pr ope r ly plac ed somewhere between index 0 and index i .
s t ep 4 : use another for loop to go down the ve c t o r s t a r t i n g from index j =
i =1, moving e l ement s up by 1 index p o s i t i o n i f the element i s g r e a t e r
than x ; that i s , i f v [ j ] > x then s e t v [ j +1] = v [ j ] .
s t ep 5 : when the loop on j breaks , pl a c e v [ i ] at index j +1; that i s , v [ j +1] = x .
s t ep 6 : increment i and go back to the top o f the for loop s t a r t e d in s t ep 2
Implement the insertion sort function in insertionSort.cpp and test your implementation using
testInsertionSort.cpp.
Declaration of class insertionSort
/* Fi l e : i n s e r t i o n S o r t . h
header f i l e f o r i n s e r t i o n s o r t func t i on */
#i fndef INSERTIONSORT H
#define INSERTIONSORT H
#include <ve c tor>
using namespace s td ;
void i n s e r t i o n S o r t ( ve c tor<int>& v ) ;
#endif /* INSERTIONSORT H */
Implementation of insertionSort
/* Fi l e : i n s e r t i o n S o r t . cpp
implementat ion of the i n s e r t i o n s o r t al gor i thm f o r an i n t e g e r v e c t o r
*/
#include ” i n s e r t i o n S o r t . h”
.
Main Program Using Class insertionSort
/* Fi l e : t e s t I n s e r t i o n S o r t . cpp
t e s t program f o r the i n s e r t i o n S o r t func t i on */
#include <ios t ream>
#include <ve c tor>
#include ” i n s e r t i o n S o r t . h”
int main ( void)
f
ve c tor<int> v ( 1 2 ) ;
v [ 0 ]=1 0 ; v [ 1 ]=9 ; v [ 2 ]=8 ; v [ 3 ]=1 1 ; v [ 4 ]=2 ; v [ 5 ]=5 ;
v [ 6 ]=7 ; v [ 7 ]=1 2 ; v [ 8 ]=3 ; v [ 9 ]=6 ; v [ 1 0 ]=4 ; v [ 1 1 ]=1 ;
Assignment # 7 4
cout << ” i n i t i a l ve c t o r : ” ;
for ( int i = 0 ; i < v . s i z e ( ) ; i++) f
cout << v [ i ] << ” ” ;
g
cout << endl ;
i n s e r t i o n S o r t ( v ) ;
cout << ” f i n a l ve c t o r : ” ;
for ( int i = 0 ; i < v . s i z e ( ) ; i++) f
cout << v [ i ] << ” ” ;
g
cout << endl ;
return 0 ;
g
/* Program output :
i n i t i a l v e c t o r : 10 9 8 11 2 5 7 12 3 6 4 1
f i n a l v e c t o r : 1 2 3 4 5 6 7 8 9 10 11 12 */
Assignment # 7 5
3. Write a program, insertionSortPerformance.cpp, similar to selectionSortPerformance.cpp
as shown in the course notes to compute and plot a graph of execution time of the insertionSort
algorithm versus vector size. Attach the plot to your solution on Brightspace.
For convenience, the source code for the selectionSortPerformance is shown below.
/* Fi l e : s e l e c t ionSor tPe r formanc e . cpp
measure the performance of s e l e c t i o n s o r t */
#include ” s e l e c t i o n S o r t . h”
#include <ios t ream>
#include <c s t d l i b > // needed f o r srand ( ) and rand ( )
#include <ctime> // needed c l o c k ( ) and CLOCKS PER SEC
int main ( void)
f
int n ; // v e c t o r s i z e
int i ;
f loat s e conds ; // t ime to car ry out the al gor i thm
c l o c k t t ; // used to record c l o c k value
s rand ( 1 2 9 7 7 5 4 8 ) ; // seed the random number g ene rator
cout << “n s e conds ” << endl ;
for (n = 10000; n <= 100000; n = n + 10000) f
ve c tor<int> v (n ) ;
// f i l l in the v e c t o r wi th random i n t e g e r s
for ( i = 0 ; i < n ; i++) f
v [ i ] = rand ( ) ;
g
// record the s t a r t t ime
t = c l o c k ( ) ;
// s o r t the v e c t o r
s e l e c t i o n S o r t ( v ) ;
// record the e l a p s e d t ime and conv e r t to seconds
t = c l o c k ( ) = t ;
s e conds = ( ( f loat ) t )/CLOCKS PER SEC;
cout << n << ” ” << s e conds << endl ;
g
return 0 ;
g
4. An analysis of the insertionSort algorithm similar to the analysis of the selectionSort in the
course notes shows that the worst case time complexity is O(n2) where n is the vector size.
Explain why insertionSort may be faster than selectionSort for a particular vector.
Assignment # 7 6
5. Make a template of the insertionSort function and test it on a vector of employees and sort
the list by name.
Do not modify the main program. Complete the program by writing the declaration and im-
plementation of the comparison operator (operator>) and the template function insertionSort.
For marking purposes, use the input le workforcein.txt
Declaration of Employee Class and InsertionSort
/* Fi l e : ins e r t ionSor tTemplat e . h
header f i l e f o r i n s e r t i o n s o r t func t i on */
#i fndef INSERTIONSORTTEMPLATE H
#define INSERTIONSORTTEMPLATE H
#include <ios t ream>
#include <f s t ream>
#include <s t r ing>
#include <ve c tor>
using namespace s td ;
clas s employee f
private :
s t r i n g name ; // l a s t name
int id ; // employee id number
f loat s a l a r y ; // employee s a l a r y
public :
employee ( s t r i n g n = “” , int i = 0 , f loat s = 0 . 0 ) ;
friend ostream & operator<< ( ostream & out , const employee & e ) ;
friend i s t r eam & operator>> ( i s t r eam & in , employee & e ) ;
// you f i l l in the d e c l a r a t i o n of the f r i e n d comparison ope rator ( operator >)
g ;
// you f i l l in the d e c l a r a t i o n and implementat ion of t emplat e func t i on i n s e r t i o n S o r t
#endif /* INSERTIONSORTTEMPLATE H */
Implementation of Employee Class and InsertionSort
/* Fi l e : ins e r t ionSor tTemplat e . cpp
implementat ion of the i n s e r t i o n s o r t t emplat e
*/
#include ” ins e r t i onSo r tTempl a t e . h”
employee : : employee ( s t r i n g n , int i , f loat s )
f
name = n ;
id = i ;
s a l a r y = s ;
g
ostream & operator<< ( ostream & out , const employee& e )
f
out << e . name << ” ” << e . id << ” ” << e . s a l a r y ;
return out ;
g
i s t r eam & operator>> ( i s t r eam & in , employee & e ) f
in >> e . name >> e . id >> e . s a l a r y ;
return in ;
g
// you f i l l in the d e f i n i t i o n of the comparison ope rator ( operator >) to s o r t employee r e cords by name
Assignment # 7 7
Main Program Using Employee Class and InsertionSort
/* Fi l e : t e s t Ins e r t i onSo r tTemp l a t e . cpp
t e s t s i n s e r t i o n s o r t on a v e c t o r of employees
Programmer : your name Date : */
#include ” ins e r t i onSo r tTempl a t e . h”
int main ( void) f
i f s t r e am f i n ( “wo rkf o r c e in . txt ” ) ;
ve c t o r <employee> v ; // empty v e c t o r
employee x ;
while ( f i n >> x )f // read from input f i l e u n t i l EOF i s reached
v . push back ( x ) ;
g
cout << ” p r i n t i n g be f o r e s o r t i n g by name . . ” << endl ;
for ( int i = 0 ; i < v . s i z e ( ) ; i++) f
cout << v [ i ] << endl ;
g
i n s e r t i o n S o r t ( v ) ;
cout << ” p r i n t i n g a f t e r s o r t i n g by name . . ” << endl ;
for ( int i = 0 ; i < v . s i z e ( ) ; i++) f
cout << v [ i ] << endl ;
g
f i n . c l o s e ( ) ;
return 0 ;
g
/* Program output :
p r i n t i n g b e f o r e s o r t i n g by name . .
Smith 123 34000
Jones 189 38000
Black 342 45000
White 111 49000
Green 200 35000
p r i n t i n g a f t e r s o r t i n g by name . .
Black 342 45000
Green 200 35000
Jones 189 38000
Smith 123 34000
White 111 49000
*/

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!
Open chat
1
You can contact our live agent via WhatsApp! Via + 1 (929) 473-0077

Feel free to ask questions, clarifications, or discounts available when placing an order.

Order your essay today and save 20% with the discount code SCORE