偶然看到一篇大佬的排序算法,学习之余记录一下。写的不好,多多担待。
1:插入排序
效果图:
//插入排序
/*
* 1.从第一个元素开始,该元素可以认为已经被排序
* 2.取下一个元素tem,从已排序的元素序列从后往前扫描
* 3.如果该元素大于tem,则将该元素移到下一位
* 4.重复步骤3,直到找到已排序元素中小于等于tem的元素
* 5.tem插入到该元素的后面,如果已排序所有元素都大于tem,则将tem插入到下标为0的位置
* 6.重复步骤2~5
* */
test = new int[] { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
for (int i = 0; i < test.Length; i++)
{
//先获取要比较的数
int targ = test[i];
//开始和它之前的数比
for (int j = i - 1; j > -1; j--)
{
if (test[i] < test[j])
{
int temp = test[i];
test[i] = test[j];
test[j] = temp;
i = j - 1; //如果发生了换位,就要用换位之后的数据和在它之前的数比较
}
break;
}
}
2:希尔排序
效果
//希尔排序
/*
*
* 1.先选定一个小于N的整数gap作为第一增量,然后将所有距离为gap的元素分在同一组,并对每一组的元素进行直接插入排序。然后再取一个比第一增量小的整数作为第二增量,重复上述操作…
* 2.当增量的大小减到1时,就相当于整个序列被分到一组,进行一次直接插入排序,排序完成。
*
* */
test = new int[] { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
int gap = test.Length;
while (gap > 0)
{
gap /= 2;
for (int i = 0; i < test.Length - gap; i++)
{
if (test[i] > test[i + gap])
{
int temp = test[i];
test[i] = test[i + gap];
test[i + gap] = temp;
if (i - gap - 1 >= -1)
i = i - gap - 1;
}
}
}
3:冒泡排序
效果图:
//冒泡排序
/*
* 左边大于右边交换一趟排下来最大的在右边
*/
test = new int[] { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
int j = test.Length - 1;
while (j > 0)
{
int flag = 0; //定义这个是为了在已经排好了的情况下,没必要再进行多余的while循环
for (int k = 0; k < j; k++)
{
if (test[k + 1] < test[k])//如果前一个大于后一个
{
int temp = test[k];
test[k] = test[k + 1];
test[k + 1] = temp;
flag = 1;
}
}
if (flag == 0)
break;
j--;
}
4:选择排序
效果图:
//选择排序
/*
* 每次从待排序列中选出一个最小值,然后放在序列的起始位置,直到全部待排数据排完即可。
*
*
*/
test = new int[] { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
//这是只找最小值的代码
for (int i = 0; i < test.Length; i++) //先从第一个最小的开始找,就是要放在最左边的
{
int temp = int.MaxValue;//因为是要求最小值放在左边
int index = -1;
for (int j = i; j < test.Length; j++)
{
if (test[j] < temp)
{
temp = test[j];
index = j;
}
}
int tempvalue = test[i];
test[i] = temp;
test[index] = tempvalue;
}
test = new int[] { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
//同时找最小值,最大值
int[] result = new int[test.Length];
int n = test.Length / 2 + test.Length % 2;
for (int i = 0; i < n; i++)
{
int Min = int.MaxValue;
int Max = int.MinValue;
int Minindex = -1, Maxindex = -1;
for (int j = i; j < test.Length - i; j++)
{
//获取该段的最小值
if (test[j] < Min)
{
Min = test[j];
Minindex = j;
}
//获取该段的最大值
if (test[j] > Max)
{
Max = test[j];
Maxindex = j;
}
}
//接下来将最小和最大值分别赋值
if (Minindex == test.Length - i - 1 || Maxindex == i)//最小值在最大值上,或者最大值在最小值上
{
int temp = test[i];//先交换获取OK的值
test[i] = test[test.Length - i - 1];
test[test.Length - i - 1] = temp;
if (Minindex == test.Length - i - 1)
{
Minindex = i;
}
if (Maxindex == i)
{
Maxindex = test.Length - i - 1;
}
}
int TempMinValue = test[i];
int TempMaxValue = test[test.Length - i - 1];
test[i] = Min;
test[test.Length - i - 1] = Max;
test[Minindex] = TempMinValue;
test[Maxindex] = TempMaxValue;
}
大佬链接:https://blog.csdn.net/weixin_50886514/article/details/119045154?utm_medium=distribute.pc_feed_v2.none-task-blog-hot_rank_bottoming-16.pc_personrecdepth_1-utm_source=distribute.pc_feed_v2.none-task-blog-hot_rank_bottoming-16.pc_personrec