An array is defined to be double sorted if its even-valued elements (if any) are in ascending order and its odd-valued elements (if any) are in ascending order.

The array {-6, 12, 1, 24, 3, 5} is double sorted because the even-valued elements (-6, 12, 24) are in ascending order and so are the odd-valued elements (1, 3, 5). However, the array {3, 2, 1} is not double sorted because the odd numbers are not in ascending order.

Write a function named isDoubleSorted that returns 1 if its array argument is double sorted, otherwise it returns 0.

If you are programming in Java or C#, the function signature is
int isDoubleSorted(int[ ] a)
If you are programming in C or C++, the function signature is
int isDoubleSorted(int a[ ], int len) where len is the number of elements in a.

Other double sorted arrays include:
{2, 4, 32},
{2, 2, 2, 1, 1, 1},
{1, 19, 23},
{1, 2},
{2, 1},
{8},
{17},
{ }

All 6 Replies

Start by checking if an array is sorted. Or in your teacher's terms "single sorted". :icon_rolleyes: Once you have the logic for determining if a sequence of values is in sorted order or not, you can apply a filter such that only even numbers are considered or only odd numbers are considered. Then run the algorithm twice: once for even numbers and once for odd numbers. Problem solved.

Since I don't what you've already tried or where you're stuck, and this looks like a homework problem, I'll describe how I would approach the question.

You need to keep track of two things at any given time, an even number and an odd number. Because you're only interested in whether or not the numbers are arranged in ascending order, you only need to compare each number (even or odd) to the corresponding number that preceded it.

So for each number in the array, you should check to see whether it's even or odd, and then compare it to the appropriate type of number which came before it. While each comparison matches the ascending pattern, you can continue to examine the array. If any comparison fails, then you will know that the array cannot be double sorted.

Start by checking if an array is sorted. Or in your teacher's terms "single sorted". :icon_rolleyes: Once you have the logic for determining if a sequence of values is in sorted order or not, you can apply a filter such that only even numbers are considered or only odd numbers are considered. Then run the algorithm twice: once for even numbers and once for odd numbers. Problem solved.

can you do it through coding.......after this....

``````int[] a={5,2,4,3,1};
int value=a.Length-1;
int temp;
for (int j = 0; j < value; j++)
{
for (int i = 0; i < value; i++)
{
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int k = 0; k < a.Length; k++)
Console.WriteLine(a[k]);``````

this give normal sorting .....and the output for this is ....
output:
1 2 3 4 5

Since there is no mention of ["what's NOT allowed"], I think a final method for isDoubleSorted could look like this (in C#)

``````private static bool isDoubleSorted(int[] a)
{
if (!isSingleSorted(a, isOdd))
return false;

return isSingleSorted(a, isEven);
}``````

I tried to do this ......

``````namespace double_sorted_array
{
class Program
{
static void Main(string[] args)
{
int[] a = { 6, 12, 1, 24, 3, 5 };
Console.WriteLine(isDoubleSorted(a));
}

static bool isDoubleSorted(int[] a)
{
int i, j, k;
int[] b = new int[0];
int[] c = new int[0];
bool res = false;
for (i = 0,j=0,k=0; i < a.Length; i++)
{
if (a[i] % 2 == 0)
{
b[j] = a[i];
j++;
}
else
{
c[k] = a[i];
k++;
}
}
if (issortArray(b))
{
if (issortArray(c))
{
res = true;
}
else
res = false;
}
else
res = false;

return res;
}

static bool issortArray(int[] a)
{
bool res=false;
int value = a.Length - 1;

for (int j = 0; j < value; j++)
{
for (int i = 0; i < value; i++)
{
if (a[i] > a[i + 1])
{
res = false;
}
else
res = true;
}
}
return res;
}
}
}``````

but, I am getting an error...it says....."array out of bounds in line 13 and 26.....can anyone help me get this done...............
``````int[] b = new int[0];