---
# System prepended metadata

title: Binary Search C#

---

# Binary Search C#
```
public class Solution {
    public int Search(int[] nums, int target)
    {
        int min = 0, max = nums.Length - 1, mid = (max - min) / 2;
        while(nums[mid] != target)
        {
            min = nums[mid] > target ? mid : min;
            max = nums[mid] > target ? max : mid;
        }
        return mid;
    }
}
```
# First Bad Version C#
```
public class Solution : VersionControl {
    public int FirstBadVersion(int n)
    {
        for(int i =0;i < n;i++){
            if(IsBadVersion(i) == true) return i;
        }
        return n;
    }
```
# Guess Number Higher or Lower C#
```
public class Solution : GuessGame {
    public int GuessNumber(int n) {
        int n1 = 0,i = 0,n2 = 0,n3 = 0;
        while(guess(n1) != 0)
        {
            if(guess(n1) == 1)
            {
                n1 = (int)Math.Pow(2,i);
                i++;
            }
            else if(guess(n1) == -1)
            {
                n2 = (int)Math.Pow(2,i - 1);
                n3 = (int)Math.Pow(2,i);
                n1 = (n2 + n3) / 2;
            }
        }
        return n1;
    }
}
```
# Sort an Array C#
## Bubble sort氣泡排序法
```
public class Solution {
        public int[] SortArray(int[] nums)
        {
            int n = 0;
            for(int i =0;i < nums.Length;i++)
                for(int i1 =0;i1 < nums.Length -1;i1++)
                {
                    if(nums[i1] > nums[i1 + 1])
                    {
                        n = nums[i1];
                        nums[i1] = nums[i1 + 1];
                        nums[i1 + 1] = n;
                    }
                }
            return nums;
        }
}
```
## Generally Sort一般的排序法
```
public class Solution {
        public int[] SortArray(int[] nums)
        {
            List<int> ans = new List<int>(), n = nums.ToList();
            while(n.Count != 0)
            {
                ans.Add(n.Min());
                n.Remove(n.Min());
            }
            return ans.ToArray();
        }
}
```
## Selection sort選擇排序法
```
public class Solution {
        public int[] SortArray(int[] nums)
        {
            int n = 0;
            for(int i =0;i < nums.Length;i++)
            {
                n = i;
                for(int i1 = i + 1;i1 < nums.Length;i1++)
                    if (nums[i] > nums[i1]) n = i1;
                int n1 = nums[i];
                nums[i] = nums[n];
                nums[n] = n1;
            }
            return nums;
        }
}
```
## Insertion sort插入排序法
```
public class Solution {
        public int[] SortArray(int[] nums)
        {
            int j, Temp;
            for (int i = 1; i < nums.Length; i++)
            {
                Temp = nums[i];
                j = i - 1;
                while (Temp < nums[j])
                {
                    nums[j + 1] = nums[j];
                    j--;
                    if (j == -1) break;
                }
                nums[j + 1] = Temp;
            }
            return nums;
        }
}
```