Skip to content

Instantly share code, notes, and snippets.

@xonev
Created September 21, 2012 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xonev/3763879 to your computer and use it in GitHub Desktop.
Save xonev/3763879 to your computer and use it in GitHub Desktop.
Coding Challenge Submissions
//
// Stephen Habegger (final submission -- removed some if/else statements)
//
void MergeIntoFirstArray(int[] firstArray, int[] mergeArray)
{
int f, m;
f = m = mergeArray.Length - 1;
for (int i = firstArray.Length - 1; i >= 0; i--)
{
if (m < 0 || (f >= 0 && firstArray[f] > mergeArray[m])) firstArray[i] = firstArray[f--];
else firstArray[i] = mergeArray[m--];
}
}
//
// Danny Fritz (final submission, Javascript -- fixed out-of-bounds/undefined problems)
//
function MergeIntoFirstArray(firstArray, secondArray) {
var cursorFirst = secondArray.length-1, cursorSecond = secondArray.length-1;
var nextSlot = firstArray.length-1;
while (nextSlot >= 0) {
if (cursorSecond < 0)
firstArray[nextSlot] = firstArray[cursorFirst--];
else if (cursorFirst < 0)
firstArray[nextSlot] = secondArray[cursorSecond--];
else
if (firstArray[cursorFirst] >= secondArray[cursorSecond])
firstArray[nextSlot] = firstArray[cursorFirst--];
else if (firstArray[cursorFirst] < secondArray[cursorSecond])
firstArray[nextSlot] = secondArray[cursorSecond--];
nextSlot--;
}
//
// Angel Shishkov (final {only} submission)
//
void MergeIntoFirstArray(int[] firstArray, int[] mergeArray)
{
//Assuming both arrays started at same size N and the firstArray's size was doubled
//Start both indexes at the end of the original array size (starts with largest value, since they are sorted)
//Algorithm proceeds backwards and inserts the correct value in the firstArray - complexity is O(n) or better
int firstIndex = mergeArray.Length-1; //Index for firstArray
int mergeIndex = mergeArray.Length-1; //Index for mergeArray
int insertIndex; //Index in firstArray to insert to
// Start inserting at the end of the resized array and move backwards
for (insertIndex = firstArray.Length-1; insertIndex >= 0; --insertIndex)
{
//If the mergeArray is fully processed, we're done
if (mergeIndex == -1)
return;
//If the firstArray is fully processed, we're just adding the rest of the mergeArray
if (firstIndex == -1)
{
firstArray[insertIndex] = mergeArray[mergeIndex--];
continue;
}
if (mergeArray[mergeIndex] >= firstArray[firstIndex])
{
//If the number in the mergeArray is >= the number in the firstArray, move it to the current first array insert position and decrement the mergeIndex
firstArray[insertIndex] = mergeArray[mergeIndex--];
}
else
{
//If the number in the firstArray is > the number in the mergeArray, move it to the current first array insert position and decrement the firstIndex
firstArray[insertIndex] = firstArray[firstIndex--];
}
}
}
//
// John Trujillo (final submission -- fixed an index out-of-bounds problem)
//
void MergeIntoFirstArray(int[] firstArray, int[] mergeArray)
{
int cIdx = firstArray.Count() - 1;
int fIdx = mergeArray.Count() - 1;
int mIdx = fIdx;
while(mIdx >= 0)
{
firstArray[cIdx--] = fIdx < 0 || firstArray[fIdx] <= mergeArray[mIdx] ?
mergeArray[mIdx--] : firstArray[fIdx--];
}
}
//
// Rob Montague (didn't have a chance to revise)
//
void MergeIntoFirstArray(int[] firstArray, int[] mergeArray)
{
// get the count of empty slots then subtract one to get total number of fills
int yetToFill = NumberOfEmptyValues(firstArray) - 1;
int mergeArrayFillLocation = mergeArray.Length - 1;
//Backwards fill the target array so you don't overwrite values
for(var firstArrayLocation = firstArray.Length - 1;firstArrayLocation>0;firstArrayLocation--)
{
if(mergeArray[mergeArrayFillLocation] > firstArray[yetToFill] ||
yetToFill < 0)
{
firstArray[firstArrayLocation] = mergeArray[mergeArrayFillLocation];
mergeArrayFillLocation--;
}
else
{
firstArray[firstArrayLocation] = firstArray[yetToFill];
yetToFill--;
}
}
}
int NumberOfEmptyValues(int[] array){
var emptyValueCount = 0;
for(var i = 0; i < array.Length; i++){
if(array[i] == 0){
emptyValueCount ++;
}
}
return emptyValueCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment