面试算法摘要(Ⅱ)
程序员文章站
2024-03-17 23:05:28
...
常见数组排序:快速排序、冒泡排序、选择排序等
/// <summary>
/// 数组排序算法
/// </summary>
class MechanismSort
{
#region 冒泡排序
/// <summary>
/// 冒泡排序
/// 原理:比较相邻的元素,根据要交换其位置
/// 参考:https://baike.baidu.com/item/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F/4602306?fr=aladdin#3_7
/// </summary>
/// <param name="arrData">需要排序的数组</param>
/// <param name="isAsc">排序类型 true 升序 反之降序</param>
public static void bubbleSort(int[] arrData, bool isAsc = true)
{
for (int i = 0, len = arrData.Length; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
//升序
if (isAsc)
{
if (arrData[j] > arrData[j + 1])
{
arrData[j] = arrData[j] + arrData[j + 1];
arrData[j + 1] = arrData[j] - arrData[j + 1];
arrData[j] = arrData[j] - arrData[j + 1];
}
}
//降序
else
{
if (arrData[j] < arrData[j + 1])
{
arrData[j] = arrData[j] + arrData[j + 1];
arrData[j + 1] = arrData[j] - arrData[j + 1];
arrData[j] = arrData[j] - arrData[j + 1];
}
}
}
}
Console.WriteLine("\n冒泡排序,{0}", isAsc ? "升序" : "降序");
foreach (var item in arrData)
{
Console.Write("{0} ", item);
}
}
#endregion
#region 快速排序
/// <summary>
/// 快速排序(二分法+递归,最快的排序方法)
/// 原理:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
/// 参考:https://baike.baidu.com/item/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95?fromtitle=%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F&fromid=2084344#3_9
/// </summary>
/// <param name="arrData">需要排序的数组</param>
/// <param name="startIndex">开始排序的位置</param>
/// <param name="endIndex">结束排序的位置</param>
/// <param name="isAsc">排序类型 true 升序 反之降序</param>
public static void quickSort(int[] arrData, int startIndex, int endIndex, bool isAsc = true)
{
if (startIndex < 0 || startIndex > endIndex || startIndex >= arrData.Length)
return;
if (endIndex < 0 || endIndex < startIndex || endIndex >= arrData.Length)
return;
int first = startIndex;
int last = endIndex;
int key = arrData[first];
while (first < last)
{
//升序
if (isAsc)
{
while (first < last && arrData[last] >= key)
--last;
arrData[first] = arrData[last];
while (first < last && arrData[first] <= key)
++first;
arrData[last] = arrData[first];
}
//降序
else
{
while (first < last && arrData[last] <= key)
--last;
arrData[first] = arrData[last];
while (first < last && arrData[first] >= key)
++first;
arrData[last] = arrData[first];
}
}
arrData[first] = key;
quickSort(arrData: arrData, startIndex: startIndex, endIndex: first - 1, isAsc: isAsc);
quickSort(arrData: arrData, startIndex: first + 1, endIndex: endIndex, isAsc: isAsc);
}
#endregion
#region 选择排序
/// <summary>
/// 选择排序
/// 原理:原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完
/// 参考:https://baike.baidu.com/item/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F#4_2
/// </summary>
/// <param name="arrData">需要排序的数组</param>
/// <param name="isAsc">排序类型 true 升序 反之降序</param>
public static void selectSort(int[] arrData, bool isAsc = true)
{
for (int i = 0, len = arrData.Length; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
//升序
if (isAsc)
{
if (arrData[i] >= arrData[j])
{
arrData[i] = arrData[i] + arrData[j];
arrData[j] = arrData[i] - arrData[j];
arrData[i] = arrData[i] - arrData[j];
}
}
//降序
else
{
if (arrData[i] <= arrData[j])
{
arrData[i] = arrData[i] + arrData[j];
arrData[j] = arrData[i] - arrData[j];
arrData[i] = arrData[i] - arrData[j];
}
}
}
}
Console.WriteLine("\n选择排序,{0}", isAsc ? "升序" : "降序");
foreach (var item in arrData)
{
Console.Write("{0} ", item);
}
}
#endregion
}
下一篇: 二分法查找实现(递归与非递归)