Question
This lab will test your implementation of insertionSortNM. See the assignment PDF on D2L for full details. You can pattern your insertionSortNM after the insertionSort
This lab will test your implementation of insertionSortNM. See the assignment PDF on D2L for full details.
You can pattern your insertionSortNM after the insertionSort we did in class, or the version in the book. I've provided a version of StdArray that contains a swap() function (use that in place of the author's exch() function), and I've included a version of less() in the StdSort template that I provided.
The test program creates a small array and tests it for various values of m.
The version of StdArray that I provided also contains implementations of upperBound() and rotateRight().
-----------------------------------------------------------------------------------------
public class StdArray {
public static
T t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
// Returns index of first element strictly greater than key.
//
// Preconditions:
// 0
// a != null or lo == hi
// a[lo,hi) != null
// a is sorted
// Postconditions (let ret be the return value):
// a is unchanged
// lo
// a[lo,ret) key
public static
while (lo
// lo' is the original value of lo
// hi' is the original value of hi
// a[lo',lo) key
int mid = lo + (hi - lo) / 2;
if (a[mid].compareTo(key)
lo = mid + 1;
else
hi = mid;
}
// a[lo',ret) key
return lo;
}
public static
return upperBound(a, 0, a.length, key);
}
public static
T t = a[--hi];
for (int i = hi; i > lo; i--)
a[i] = a[i-1];
a[lo] = t;
}
public static
rotateRight(a, 0, a.length);
}
public static
for (int i = 1; i
if (a[i-1].compareTo(a[i]) > 0)
return false;
return true;
}
}
--------------------------------------------------------------------------------------------
Current file: StdSort.java Load default template... 1 public class StdSort { 3 4. public static
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started