I need a program to reverse part of a list between two terminals.
Example :
List: 1, 2, 3, 3, 5, 4
Output: 1, 2, 3, 3, 4, 5 (Only the 4 and 5 are inverted)
I found this:
positionCrepe.Reverse(indexOfMaxToSearch, positionCrepe.Count);
But it doesn't work because I have a mistake:
System.ArgumentException: The offset and length were out of bounds for this table or the number is greater than the number of index elements at the end of the source collection.
However
indexOfMaxToSearch = 2
and
positionCrepe.count = 5
and so it does not exceed the index of the table
Anyone have a solution?
Thank you.
The second argument is how many elements you want to reverse, not how many elements there are in the list.
So if you want to reverse everything starting from indexOfMaxToSearch, you want to reverse positionCrepe.Count - indexOfMaxToSearch elements:
positionCrepe.Reverse(indexOfMaxToSearch, positionCrepe.Count - indexOfMaxToSearch);
The error message is actually saying that the first argument plus the second argument is out of range of the array.
if you look at the definition of Reverse,
index: The zero-based starting index of the range to reverse.
count: The number of elements in the range to reverse.
You can use the following to make it work. Count must be less then the remaining indecies
positionCrepe.Reverse(2, positionCrepe.Count - 2);
Related
In Preview 3 of .NET Core 3.0, Microsoft explained in more detail how Index and Range work.
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
In this case we have array with 10 elements.
Using:
int[] subsetNums = nums[2..6];
This would take the elements: {3,4,5,6}. Starting with the element at index 2—which makes perfect sense—but ending with element at index 5. Why wouldn't it take the 6th element since you have put 6 in the brackets?
The end index you specify is exclusive. One advantage of this approach is it makes it easier to reason about the number of elements you're extracting:
2..6 extracts 4 elements, which is 6 - 2.
I have List and its values is ("Brandenburg","Alabama" and "Alberta"). When i used BinarySearch("Brandenburg") method, it returns -4 instead of 0. but i can get the correct index, when sorted this list. Why it returns wrong value if I use the unsorted list?. And I have also get the correct index from IndexOf("Brandenburg") method. Which method is useful that i can use?.
Thanks in Advance,
Prithivi
It MUST be sorted, to use binary search. The reason you're getting -4 is;
Your collection isn't sorted and for binary search the list will 'cut' in half each iteration. So:
When it starts, the topIndex == 0 and bottom = 2
TopIndex -> (0) "Brandenburg",
(1) "Alabama"
BottomIndex -> (2) "Alberta
The binarysearch will check the item in the middle: (2-0) / 2 = 1. If you're searching for Brandenburg. It will compare Alabama with your search item. The letter B is 'bigger' than letter 'A'. So it moves the topIndex to index 1.
(0) "Brandenburg",
TopIndex -> (1) "Alabama"
BottomIndex -> (2) "Alberta
Then it will compare to the next 'middle' item. In this case again Alabama. (2-1) / 2 = 1. It will also be compare to the bottomIndex, but this is the last one.
When binarysearch returns a negative number, it means that the item cannot be found. The negative number is the Index where it should be inserten. (-result -1) So if you want the new item added, it should be inserted on index (--4 -1) == 3
Let me explain how binary search works.
Say you have this array:
{1, 3, 5, 7, 10, 15, 20}
And I want to find the index of 15. What binary search will do is that it looks at the middle of the array, 7. Is 7 greater or less than 15? If it is less than 15, do the same thing again on the second half of the array (10, 15, 20). If it is greater than 15, do it on the first half (1, 3, 5). If it is equal to 15, then that means 15 is found.
This means that the array must be sorted for binary search to work. This explains why doing a binary search on your array returns a negative number. Because obviously, the method can't find the string you requested using the binary search algorithm.
You can get the correct index with IndexOf. This is because IndexOf uses a linear search to find the item. It looks at each element in the array and compare to the one that you're finding. Therefore, whether the array is sorted doesn't matter.
Note: I have not read the source code of IndexOf. It might use a binary search if it finds that the array is sorted. This is only my guess.
This question already exists:
Closed 11 years ago.
Possible Duplicate:
Fastest way to get range complement
I have a sorted array of nonoverlaping ranges for example (0,2],(2,4],(6,9] and I wish to get it's complement with (0,12] which shoud return (4,6],(9,12] .Whats the fastest way to do that?
Assume your input data is an array of this form:
{ 0, 2, 2, 4, 6, 9 }
Simply add the new elements 0 and 12 to the beginning and end, and you have
{ 0, 0, 2, 2, 4, 6, 9, 12 }
And reinterpreting consecutive pairs as intervals, you have:
(0, 0]
(2, 2]
(4, 6]
(9, 12]
The fact that you have degenerate intervals makes this something of a mess, but if your original list did not have any degenerate intervals, your output list would not either.
Depending on the format of your data and whether you can do in-place modification, this operation may be O(1).
I think it takes O(n) assuming n as the size of the sorted array.
Because you should check the gap between every adjacent ranges.
P.S. I guess it is your homework!
create one list of 2*n numbers. {a[0] .. a[2n-1]} by merging all intervals. It's sorted by construction.
ignore the pairs (a[i], a[i+1]) where i is odd and a[i]==a[i+1].
put at the front the lowest possible value.
put at the back the highest possible value.
splice two by two, you obtain the complement.
Can anyone explain why this is happening?
ie. Even when 175 is present in the array at location 7, the array.binarysearch is returning a negative value?
Please see this image:
Code http://www.freeimagehosting.net/uploads/555fef4560.jpg
Did you sort your array beforehand? BinarySearch expects the array to be sorted. Otherwise, it may return incorrect results.
Also, you should check for >= 0, not > 0. The element can be present at index 0.
From the picture, the array is 220 elements and you only show the first 7. All 220 elements must be sorted, otherwise BinarySearch will fail.
If for instance you only use the first num elements, use BinarySearch(0, num, 175)
Make sure the array is sorted
Make sure the object you are searching for is of the same type as the objects int he array. It helps to use the generic version:
Array.BinarySearch(..)
You could use Type specific version to make sure your input parameters are correct if the array is sorted:
int[] array = new int[] { 1, 3, 4, 5, 175, 200, 300, 400 };
int index = Array.BinarySearch<int>(array, 175);
You'll get a compilation error if the input array or search parameter is not of type int.
Is there any built-in C# support for doing an index sort?
More Details:
I have several sets of data stored in individual generic Lists of double.
These are lists always equal in length, and hold corresponding data items, but these lists come and go dynamically, so I can't just store corresponding data items in a class or struct cleanly. (I'm also dealing with some legacy issues.)
I need to be able to sort these keyed from any one of the data sets.
My thought of the best way to do this is to add one level of indirection, and use an index based sort. Such sorts have been in use for years.
Quick definition of index based sort :
make "index", an array of consecutive integers the same length as the lists, then the sort algorithm sorts the list of integers so that anylist[index[N]] gives the Nth item of anylist in sorted order. The lists themselves are never re-ordered.
Is there any built-in C# support for doing an index sort?
I have been unable to find it... everything I have found reorders the collection itself. My guess is support exists but I haven't looked in the right place yet.
I am using C#.NET 3.5, under Windows.
Once you have set up the index array, you can sort it using a custom Comparison<T> that compares the values in the corresponding items in the data array:
Array.Sort<int>(index, (a,b) => anylist[a].CompareTo(anylist[b]));
The following code achievs an indexed sort. Note the ToArray() call to clone the data array. If omited, the data array becomes sorted, too.
static void Main(String[] args)
{
Int32[] data = new Int32[] { -6, 6, 5, 4, 1, 2, 3, 0, -1, -2, -3, -4, -5 };
Int32[] indices = Enumerable.Range(0, data.Length).ToArray();
Array.Sort(data.ToArray(), indices);
foreach (Int32 index in indices)
{
Console.Write(String.Format("{0} ", data[index]));
}
Console.ReadLine();
}
The output is as exspected.
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6