C算法Code.docx

上传人:b****8 文档编号:9564005 上传时间:2023-02-05 格式:DOCX 页数:48 大小:36.93KB
下载 相关 举报
C算法Code.docx_第1页
第1页 / 共48页
C算法Code.docx_第2页
第2页 / 共48页
C算法Code.docx_第3页
第3页 / 共48页
C算法Code.docx_第4页
第4页 / 共48页
C算法Code.docx_第5页
第5页 / 共48页
点击查看更多>>
下载资源
资源描述

C算法Code.docx

《C算法Code.docx》由会员分享,可在线阅读,更多相关《C算法Code.docx(48页珍藏版)》请在冰豆网上搜索。

C算法Code.docx

C算法Code

算法

冒泡排序bubblesorting

intt;

int[]a={21,56,64,94,97,123};

for(intj=a.Length-1;j>0;j--)

{for(inti=0;i

{if(a[i]>a[i+1])

{t=a[i];

a[i]=a[i+1];

a[i+1]=t;

}}}

for(intu=0;u

Console.WriteLine(a[u]);结果:

21,56,64,94,97,123

同时找最大最小

inttemp;

int[]a={56,66,5,1230,87,95};

for(inti=0;i<(a.Length+1)/2;i++)

{if(a[i]>a[a.Length-1-i])

{temp=a[i];

a[i]=a[a.Length-1-i];

a[a.Length-1-i]=temp;

}

}

intmax=a[a.Length-1],min=a[0];

for(inti=0;i<(a.Length+1)/2;i++)

{if(min>a[i])

min=a[i];

}

for(inti=(a.Length+1)/2;i

{if(max

max=a[i];

}

Console.WriteLine("{0},{1}",min,max);结果:

5,1230

基数排序

int[]a={1,5,9,7};

int[]b=newint[10];

for(inti=0;i

b[a[i]]=1;

for(intj=0;j

if(b[j]==1)

Console.WriteLine(j);结果:

1,5,7,9

插入排序

int[]r={12,2,6,65,42};

for(inti=1;i

{intt;

t=r[i];

intj;

for(j=i-1;j>=0&&r[j]>t;j--)

{}

for(intk=i;k>j+1;k--)

r[k]=r[k-1];

r[j+1]=t;

}

for(intf=0;f

Console.WriteLine(r[f]);结果:

2,6,12,42,65

QuickSort快速排序

staticvoidQuickSort(int[]a,intstart,intend)

{inti=start,j=end;

intpivot=a[i];

while(i

{while(i

j--;

a[i]=a[j];

while(i

i++;

a[j]=a[i];

}

a[i]=pivot;

if(i>start)

QuickSort(a,start,i);

if(i

QuickSort(a,i+1,end);

}

staticvoidMain(string[]args)

{int[]x={87,56,5,13,5,12,};

QuickSort(x,0,x.Length-1);

for(inti=0;i

Console.WriteLine(x[i]);

}结果:

5,5,12,13,56,87

MergeSort归并排序

staticvoidMergeSort(int[]a,ints,inte)

{if(s>=e)return;

MergeSort(a,s,(s+e)/2);

MergeSort(a,(s+e)/2+1,e);

Merge(a,s,(s+e)/2,e);

}

staticvoidMerge(int[]a,ints,intmid,inte)

{int[]b=newint[a.Length];

for(intw=0;w

b[w]=a[w];

inti=s;

intj=mid+1;

intk=s;

while(i<=mid&&j<=e)

{if(b[i]

a[k++]=b[i++];

else

a[k++]=b[j++];

}

while(i<=mid)

a[k++]=b[i++];

while(j<=e)

a[k++]=b[j++];

}

staticvoidMain(string[]args)

{int[]a={34,2,5,66,87,99};

MergeSort(a,0,a.Length-1);

for(inti=0;i

Console.WriteLine(a[i]);

}结果:

2,5,34,66,87,99

二叉查找树

//二叉查找树节点Binarysearchtreenode

publicclassBinarySearchTreeNode

{publicintkey;//二叉查找树节点的值

publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点

publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点

///二叉查找树节点构造函数

publicBinarySearchTreeNode(intnodeValue)

{key=nodeValue;//nodeValue节点的值

left=null;right=null;

}

///插入节点

publicvoidInsertNode(BinarySearchTreeNodenode)

{if(node.key>this.key)

{if(this.right==null)

{this.right=node;//node插入的节点

return;

}

else

this.right.InsertNode(node);

}

else

{if(this.left==null)

{this.left=node;return;}

else

this.left.InsertNode(node);

}

}

///二叉查找树查询

publicboolSearchKey(intsearchValue)

{if(this.key==searchValue)//searchValue需要查询的值

returntrue;//是否找到查询的值

if(searchValue>this.key)

{if(this.right==null)returnfalse;

else

returnthis.right.SearchKey(searchValue);

}

else

{if(this.left==null)returnfalse;

else

returnthis.left.SearchKey(searchValue);

}

}

//中序遍历

publicvoidMiddleDisplay()

{if(this.left!

=null)

this.left.MiddleDisplay();

Console.WriteLine(this.key);

if(this.right!

=null)

this.right.MiddleDisplay();

}

//前序遍历

publicvoidFrontDisplay()

{Console.WriteLine(this.key);

if(this.left!

=null)

this.left.FrontDisplay();

if(this.right!

=null)

this.right.FrontDisplay();

}

//后序遍历

publicvoidBehindDisplay()

{if(this.left!

=null)

this.left.BehindDisplay();

if(this.right!

=null)

this.right.BehindDisplay();

Console.WriteLine(this.key);

}

}

///二叉查找树

publicclassBinarySearchTree

{privateBinarySearchTreeNoderoot;

///生成一个二叉查找树

publicBinarySearchTree()

{root=nul;}

///生成一个二叉查找树

///二叉查找树根节点的值

publicBinarySearchTree(intnodeValue)

{root=newBinarySearchTreeNode(nodeValue);}

///在二叉查找树上插入一个节点

///插入节点的值

publicvoidInsertBinarySearchTreeNode(intnodeValue)

{BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);

if(root==null)

{root=insertNode;

return;

}

else

root.InsertNode(insertNode);

return;

}

///在二叉查找树上查询一个数

///需要查询的值

///是否找到查询的值

publicboolSearchKey(intsearchValue)

{if(root.key==searchValue)returntrue;

else

returnroot.SearchKey(searchValue);

}

///二叉查找树中序遍历

publicvoidMiddleDisplay()

{root.MiddeleDisplay();return;}

///二叉查找树前序遍历

publicvoidFrontDisplay()

{root.FrontDisplay();return;}

///二叉查找树后序遍历

publicvoidBehindDisplay()

{root.BehindDisplay();return;}

///二叉查找树排序

///需要排序的数组

publicstaticvoidBinarySearchTreeSort(int[]a)

{BinarySearchTreet=newBinarySearchTree();

for(inti=0;i

t.InsertBinarySearchTreeNode(a[i]);

t.MiddleDisplay();return;

}///二叉查找树查找

///进行查找的数组

///需要查找的树

publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey)

{BinarySearchTreet=newBinarySearchTree();

for(inti=0;i

t.InsertBinarySearchTreeNode(a[i]);

returnt.SearchKey(searchKey);

}

}

namespace二叉树

{classNode

{intn;

publicNode(intx)

{n=x;}

publicNodeLeft;

publicNodeRight;

publicvoidInsert(Nodenode)

{if(node.n>this.n)

{if(this.Right==null)

this.Right=node;

else

this.Right.Insert(node);}

else

{if(this.Left==null)

{this.Left=node;}

else

{this.Left.Insert(node);}}}//递归

publicvoidShow()

{Console.WriteLine(n);}}

classBinaryTree

{Noderoot;

publicvoidGenerateTree(Nodenode)//高内聚,低耦合

{if(root==null)

{root=node;return;}//如果树是空,第一次加节点

root.Insert(node);

}

publicvoidShowInOrder(Nodenode)//中序遍历(inorder):

左中右。

先(前)序遍历(preorder):

中左右。

后序遍历(postorder):

左右中。

{if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数

ShowInOrder(node.Left);

node.Show();

ShowInOrder(node.Right);

}

publicvoidShow()

{ShowInOrder(root);}

}

classA

{staticvoidMain()

{BinaryTreeb=newBinaryTree();

Nodenode=newNode(5);

b.GenerateTree(node);

node=newNode(13);

b.GenerateTree(node);

node=newNode(6);

b.GenerateTree(node);

node=newNode(26);

b.GenerateTree(node);

node=newNode(7);

b.GenerateTree(node);

b.Show();}}}结果:

5,6,7,13,26

单链表

classNode

{inta;

publicNode(inta)

{this.a=a;}

publicintA

{get{returna;}set{a=value;}}

publicNodenext;

}

classLinkedList

{Nodeheader;

publicvoidGenerate(intx)

{if(header==null)

header=newNode(x);

else

{Noden=newNode(x);

if(n.A

{n.next=header;

header=n;

return;

}

Nodetmp=header;

Nodet=header;

while(tmp.A

{t=tmp;//为了下一次循环

tmp=tmp.next;

if(tmp==null)

break;

}

t.next=n;

n.next=tmp;

}

}

publicvoidOut()

{Nodetmp=header;

while(tmp!

=null)

{Console.WriteLine(tmp.A);

tmp=tmp.next;

}}}

classTest

{staticvoidMain()

{LinkedListll=newLinkedList();

ll.Generate(6);

ll.Generate(36);

ll.Generate(26);

ll.Generate(16);

ll.Out();

}}}

反向链表

classLink//thisclassreversetheLinkedList

{publicinta;

publicLinknext;

}

classcreateLink//theclasscreatetheLinkedList

{

Linkheader=null;

Linkp=null;

Linktemp=null;

Linkl=null;//Linkk=null;

Linkg=null;

publicvoidcreate()

{stringstr;

inti;

Console.WriteLine("Pleaseenternumber:

");

str=Console.ReadLine();

while(str!

="y")

{i=Convert.ToInt32(str);

temp=newLink();

temp.a=i;

temp.next=null;

if(g==null)

g=temp;

if(header==null)

header=temp;

if(p==null)

p=temp;

else

{p.next=temp;

p=p.next;

}

Console.WriteLine("pleaseenternumber:

");

str=Console.ReadLine();

}

}

publicvoiddisplay()

{while(header!

=null)

{Console.WriteLine(header.a);

header=header.next;

}

}

publicvoidreversed()//themothodreversedtheLinkedList

{Linkk=null;

Linktmp=null;

Linkcom=null;

if(tmp==null)

tmp=header.next;

while(tmp!

=null)

{//if(com==null)

//com=header;

l=tmp;

if(k==null)

{header.next=null;

k=header;

}

com=header;

header=l;

tmp=l.next;

l.next=com;

}

}

publicvoidshow()

{while(l!

=null)

{Console.WriteLine(l.a);

l=l.next;

}}}

classTester

{staticvoidMain()

{createLinkcl=newcreateLink();

cl.create();

//cl.display();

cl.reversed();

cl.show();

}}}

Stack栈

classNode

{inta;

publicNode(inta)

{this.a=a;}

publicintA

{get{returna;}set{a=value;}}

publicNodenext;

}

classLinkedList

{protectedNodeheader;

publicvoidGenerate(intx)

{if(header==null)

header=newNode(x);

else

{Noden=newNode(x);

n.next=header;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 解决方案

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1