总结
1.视频资源P24-P30:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
Part1 查找算法
1)顺序查找(暴力查找)与二分查找P24;
2)顺序查找(暴力查找)与二分查找性能比较P25;
Part2 有序数组
3)新数据结构:有序数组P26-P28;
4)基于有序数组实现集合P29;
5)基于有序数组实现映射/字典P30。
Part3 2021/10/25、26回溯
1)根据伪代码编写程序;
2)二分查找法:mid=l+(r-l)/2
具体内容
Part1查找算法
P24 顺序查找(暴力查找)与二分查找
1、二分查找概念;
2、创建查找类;
3、编写静态二分查找方法;
4、查询算法复杂度分析。
//TestSearch类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class TestSearch
{
public int BinarySearch(int[] arr,int target)
{
int l = 0;
int r = arr.Length - 1;
while (l<=r)
{
int mid = ((r - l) + l) / 2;
if (arr[mid] < target)
{
l = mid + 1;
}
else if (arr[mid] > target)
{
r = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
}
}
P25 查找算法性能比较
Part2有序数组
P26-28 有序数组及有序数组的创建
1、有序数组基本操作;
2、创建有序数组类;
3、实现有序数组Rank方法;
4、实现有序数组增、删元素方法;
5、实现有序数组相关方法。
//有序数组类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class SortedArray1<Key> where Key : IComparable<Key>
{
private Key[] keys;
private int N;
public SortedArray1(int capacity)
{
keys = new Key[capacity];
}
public SortedArray1()
{
keys = new Key[10];
}
#region 返回数组容量、元素个数、判断数组是否为空
//返回数组容量
public int Capacity
{
get { return keys.Length; }
}
//返回数组中元素个数
public int Count
{
get { return N; }
}
//判断数组是否为空
public bool IsEmpty
{
get { return N == 0; }
}
#endregion
public int Rank(Key key)
{
int l = 0;
int r = N - 1;
while (l <= r)
{
//这里的问题 有毒 写成了(l + (r - l) )/ 2
int mid = l + (r - l) / 2;
if (keys[mid].CompareTo(key) < 0)
{
l = mid + 1;
}
else if (keys[mid].CompareTo(key) > 0)
{
r = mid - 1;
}
else
{
return mid;
}
}
return l;
}
public void Add(Key key)
{
int index = Rank(key);
if (N == keys.Length)
{
RestCapacity(keys.Length * 2);
}
if (index<N && keys[index].CompareTo(key)==0)
{
return;
}
else
{
for (int i = N-1 ; i >= index; i--)
{
keys[i+1] = keys[i];
}
keys[index] = key;
N++;
}
}
public void Remove(Key key)
{
if (IsEmpty)
{
return;
}
int index = Rank(key);
if (index < N && keys[index].CompareTo(key) != 0)
{
return;
}
for (int i = index + 1; i < N; i++)
{
keys[i - 1] = keys[i];
}
N--;
keys[N] = default(Key);
if (N == keys.Length / 4)
{
RestCapacity(keys.Length / 2);
}
}
#region 扩容操作
private void RestCapacity(int newCapacity)
{
Key[] newKeys = new Key[newCapacity];
for (int i = 0; i < N; i++)
{
newKeys[i] = keys[i];
}
keys = newKeys;
}
#endregion
//重写 ToString()
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append("[");
for (int i = 0; i < N; i++)
{
res.Append(keys[i]);
if (i != N - 1)
{
res.Append(",");
}
}
res.Append("]");
return res.ToString();
}
//找到最小值
public Key Min()
{
if (IsEmpty)
{
throw new ArgumentException("数组为空");
}
else
{
return keys[0];
}
}
//找到最大值
public Key Max()
{
if (IsEmpty)
{
throw new ArgumentException("数组为空");
}
else
{
return keys[N-1];
}
}
//返回索引为k的键
public Key Select(int k)
{
if (k<0 || k>=N)
{
throw new ArgumentException("数组索引异常");
}
else
{
return keys[k];
}
}
//判断数组中是否包含值为key的键
public bool Contains(Key key)
{
int index = Rank(key);
if (index<N && keys[index].CompareTo(key) == 0)
{
return true;
}
else
{
return false;
}
}
//找出小于或等于key的最大键
public Key Floor(Key key)
{
int index = Rank(key);
if (index < N && keys[index].CompareTo(key) == 0)
{
return keys[index];
}
if (index ==0)
{
throw new ArgumentException("不存在小于或等于key的最大键");
}
else
{
return keys[index-1];
}
}
//找出大于或等于key的最小键
public Key Ceiling(Key key)
{
int index = Rank(key);
if (index == N)
{
throw new ArgumentException("不存在大于或等于key的最小键");
}
else
{
return keys[index +1];
}
}
}
}
P29 基于有序数组实现集合
1、创建有序数组集合类;
2、比较链表实现集合与有序数组实现集合性能;
//有序数组集合类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class SortedArray1Set<Key> : ISet<Key> where Key : IComparable<Key>
{
private SortedArray1<Key> sortedArray1Set;
public SortedArray1Set(int capacity)
{
sortedArray1Set = new SortedArray1<Key>();
}
public SortedArray1Set()
{
sortedArray1Set = new SortedArray1<Key>();
}
public int Count { get { return sortedArray1Set.Count;} }
public bool IsEmpty { get { return sortedArray1Set.IsEmpty; } }
public void Add(Key key)
{
sortedArray1Set.Add(key);
}
public bool Contains(Key key)
{
return sortedArray1Set.Contains(key);
}
public void Remove(Key key)
{
sortedArray1Set.Remove(key);
}
}
}
P30 基于有序数组实现映射/字典
1、更改有序数组类;
2、创建有序数组字典类;
2、比较链表实现字典与有序数组实现集字典性能;
//有序数组2类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class SortedArray2<Key,Value> where Key : IComparable<Key>
{
private Key[] keys;
private Value[] values;
private int N;
public SortedArray2(int capacity)
{
keys = new Key[capacity];
values = new Value[capacity];
}
public SortedArray2()
{
keys = new Key[10];
values = new Value[10];
}
#region 返回数组容量、元素个数、判断数组是否为空
//返回数组容量
public int Capacity
{
get { return keys.Length; }
}
//返回数组中元素个数
public int Count
{
get { return N; }
}
//判断数组是否为空
public bool IsEmpty
{
get { return N == 0; }
}
#endregion
public int Rank(Key key)
{
int l = 0;
int r = N - 1;
while (l <= r)
{
//这里的问题 有毒 写成了(l + (r - l) )/ 2
int mid = l + (r - l) / 2;
if (keys[mid].CompareTo(key) < 0)
{
l = mid + 1;
}
else if (keys[mid].CompareTo(key) > 0)
{
r = mid - 1;
}
else
{
return mid;
}
}
return l;
}
public Value Get(Key key)
{
if (IsEmpty)
{
throw new ArgumentException("数组为空");
}
int index = Rank(key);
if (index < N && keys[index].CompareTo(key) == 0)
{
return values[index];
}
else
{
throw new ArgumentException("该键不存在");
}
}
public void Add(Key key,Value value)
{
int index = Rank(key);
if (N == keys.Length)
{
RestCapacity(keys.Length * 2);
}
if (index<N && keys[index].CompareTo(key)==0)
{
values[index] = value;
return;
}
else
{
for (int i = N-1 ; i >= index; i--)
{
keys[i+1] = keys[i];
values[i + 1] = values[i];
}
keys[index] = key;
values[index] = value;
N++;
}
}
public void Remove(Key key)
{
if (IsEmpty)
{
return;
}
int index = Rank(key);
if (index < N && keys[index].CompareTo(key) != 0)
{
return;
}
for (int i = index + 1; i < N; i++)
{
keys[i - 1] = keys[i];
values[i - 1] = values[i];
}
N--;
keys[N] = default(Key);
values[N] = default(Value);
if (N == keys.Length / 4)
{
RestCapacity(keys.Length / 2);
}
}
#region 扩容操作
private void RestCapacity(int newCapacity)
{
Key[] newKeys = new Key[newCapacity];
Value[] newValues = new Value[newCapacity];
for (int i = 0; i < N; i++)
{
newKeys[i] = keys[i];
newValues[i] = values[i];
}
keys = newKeys;
values = newValues;
}
#endregion
//重写 ToString()
public override string ToString()
{
StringBuilder res = new StringBuilder();
res.Append("[");
for (int i = 0; i < N; i++)
{
res.Append("{" + keys[i] + ","+values[i] +"}" );
if (i != N - 1)
{
res.Append(",");
}
}
res.Append("]");
return res.ToString();
}
//找到最小值
public Key Min()
{
if (IsEmpty)
{
throw new ArgumentException("数组为空");
}
else
{
return keys[0];
}
}
//找到最大值
public Key Max()
{
if (IsEmpty)
{
throw new ArgumentException("数组为空");
}
else
{
return keys[N-1];
}
}
//返回索引为k的键
public Key Select(int k)
{
if (k<0 || k>=N)
{
throw new ArgumentException("数组索引异常");
}
else
{
return keys[k];
}
}
//判断数组中是否包含值为key的键
public bool Contains(Key key)
{
int index = Rank(key);
if (index<N && keys[index].CompareTo(key) == 0)
{
return true;
}
else
{
return false;
}
}
//找出小于或等于key的最大键
public Key Floor(Key key)
{
int index = Rank(key);
if (index < N && keys[index].CompareTo(key) == 0)
{
return keys[index];
}
if (index ==0)
{
throw new ArgumentException("不存在小于或等于key的最大键");
}
else
{
return keys[index-1];
}
}
//找出大于或等于key的最小键
public Key Ceiling(Key key)
{
int index = Rank(key);
if (index == N)
{
throw new ArgumentException("不存在大于或等于key的最小键");
}
else
{
return keys[index +1];
}
}
}
}
//有序数组字典类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class SortedArray2Dictionary<Key,Value> : IDictionary<Key,Value> where Key : IComparable<Key>
{
private SortedArray2<Key, Value> sortedArray2Dictionary;
public int Count { get { return sortedArray2Dictionary.Count; } }
public bool IsEmpty { get { return sortedArray2Dictionary.IsEmpty; } }
public SortedArray2Dictionary(int capacity)
{
sortedArray2Dictionary = new SortedArray2<Key, Value>(capacity);
}
public SortedArray2Dictionary()
{
sortedArray2Dictionary = new SortedArray2<Key, Value>();
}
public void Remove(Key key)
{
sortedArray2Dictionary.Remove(key);
}
public void Add(Key key, Value value)
{
sortedArray2Dictionary.Add(key,value);
}
public bool ContainsKey(Key key)
{
return sortedArray2Dictionary.Contains(key);
}
public Value Get(Key key)
{
return sortedArray2Dictionary.Get(key);
}
public void Set(Key key, Value newValue)
{
sortedArray2Dictionary.Add(key, newValue);
}
}
}