1,105,556 Community Members

double sorted arrays

Member Avatar
sahiti1988
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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},
{ }


please help me out with this question

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

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.

Member Avatar
Kayy
Newbie Poster
6 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
sahiti1988
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

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);
}
Member Avatar
sahiti1988
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for your reply............
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...............
Thanks in advance............

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
2
 
int[] b = new int[0];
int[] c = new int[0];

Arrays of size zero...sounds useful. :icon_rolleyes:

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: