前言
小编最近开始走高大上路线了,知道为什么吗?因为最近两三个月小编一直在进行算法的学习,说起算法,那他的地位可是了不得了。算法是计算机科学领域最重要的基石之一。当今社会的人工智能技术以及大数据我们都可以看到算法的身影。如果你想让你的工资翻几倍,那么就从现在开始学习算法吧。
叙述
接下来小编具体阐述一下“算法”是什么,以及用流水线+装饰模式简单的了解一下所学到的几个算法。
算法是什么
简单了解算法(流水线+装饰模式):
定义及原理:
选择排序:
每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。
------直接选择排序:
首先在未排序序列中找到最小元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。以此类推,直到所有元素均排序完毕。
选择最小的元素与未排序部分的首部交换,使得序列的前面为有序。
------堆排序
在排序过程中,将R[l..n]看成是一棵完全二叉树的顺序存储结构,利用完全二叉树中双亲结点和孩子结点之间的内在关系(参见二叉树的顺序存储结构),在当前无序区中选择关键字最大(或最小)的记录
交换排序:
所谓交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动。
------冒泡排序
通过无序区中相邻记录关键字间的比较和位置的交换,使关键字最小的记录如气泡一般逐渐往上“漂浮”直至“水面”。
------快速排序
通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
插入排序
------直接插入排序
将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据
------希尔排序
先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量 =1( < …<d2<d1),即所有记录放在同一组中进行直接插入排序为止。
图:
选择排序
------直接选择排序
------堆排序
交换排序
------冒泡排序
------快速排序
插入排序
------直接插入排序
------希尔排序
代码实现:
选择排序
------直接选择排序
#include<iostream>
#include<time.h>
#include<iomanip>
using namespace std;
const int N=10;
int main()
{
int a[N],i,j,temp,b;
srand(time(NULL));
for(i=0;i<N;i++)
a[i]=rand()%100;
for(i=0;i<N;i++)
cout<<setw(3)<<a[i];
cout<<endl;
for(i=0;i<N-1;i++)
{
temp=i;
for(j=i+1;j<N;j++)
{
if(a[temp]>a[j])
temp=j;
}
if(i!=temp)
{
b=a[temp];
a[temp]=a[i];
a[i]=b;}
}
for(i=0;i<N;i++)
cout<<setw(3)<<a[i];
cout<<endl;
}
------堆排序
///<summary>
///构建堆
///</summary>
static void HeapAdjust(List<int> list,int parent,int length)
{
int temp=list[parent];
int child=2*parent+1;
while(child<length)
{
if(child+1<length&&list[child]<list[child+1])child++;
if(temp>=list[child])
break;
list[parent]=list[child];
parent=child;
child=2*parent+1;
}
list[parent]=temp;
}
///<summary>
///堆排序
///</summary>
public static List<int> HeapSort(List<int> list,int top)
{
List<int> topNode=new List<int>();
for(int i=list.Count/2-1;i>=0;i--)
{
HeapAdjust(list,i,list.Count);
}
for(int i=list.Count-1;i>=list.Count-top;i--)
{
int temp=list[0];
list[0]=list[i];
list[i]=temp;
topNode.Add(temp);
HeapAdjust(list,0,i);
}
return topNode;
}
交换排序------冒泡排序
冒泡算法C#
namespace 数组排序
{
class Program
{
static void Main(string[] args)
{
int temp = 0;
int[] arr = {23, 44, 66, 76, 98, 11, 3, 9, 7};
#region该段与排序无关
Console.WriteLine("排序前的数组:");
foreach (intiteminarr)
{
Console.Write(item + "");
}
Console.WriteLine();
#endregion
for (int i = 0; i < arr.Length - 1; i++)
{
#region将大的数字移到数组的arr.Length-1-i
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
#endregion
}
Console.WriteLine("排序后的数组:");
foreach (int item in arr)
{
Console.Write(item+"");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
------快速排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class QuickSort
{
static void Main(string[] args)
{
int[] array = { 49, 38, 65, 97, 76, 13, 27 };
sort(array, 0, array.Length - 1);
Console.ReadLine();
}
/**一次排序单元,完成此方法,key左边都比key小,key右边都比key大。
**@param array排序数组
**@param low排序起始位置
**@param high排序结束位置
**@return单元排序后的数组 */
private static int sortUnit(int[] array, int low, int high)
{
int key = array[low];
while (low < high)
{
/*从后向前搜索比key小的值*/
while (array[high] >= key && high > low)
--high;
/*比key小的放左边*/
array[low] = array[high];
/*从前向后搜索比key大的值,比key大的放右边*/
while (array[low] <= key && high > low)
++low;
/*比key大的放右边*/
array[high] = array[low];
}
/*左边都比key小,右边都比key大。//将key放在游标当前位置。//此时low等于high */
array[low] = key;
foreach (int i in array)
{
Console.Write("{0}\t", i);
}
Console.WriteLine();
return high;
}
/**快速排序
*@paramarry
*@return */
public static void sort(int[] array, int low, int high)
{
if (low >= high)
return;
/*完成一次单元排序*/
int index = sortUnit(array, low, high);
/*对左边单元进行排序*/
sort(array, low, index - 1);
/*对右边单元进行排序*/
sort(array, index + 1, high);
}
}
}
插入排序------直接插入排序
classProgram
{
staticvoidMain(string[]args)
{
InsertionSort();
}
///<summary>
///插入排序法
///</summary>
privatestaticvoidInsertionSort()
{
Console.WriteLine("插入排序法");
inttemp=0;
int[]arr={23,44,66,76,98,11,3,9,7};
Console.WriteLine("排序前的数组:");
foreach(intiteminarr)
{
Console.Write(item+",");
}
Console.WriteLine();
varlength=arr.Length;
for(inti=1;i<length;i++)
{
for(intj=i;j>0;j--)
{
if(arr[j]>arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
//每次排序后数组
PrintResult(arr);
}
Console.ReadKey();
}
///<summary>
///打印结果
///</summary>
///<paramname="arr"></param>
privatestaticvoidPrintResult(IEnumerable<int>arr)
{
foreach(intiteminarr)
{
Console.Write(item+",");
}
Console.WriteLine();
}
}
------希尔排序
usingSystem;
publicclassShellSorter
{
publicvoidSort(int[]list)
{
intinc;
for(inc=1;inc<=list.Length/9;inc=3*inc+1);
for(;inc>0;inc/=3)
{
for(inti=inc+1;i<=list.Length;i+=inc)
{
intt=list[i-1];
intj=i;
while((j>inc)&&(list[j-inc-1]>t))
{
list[j-1]=list[j-inc-1];
j-=inc;
}
list[j-1]=t;
}
}
}
}
publicclassMainClass
{
publicstaticvoidMain()
{
int[]iArrary=newint[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
ShellSortersh=newShellSorter();
sh.Sort(iArrary);
for(intm=0;m<=13;m++)
Console.WriteLine("{0}",iArrary[m]);
Console.ReadKey();
}
}
小结:
算法作为一种思想,能锻炼我们的思维,使思维变得更清晰、更有逻辑。算法是对事物本质的数学抽象,看似深奥,却体现着点点滴滴的朴素思想。虽然真理未必只有一个,但是当你掌握了其中的一个,你就掌握了全部。因此,学会算法的思想,其意义不仅仅在算法本身,也会对日后的学习生活产生深远的影响。