本文整理汇总了C#中List.BinarySearch方法的典型用法代码示例。如果您正苦于以下问题:C# List.BinarySearch方法的具体用法?C# List.BinarySearch怎么用?C# List.BinarySearch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类List
的用法示例。
在下文中一共展示了List.BinarySearch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindCandidatePeaks
/// <summary>
/// Use binary search to find all Isotopic Peaks we want to consider when looking for the cross-links shifts.
/// </summary>
/// <param name="completePeakList">The complete list of Isotopic Peaks that we will use for searching.</param>
/// <param name="minimumMz">The minimum m/z value to consider.</param>
/// <param name="scanLc">The LC Scan to consider.</param>
/// <param name="scanIms">The IMS Scan to consider.</param>
/// <returns>All peaks that are in the given LC Scan and IMS Scan and m/z >= thegiven m/z of the Feature.</returns>
public static List<IPeak> FindCandidatePeaks(List<IsotopicPeak> completePeakList, double minimumMz, int scanLc, int scanIms)
{
// Set up Peak Comparer to use for binary search later on
AnonymousComparer<IsotopicPeak> peakComparer = new AnonymousComparer<IsotopicPeak>((x, y) => x.ScanLc != y.ScanLc ? x.ScanLc.CompareTo(y.ScanLc) : x.ScanIms != y.ScanIms ? x.ScanIms.CompareTo(y.ScanIms) : x.Mz.CompareTo(y.Mz));
IsotopicPeak lowPeak = new IsotopicPeak {ScanLc = scanLc, ScanIms = scanIms, Mz = minimumMz, Intensity = 1};
IsotopicPeak highPeak = new IsotopicPeak { ScanLc = scanLc, ScanIms = scanIms + 1, Mz = 0, Intensity = 1 };
int lowPeakPosition = completePeakList.BinarySearch(lowPeak, peakComparer);
int highPeakPosition = completePeakList.BinarySearch(highPeak, peakComparer);
lowPeakPosition = lowPeakPosition < 0 ? ~lowPeakPosition : lowPeakPosition;
highPeakPosition = highPeakPosition < 0 ? ~highPeakPosition : highPeakPosition;
List<IPeak> candidatePeaks = new List<IPeak>();
for (int j = lowPeakPosition; j < highPeakPosition; j++)
{
IsotopicPeak peak = completePeakList[j];
MSPeak msPeak = new MSPeak(peak.Mz, peak.Intensity, 0.05f, 1);
candidatePeaks.Add(msPeak);
}
return candidatePeaks;
}
示例2: Main
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort");
dinosaurs.Sort();
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":");
int index = dinosaurs.BinarySearch("Coelophysis");
if (index < 0)
{
dinosaurs.Insert(~index, "Coelophysis");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");
index = dinosaurs.BinarySearch("Tyrannosaurus");
if (index < 0)
{
dinosaurs.Insert(~index, "Tyrannosaurus");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
}
示例3: Main
static void Main()
{
Console.WriteLine("Enter the word:");
string word = Console.ReadLine();
List<char> wordSymbols = new List<char>();
for (int i = 0; i < word.Length; i++)
{
wordSymbols.Add(word[i]);
}
//using the ACII table for filling o f the array with leters
List<char> letters = new List<char>();
for (int i = 65; i < 122; i++)
{
letters.Add((char)i);
//jump to lowercase "a"
if (i==90)
{
i = 96;
}
}
//sorting the char array in order to use BinarySearch for representation of the letter index
letters.Sort();
for (int i = 0; i < wordSymbols.Count; i++)
{
Console.WriteLine("The postion of the letter {0} is {1}",wordSymbols[i], letters.BinarySearch(wordSymbols[i]));
}
Console.WriteLine("\nThe array with letters is ordered as follows /index -> value/:");
for (int i = 0; i < letters.Count; i++)
{
Console.Write("{1} -> {0}; ", letters[i], i);
}
Console.WriteLine();
}
示例4: Main
static void Main(string[] args)
{
List<Contact> phoneBook = new List<Contact>();
Contact p1 = new Contact("Gerry", "123");
Contact p2 = new Contact("Test", "456");
Contact p3 = new Contact("Test3", "789");
//add one contact
phoneBook.Add(p1);
//add two contacts
phoneBook.AddRange(new Contact[]{p2,p3});
//remove the first contact
//phoneBook.RemoveAt(0);
//find the contact in the List, based on the compareTo method
Contact ctofind = new Contact("Gerry", "");
//write
Console.WriteLine(phoneBook.BinarySearch(ctofind));
foreach (Contact p in phoneBook)
Console.WriteLine(p.Name + " " + p.Telnumber);
Console.ReadLine();
}
示例5: Main
static void Main(string[] args)
{
checked
{
Stopwatch sw = new Stopwatch();
List<BigInteger> squares = new List<BigInteger>();
BigInteger pmax = 0;
sw.Start();
for (BigInteger i = 1; i < 1000; i++)
{
//Console.WriteLine(i);
BigInteger temp = i * i;
squares.Add(temp);
}
for (int D = 2; D < 1001; D++)
{
if (squares.BinarySearch(D) >= 0) continue;
BigInteger limit = (BigInteger)Math.Sqrt(D);
int result = 0;
BigInteger m = 0;
BigInteger d = 1;
BigInteger a = limit;
BigInteger x1 = 1;
BigInteger x = a;
BigInteger y1 = 0;
BigInteger y = 1;
while (x * x - D * y * y != 1)
{
m = d * a - m;
d = (D - m * m) / d;
a = (limit + m) / d;
BigInteger numm2 = x1;
x1 = x;
BigInteger denm2 = y1;
y1 = y;
x = a * x1 + numm2;
y = a * y1 + denm2;
}
if (x > pmax)
{
pmax = x;
result = D;
Console.WriteLine("*************D={0} Y={1} x={2}", D, y, x);
}
}
sw.Stop();
Console.WriteLine("Elapsed time {0} ms", sw.ElapsedMilliseconds);
}
}
示例6: ImportFromDataset
public static List<IRating> ImportFromDataset(string filename, List<string> users, List<string> artists, int limit = int.MaxValue)
{
TextReader reader = new StreamReader(filename);
var ratings = new List<IRating>();
string line;
var sep = new[] {"\t"};
while ((line = reader.ReadLine()) != null && limit > 0)
{
limit--;
var parts = line.Split(sep, StringSplitOptions.None);
var userIndex = users.BinarySearch(parts[0]);
if (userIndex < 0)
continue;
ratings.Add(new Rating(
userIndex,
artists.BinarySearch(parts[2]),
float.Parse(parts[3], CultureInfo.InvariantCulture)
));
}
reader.Close();
return ratings;
}
示例7: Main
static void Main()
{
List<int> array = new List<int>{5,3,2,8,4,1,3,6};
array = QuickSort(array);
Console.WriteLine(String.Join(" ", array));
Console.WriteLine(array.BinarySearch(10));
}
示例8: FindAllPeaks
public static IList<Peak> FindAllPeaks(List<Peak> peakList, double minMz, double maxMz)
{
//var index = peakList.BinarySearch(new Peak(mz, 0.0), comparer);
//return index < 0 ? null : peakList[index];
var index = peakList.BinarySearch(new Peak((minMz + maxMz) / 2, 0));
if (index < 0) index = ~index;
var matchedPeakList = new List<Peak>();
// go down
var i = index - 1;
while (i >= 0 && i < peakList.Count)
{
if (peakList[i].Mz <= minMz) break;
matchedPeakList.Add(peakList[i]);
--i;
}
// go up
i = index;
while (i >= 0 && i < peakList.Count)
{
if (peakList[i].Mz >= maxMz) break;
matchedPeakList.Add(peakList[i]);
++i;
}
matchedPeakList.Sort();
return matchedPeakList;
}
示例9: Main
static void Main(string[] args)
{
List<string> nameList = new List<string>();
while (true)
{
Console.WriteLine("Enter a name.");
string newName = Console.ReadLine();
if (newName == "")
{
break;
}
int result = nameList.BinarySearch(newName);
if (result < 0)
{
int index = ~result;
Console.WriteLine("Insert at index {0}.", index);
nameList.Insert(index, newName);
}
else
{
int index = result;
Console.WriteLine("Found at index {0}.", index);
}
for (int index = 0; index < nameList.Count(); ++index)
{
Console.WriteLine(index + ": " + nameList[index]);
}
Console.WriteLine();
}
}
示例10: FindVertices
public override List<Vertex> FindVertices(List<Vertex> vertices, Vector3 position, double maxDistanceToConsiderVertexAsSame)
{
List<Vertex> foundVertexes = new List<Vertex>();
Vertex testPos = new Vertex(position);
int index = vertices.BinarySearch(testPos, this);
if (index < 0)
{
index = ~index;
}
// we have the starting index now get all the vertices that are close enough starting from here
double maxDistanceToConsiderVertexAsSameSquared = maxDistanceToConsiderVertexAsSame * maxDistanceToConsiderVertexAsSame;
for (int i = index; i < vertices.Count; i++)
{
if (Math.Abs(vertices[i].Position.x - position.x) > maxDistanceToConsiderVertexAsSame)
{
// we are too far away in x, we are done with this direction
break;
}
AddToListIfSameEnough(vertices, position, foundVertexes, maxDistanceToConsiderVertexAsSameSquared, i);
}
for (int i = index - 1; i >= 0; i--)
{
if (Math.Abs(vertices[i].Position.x - position.x) > maxDistanceToConsiderVertexAsSame)
{
// we are too far away in x, we are done with this direction
break;
}
AddToListIfSameEnough(vertices, position, foundVertexes, maxDistanceToConsiderVertexAsSameSquared, i);
}
return foundVertexes;
}
示例11: PosTest2
public bool PosTest2()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest2: The generic type is a referece type of string");
try
{
string[] strArray = { "apple", "banana", "chocolate", "dog", "food" };
List<string> listObject = new List<string>(strArray);
int result = listObject.BinarySearch("egg");
if (result != -5)
{
TestLibrary.TestFramework.LogError("003", "The result is not the value as expected,The result is: " + result);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例12: Main
static void Main()
{
List<int> list = new List<int>();
Console.WriteLine("Enter array elements: \nFor end enter 'n'");
while (true)
{
string number = Console.ReadLine();
int value;
bool isNum = int.TryParse(number, out value);
if (isNum)
{
list.Add(value);
}
else
{
break;
}
}
Console.Write("Searchrd element is: ");
int n = int.Parse(Console.ReadLine());
list.Sort();
int index = list.BinarySearch(n);
Console.WriteLine("Index of searched element is: {0}", list[index]);
}
示例13: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: The generic type is int");
try
{
int[] iArray = { 1, 9, 3, 6, 5, 8, 7, 2, 4, 0 };
List<int> listObject = new List<int>(iArray);
listObject.Sort();
int i = this.GetInt32(0, 10);
int result = listObject.BinarySearch(i);
if (result != i)
{
TestLibrary.TestFramework.LogError("001", "The result is not the value as expected,The result is: " + result);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例14: Main
static void Main(string[] args)
{
List<int> firstArr = new List<int>();
int num = 1;
int l1 = 0;
Console.Write("Element {0} of the array(write some bad input to finish):", l1);
l1++;
while (int.TryParse(Console.ReadLine(), out num))
{
Console.Write("Element {0} of the array(write some bad input to finish):", l1);
firstArr.Add(num);
l1++;
}
Console.Write("Element:");
int element = int.Parse(Console.ReadLine());
firstArr.TrimExcess();
firstArr.Sort();
Console.WriteLine();
Console.WriteLine();
DateTime start = DateTime.Now;
int index = firstArr.BinarySearch(element);
DateTime stop = DateTime.Now;
Console.WriteLine("The index of the element found with Binary Search: {0}",index);
Console.WriteLine("It took {0} milliseconds",stop-start);
}
示例15: GetClosestMassIdx
public static int GetClosestMassIdx(List<float> argPeaks, float argMZ)
{
if (argPeaks.Count == 0)
{
return -1;
}
int KeyIdx = argPeaks.BinarySearch(argMZ);
if (KeyIdx < 0)
{
KeyIdx = ~KeyIdx;
}
int ClosetIdx = 0;
double ClosestValue = 10000.0;
for (int i = KeyIdx - 2; i <= KeyIdx + 2; i++)
{
if (i >= 0 && i < argPeaks.Count)
{
if (Math.Abs(argPeaks[i] - argMZ) <= ClosestValue)
{
ClosestValue = Math.Abs(argPeaks[i] - argMZ);
ClosetIdx = i;
}
}
}
return ClosetIdx;
}