Question: C# 1. Sort an array of integers, in which all the odd positions will be in an ascending order and all the even positions will
C#
1. Sort an array of integers, in which all the odd positions will be in an ascending order and all the even positions will be in a descending order.
Input: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Sorted: {0, 9, 1, 8, 2, 7, 3, 6, 4, 5}
public int[] SortEvenOdds(int[] a) {
2. Sort an array of strings; do not use built-in function; use string's compareTo function.
public string[] SortStrings(string[] array) {
3. Given an array, sort the array by moving from the front and then moving from the back until the array is sorted to the center position. For an array from 0 to 9, the "sorted" array goes like this: {0, 2, 4, 6, 8, 9, 7, 5, 3, 1}.
public void SortBackAndForth(int[] a) {
4. Sort an array with both strings and integers. Keep both types at the same positions without changing these types' positions. You may use built-in conversions.
public object[] SortIntStrings(object[] array) {
5. Given a rectangular array, perform a sort on each and every column in the array. This is like a table in your spreadsheet, where you are sorting each and every column.
public int[,] SortAllColumns(int[,] a) {
6. Given a rectangular array, perform an insertion sort on each and every diagonal segment that go in the south-east direction (i.e., rightwards and downwards) without crossing other diagonal segments. See examples.
/// Input Sorted
/// 12 11 10 9 2 1 5 9
/// 8 7 6 5 3 7 6 10
/// 4 3 2 1 4 8 12 11
/// You are sorting the following segments:
/// 4, 8 3, 12 7 2, 11 6 1, 10 5, and 9.
///
///
/// Input Sorted
/// 20 19 18 17 5 9 13 17
/// 16 15 14 13 1 10 14 18
/// 12 11 10 9 2 6 15 19
/// 8 7 6 5 3 7 11 20
/// 4 3 2 1 4 8 12 16
/// You are sorting the following segments:
/// 4, 8 3, 12 7 2, 16 11 6 1, 20 15 10 5, 19 14 9, 18 13, and 17.
Return the reference to the original array
public int[,] DiagonalSort(int[,] a) {
7. Given two separate subarrays that are both sorted in an array, merge the two subarrays together so that the elements in these two subarrays are sorted. We will assume that subarrays a and b will not overlap and that subarray a always comes before subarray b.
public void Merge(int[] array, int aFirst, int aLast, int bFirst, int bLast) {
8. Merge Sort - Partition and then merge: This will divide the subarray from the first index to the last index into half, and then recursively partition and merge the halves until only one element remains. You will need to call the Merge method.
cref="Merge(int[], int, int, int, int)
public void PartitionAndMerge(int[] array, int firstIndex, int lastIndex) {
9. Merge sort. This will call the PartitionAndMerge method on the entire array rather than the subarray. Make sure you've checked for null references.
cref="PartitionAndMerge(int[], int, int)
public void MergeSort(int[] array) {
10. Given a subarray (between first index and last index), swap all the elements (from first to last - 1) that are smaller than the last element to before a specific position. We will call this position the pivot because it is a fixed position. Finally, we need to move the last element to the pivot. ///
public int Pivot(int[] array, int firstIndex, int lastIndex) {
11. We will first pivot our subarray to get the pivot index. Since the pivot will be fixed even after the array is sorted, we will partition the subarray into two halves: before the pivot and after the pivot. This will call recursively.
cref="Pivot(int[], int, int)
public void PartitionAndPivot(int[] array, int firstIndex, int lastIndex) {
12. Quick sort an array by invoking the PartitionAndPivot method on the entire array.
cref="PartitionAndPivot(int[], int, int)
public void QuickSort(int[] array) {
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
