public boolean inSort ( double element ) ; /* A form of ins operations, but here we do not insert based on index; rather we look through the list and find the first place where there are two element values that "straddle" the element we are adding... meaning we find a spot in the list where one element is <= to the item we are adding, and the next element (next higher index) is > the one we are adding. The new element goes between these two. If the head element is > the one we are adding, then the new element goes at the head; if all elements in the list are <= the one we are adding, the new element goes at the end. return true if the add succeeds (it will almost always succeed); return false if for some reason the insert cannot happen (such as in Array Implementation, the array has no room for another element). If the list is empty, just insert the element into the list. Example: list is 3.1, 5.5, 18.2, 23.01 inSort(7.0) makes this list: 3.1, 5.5, 7.0, 18.2, 23.01 Example: list is 3.1, 5.5, 18.2, 23.01 inSort(1.5) makes this list: 1.5, 3.1, 5.5, 18.2, 23.01 Example: list is 3.1, 5.5, 18.2, 23.01 inSort(55.2) makes this list: 3.1, 5.5, 18.2, 23.01, 55.2 Also note the we do not assume the list is sorted when we start looking for a "straddle" so here is a strange (but correct) example Example: list is 1.2, 5.5, 17.03, 12.8, 6.6 inSort(19.0) makes this list: 1.2, 5.5, 17.03, 12.8, 6.6, 19.0 Note: if we use only inSort to add items to a list (dont use ins) then the list will be in increasing sorted order at all times */ *NOTE THAT THIS METHOD SHOULD NOT SORT THE LIST, RATHER WE ARE JUST INSORTING A NUMBER" LOOK AT EXAMPLE LISTS AND DESCRIPTION PLEASE AND THANK YOUI"
public boolean inSort ( double element ) ;
/* A form of ins operations, but here we do not insert based on index;
rather we look through the list and find the first place where there
are two element values that "straddle" the element we are adding...
meaning we find a spot in the list where one element is <= to the item
we are adding, and the next element (next higher index) is > the one we
are adding. The new element goes between these two.
If the head element is > the one we are adding, then the new element goes
at the head; if all elements in the list are <= the one we are adding, the
new element goes at the end.
return true if the add succeeds (it will almost always succeed); return false
if for some reason the insert cannot happen (such as in Array Implementation,
the array has no room for another element). If the list is empty, just
insert the element into the list.
Example: list is 3.1, 5.5, 18.2, 23.01
inSort(7.0) makes this list: 3.1, 5.5, 7.0, 18.2, 23.01
Example: list is 3.1, 5.5, 18.2, 23.01
inSort(1.5) makes this list: 1.5, 3.1, 5.5, 18.2, 23.01
Example: list is 3.1, 5.5, 18.2, 23.01
inSort(55.2) makes this list: 3.1, 5.5, 18.2, 23.01, 55.2
Also note the we do not assume the list is sorted when we start looking
for a "straddle" so here is a strange (but correct) example
Example: list is 1.2, 5.5, 17.03, 12.8, 6.6
inSort(19.0) makes this list: 1.2, 5.5, 17.03, 12.8, 6.6, 19.0
Note: if we use only inSort to add items to a list (dont use ins)
then the list will be in increasing sorted order at all times
*/

Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images









