Question

-I need help with C++. I'm working with an array of words. The array size is...

-I need help with C++. I'm working with an array of words. The array size is 1000 but not all 1000 slots will be filled. I run an insertion sort algorithm and then output the array but the sorted array output includes the unused empty slots so there is a lot of blank spaces before the sorted words appear.

-The sorting algorithm seems to be reading in the empty array slots as well. Is there a way to read an array and only work with the non-empty slots? In other words, how do I filter out the array so that it will exclude the blanks? I can't be sorting and printing out the blank array slots, only the slots with a word on it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I can suggest you few methods for the same:

method 1:

Since Insertion sort is based on sequential sorted frame checking so it checks that the elements before the current cell is sorted or not.

You can use vectors<string>new_array;

and traverse the array and only push the non_blank elements int the vector<string> and then perform sort on this vector<string>

so now you have eliminated the blank string blocks.

if you want to preserve the array index,you could create a vector of pairs of type pair<sting,int>

vector<pair<string,int>> and apply insertion sort on this then just reassign the elements according to the new index.

you could also create a function to convert this back to the array of words with only non_blank slots then perforn sorting.

Method2:

you can create a Boolean array bool has_a_word[1000];

then traverse through the whole array of words in O(n) complexity and store true in the array index where words are non_blank and false other wise

bool has_a_word[1000];

for(int i=0;i<1000;i++)

{

if(arr[I]==" ")

has_a_word[I]=false;

else

has_a_word[I]=true;

}

then while operating the insertion sort code function you could just check if it has a word then only check for the sorted order in the previous words else just iterate ahead and the same while checking and compare functions.

and

while printing the array you could just print the elements or the words with bool value true from the Boolean array.

Add a comment
Know the answer?
Add Answer to:
-I need help with C++. I'm working with an array of words. The array size is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • I'm trying to do an insertion sort in which i sort the characters of a string,...

    I'm trying to do an insertion sort in which i sort the characters of a string, what do i replace inputString.charAt(j) with? note: //arr[j] = arr[j-1] is an example that my professor used for an array. (just a reminder for myself) Written in Java lang public static String sort(String inputString) if (inputS tring null) t //insertion sortsort characters for(int i = 0; i inputstring.length(); i++) int index - inputString.charAt(i); int j- i; while( i> 0 && inputString.charAt (i-1) > index)...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT