Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I am in need of a sort function that uses an insertion sort to sort strin

ID: 3624596 • Letter: H

Question

Hello,

I am in need of a sort function that uses an insertion sort to sort strings into alphabetical order. Having trouble coming up with the code. I have the code for an integer which looks like:

void insertionSort(int array[],int length)
{
int location; //i
int firstOutOfOrder; //j
int temp;
//firstOutofOrder = 1 is the second element in array
for( firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++)
{
temp = array[firstOutOfOrder];
location = firstOutOfOrder-1;

//while location in array is greater than temp AND this location in array is greater than or equal to 0
while( array[location] > temp && location >= 0 )
{
array[location +1 ]=array[location];
location--;
}
array[location + 1] = temp;
}
}


having trouble figuring out a string version!

Explanation / Answer

From what I can tell, you appear to be using either C or C++ as a programming language. In both languages a string is nothing more than a char array. Thus the following changes should get you started...

void insertionSort(char array[],int length)
{

...

char temp;

...

}



Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote