- I need to create an insertion sort within an array of integers. These integers are drawn from a random integer from a range (0,499). The integer will be inserted in the correct position of the array and sorted.

This next step is where I'm having trouble understanding it.

-Insert 100 integers into the array (one per iteration), which will begin with a size of 1. When the

array is fi lled up, a new array of twice the size is created and the contents of the existing array is copied into the new array and the algorithm will continue.

For example the output will give me a sorted array with 1 element and print out that value as the output. I will then increase this array by 2*(the last array size) until it reaches 128 elements.

I'm am able to implement the 100 integers and sort it to correctly output the contents. I'm just not sure how I would copy an array and display 1 element or 2 elements or 4 elements etc.. Each time it iterates.

I hope this makes sense please ask if i need to clarify something.

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand((unsigned)time(NULL)); int i, key; int num[100] = {0}; cout << "Unsorted Array:" << endl; for (i=1; i<101;i++) { num[i] = (rand())%499; cout << i << ":" << num[i] << endl; } for (int j=1; j<101; j++) //Insertion Sort key = num[j]; while (i>=0 && num[i]>key) { num[i+1] = num [i]; i--; } num[i+1] = key; } cout << endl << "Sorted Array:\t" << endl; for (i=1;i<101;i++) { cout << i << ":" << num[i]<< endl; } int newArray[1] = {0}; return 0; }

Thanks guys. I may be thinking too hard on this. From what I can understand I have to create a for loop that will keep going until there's 128 elements. This first array only has 1 element and is displayed and the next element displays the 2 elements. I can't seem to figure out how to copy the first array into this new array because I keep getting index out of bounds error since the array size can't support it.