Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include using namespace std; / / Let's get started. Given an array of strings, swap the positions of / / the elements at idx 1
#include using namespace std; Let's get started. Given an array of strings, swap the positions of the elements at idx and idx For example, if names contains ABCDE and we call swapnames names will contain ADCBE on output, swapping element B with element D void swapstring names int idx int idx This one is a bit more complex but read this carefully to get an unsterstanding of what it does... names is a partially sorted array of strings itemIndexToInsert is the index of the first item in the array that HAS NOT been sorted yet. For example, on input names may contain ACBED and itemIndexToInsert is This means that items and have been sorted and item B is the first item that has not been sorted it The job of this function is to move the element at the index itemIndexToInsert into the sublist that is immediately before it in the array. In our example we need to move the B into the proper position in the sublist AC The algorithm: loop from itemIndexToInsert backwards count down to instead of up in the loop compare the element at itemIndexToInsert with the previous element in the list itemIndexToInsert if the they are out of order... swap them call your swap function decrease itemIndexToInsert by else things are in order so stop your loop void insertItemFromIndexstring names int itemIndexToInsert loop from itemIndexToInsert backwards count down to instead of up in the loop compare the element at itemIndexToInsert with the previous element in the list itemIndexToInsert if the they are out of order... swap them call your swap function decrease itemIndexToInsert by else things are in order so stop your loop Given an array of strings and the size, use insertion sort to put the elements into ascending alphabetical order. Start by assuming that the first element is already sorted, so we can loop from index skip the first element Loop on all of the unsorted elements from the nd to the last call the insertItemFromIndex function, passing the array and the index of the current element to insert void insertionSortstring names int size Loop on all of the unsorted elements from the nd to the last call the insertItemFromIndex function, passing the array and the index of the current element to insert Print out the array with a comma and space between each element. I wrote this one for you but if you want to try it yourself, feel free to delete the code and rewrite it ; void printArraystring names int size forint i ; i size; i cout namesi; if i size cout ; cout endl; Code to test your functions... int main string swapTestABCD; cout "Testing swap. Before swapping: ; printArrayswapTest; cout "Swapping element and ; swapswapTest; printArrayswapTest; cout "Swapping element and ; swapswapTest; printArrayswapTest; cout "Swapping element and ; swapswapTest; printArrayswapTest; string insertTestACDB; cout "Testing insertItemFromIndex. Before insertItemFromIndex: ; printArrayinsertTest; cout "Inserting element Expect B to be moved between A and C ; insertItemFromIndexinsertTest; printArrayinsertTest; string names "Twenty One Pilots", "The Beatles", "Pink Floyd", ACDC "Metallica", "Nirvana", "Arctic Monkeys", "Imagine Dragons", "The Black Keys", "Tame Impala" ; cout "Testing sort. Before sorting: ; printArraynames; insertionSortnames; cout "After Sorting: ; printArraynames; return ;
#include
using namespace std;
Let's get started. Given an array of strings, swap the positions of
the elements at idx and idx
For example, if names contains ABCDE and we call
swapnames names will contain ADCBE on output,
swapping element B with element D
void swapstring names int idx int idx
This one is a bit more complex but read this carefully to get an unsterstanding of what it does...
names is a partially sorted array of strings
itemIndexToInsert is the index of the first item in the array that HAS NOT been sorted yet.
For example, on input names may contain ACBED and itemIndexToInsert is
This means that items and have been sorted and item B is the first item that has not been
sorted it
The job of this function is to move the element at the index itemIndexToInsert into the sublist
that is immediately before it in the array.
In our example we need to move the B into the proper position in the sublist AC
The algorithm:
loop from itemIndexToInsert backwards count down to instead of up in the loop
compare the element at itemIndexToInsert with the previous element in the list itemIndexToInsert
if the they are out of order...
swap them call your swap function
decrease itemIndexToInsert by
else
things are in order so stop your loop
void insertItemFromIndexstring names int itemIndexToInsert
loop from itemIndexToInsert backwards count down to instead of up in the loop
compare the element at itemIndexToInsert with the previous element in the list itemIndexToInsert
if the they are out of order...
swap them call your swap function
decrease itemIndexToInsert by
else
things are in order so stop your loop
Given an array of strings and the size, use insertion sort to put the elements into ascending
alphabetical order.
Start by assuming that the first element is already sorted, so we can loop from index skip the first element
Loop on all of the unsorted elements from the nd to the last
call the insertItemFromIndex function, passing the array and the index of the current element to insert
void insertionSortstring names int size
Loop on all of the unsorted elements from the nd to the last
call the insertItemFromIndex function, passing the array and the index of the current element to insert
Print out the array with a comma and space between each element.
I wrote this one for you but if you want to try it yourself, feel free to
delete the code and rewrite it ;
void printArraystring names int size
forint i ; i size; i
cout namesi;
if i size cout ;
cout endl;
Code to test your functions...
int main
string swapTestABCD;
cout "Testing swap. Before swapping:
;
printArrayswapTest;
cout "Swapping element and
;
swapswapTest;
printArrayswapTest;
cout "Swapping element and
;
swapswapTest;
printArrayswapTest;
cout "Swapping element and
;
swapswapTest;
printArrayswapTest;
string insertTestACDB;
cout "Testing insertItemFromIndex. Before insertItemFromIndex:
;
printArrayinsertTest;
cout "Inserting element Expect B to be moved between A and C
;
insertItemFromIndexinsertTest;
printArrayinsertTest;
string names
"Twenty One Pilots",
"The Beatles",
"Pink Floyd",
ACDC
"Metallica",
"Nirvana",
"Arctic Monkeys",
"Imagine Dragons",
"The Black Keys",
"Tame Impala" ;
cout "Testing sort. Before sorting:
;
printArraynames;
insertionSortnames;
cout "After Sorting:
;
printArraynames;
return ;
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