SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Implementing A Collection Class
Exercise:
Exercises
Question:6 | ISBN:9780136091813 | Edition: 2

Question

Add the following method to the ArrayIntList class from this chapter.

Write a method called isPairwiseSorted that returns whether or not a list of integers is pairwise sorted. A list is considered pairwise sorted if each successive pair of numbers is in nondecreasing order. For example, if a variable

called list stores [3, 8, 2, 5, 19, 24, –3, 0, 4, 4, 8, 205, 42] , then the call of list.isPairwiseSorted() should return true because the successive pairs of this list are all sorted: (3, 8), (2, 5), (19, 24), (–3, 0), (4, 4), (8, 205) . The extra value 42 at the end had no effect on the result because it is not part of a pair. If the list had instead stored [7, 42, 308, 409, 19, 17, 2] , then the method should return false because the pair (19, 17) is not in sorted order. If a list is so short that it has no pairs, then it is considered to be pairwise sorted.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Implementation of isPairwiseSorted method:

	public boolean isPairwiseSorted()
	{
		for(int i = 0; i < size - 1; i += 2)
		{
			if(elementData[i] > elementData[i + 1])
				return false;
		}

		return true;
	}
0 0

Discussions

Post the discussion to improve the above solution.