Need some help with this c++ code When finished your function will sort the list
ID: 642598 • Letter: N
Question
Need some help with this c++ code
When finished your function will sort the list of strings in alphabetical order. The main sorting function is the shellSort() function, which takes in an array of strings along with the number that indicates the total number of items in the list. The shellSort() function will call the intervalInsertionSort()
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void printItems(string list[], int length);
void shellSort(string list[], int length);
void intervalInsertionSort(string list[], int begin, int length, int inc);
int main(int argc, char **argv)
{
string starGateCharacters[] = {
"Picard", "Riker", "Data", "La Forge", "Worf", "Dr. Crusher",
"Dr. Pulaski", "Wesley", "Troi", "Tasha", "Sisko", "Odo",
"Reed", "Travis", "Hoshi", "Dr. Phlox", "Kirk", "Spock",
"Chakotay", "Janeway", "Neelix", "Seven of Nine", "Tuvok",
"Doctor", "Harry", "Tom", "Kes", "Archer", "T'Pol", "Tucker",
"Dax", "O'Brien", "Quark", "Dr. Bashier", "Kira", "B'Elanna",
"Bones", "Scotty", "Chekov", "Uhura", "Sulu", "Nurse Chapel"
};
int numberOfCharacters = 42;
// Print the unsorted items
cout << "Items unsorted:" << endl;
printItems(starGateCharacters, numberOfCharacters);
// Sort the items
shellSort(starGateCharacters, numberOfCharacters);
// Print the sorted items
cout << "Items sorted:" << endl;
printItems(starGateCharacters, numberOfCharacters);
cout << " ** Press any key to continue **";
getchar();
return 0;
}
void printItems(string list[], int length)
{
for (int i=0; i < length; i++)
{
cout << " " << list[i] << endl;
}
cout << endl;
return;
}
void shellSort(string list[], int length)
{
// TODO: implement the details of the shellSort function here.
return;
}
void intervalInsertionSort(string list[], int begin, int length, int inc)
{
// TODO: implement the details of the intervalInsertionSort function
// here.
return;
}
Explanation / Answer
// I did the shellSort function. Can you explain what the intervalInsertionSort function do?
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void printItems(string list[], int length);
void shellSort(string list[], int length);
void intervalInsertionSort(string list[], int begin, int length, int inc);
int main(int argc, char **argv)
{
string starGateCharacters[] = {
"Picard", "Riker", "Data", "La Forge", "Worf", "Dr. Crusher",
"Dr. Pulaski", "Wesley", "Troi", "Tasha", "Sisko", "Odo",
"Reed", "Travis", "Hoshi", "Dr. Phlox", "Kirk", "Spock",
"Chakotay", "Janeway", "Neelix", "Seven of Nine", "Tuvok",
"Doctor", "Harry", "Tom", "Kes", "Archer", "T'Pol", "Tucker",
"Dax", "O'Brien", "Quark", "Dr. Bashier", "Kira", "B'Elanna",
"Bones", "Scotty", "Chekov", "Uhura", "Sulu", "Nurse Chapel"
};
int numberOfCharacters = 42;
// Print the unsorted items
cout << "Items unsorted:" << endl;
printItems(starGateCharacters, numberOfCharacters);
// Sort the items
shellSort(starGateCharacters, numberOfCharacters);
// Print the sorted items
cout << "Items sorted:" << endl;
printItems(starGateCharacters, numberOfCharacters);
cout << " ** Press any key to continue **";
getchar();
return 0;
}
void printItems(string list[], int length)
{
for (int i=0; i < length; i++)
{
cout << " " << list[i] << endl;
}
cout << endl;
return;
}
void shellSort(string list[], int length)
{
// TODO: implement the details of the shellSort function here.
for (int i = length/2; i > 0; i /= 2)
{
for (int j = i; j < length; j += 1)
{
string temp = list[j];
int k;
for (k = j; k >= i && list[k - i] > temp; k -= i)
list[k] = list[k - i];
list[k] = temp;
}
}
return;
}
void intervalInsertionSort(string list[], int begin, int length, int inc)
{
// TODO: implement the details of the intervalInsertionSort function
// here.
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.