#约瑟夫问题c#排序算法C#非递归回溯法文档格式.docx
《#约瑟夫问题c#排序算法C#非递归回溯法文档格式.docx》由会员分享,可在线阅读,更多相关《#约瑟夫问题c#排序算法C#非递归回溯法文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
![#约瑟夫问题c#排序算法C#非递归回溯法文档格式.docx](https://file1.bdocx.com/fileroot1/2023-1/10/14477248-f517-494f-ae37-e5faa0abe4a3/14477248-f517-494f-ae37-e5faa0abe4a31.gif)
if(j==k)
a[i]=0;
count++;
j=0;
//重新开始,找下一个值!
}
i++;
if(i==sum)
i=0;
//实现环(即围成一圈);
if(a[m]!
Console.Write(m+1+"
\n"
);
Console.ReadKey();
}
一、冒泡排序(Bubble)
namespaceBubbleSorter
publicclassBubbleSorter
publicvoidSort(int[]list)
inti,j,temp;
booldone=false;
j=1;
while((j<
list.Length)&
&
(!
done))
done=true;
for(i=0;
i<
list.Length-j;
i++)
if(list[i]>
list[i+1])
done=false;
temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
j++;
publicclassMainClass
{
publicstaticvoidMain()
int[]iArrary=newint[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
BubbleSortersh=newBubbleSorter();
sh.Sort(iArrary);
for(intm=0;
m<
iArrary.Length;
m++)
Console.Write("
{0}"
iArrary[m]);
Console.WriteLine();
二、选择排序(Selection)
namespaceSelectionSorter
publicclassSelectionSorter
privateintmin;
publicvoidSort(int[]list)
for(inti=0;
list.Length-1;
min=i;
for(intj=i+1;
j<
list.Length;
j++)
if(list[j]<
list[min])
min=j;
intt=list[min];
list[min]=list[i];
list[i]=t;
int[]iArrary=newint[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
SelectionSorterss=newSelectionSorter();
ss.Sort(iArrary);
for(intm=0;
三、插入排序(InsertionSorter)
namespaceInsertionSorter
publicclassInsertionSorter
for(inti=1;
intt=list[i];
intj=i;
while((j>
0)&
(list[j-1]>
t))
list[j]=list[j-1];
--j;
list[j]=t;
int[]iArrary=newint[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
InsertionSorterii=newInsertionSorter();
ii.Sort(iArrary);
{0}"
四、希尔排序(ShellSorter)
namespaceShellSorter
publicclassShellSorter
intinc;
for(inc=1;
inc<
=list.Length/9;
inc=3*inc+1);
for(;
inc>
0;
inc/=3)
for(inti=inc+1;
=list.Length;
i+=inc)
intt=list[i-1];
inc)&
(list[j-inc-1]>
list[j-1]=list[j-inc-1];
j-=inc;
list[j-1]=t;
ShellSortersh=newShellSorter();
}
冒泡排序
publicvoidBubbleSort(int[]R)
inti,j,temp;
//交换标志
boolexchange;
//最多做R.Length-1趟排序
for(i=0;
i<
R.Length;
i++)
{
//本趟排序开始前,交换标志应为假
exchange=false;
for(j=R.Length-2;
j>
=i;
j--)
//交换条件
if(R[j+1]<
R[j])
temp=R[j+1];
R[j+1]=R[j];
R[j]=temp;
//发生了交换,故将交换标志置为真
exchange=true;
}
//本趟排序未发生交换,提前终止算法
if(!
exchange)
break;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceSort
classProgram
delegateboolCompareOp(objectlhs,objectrhs);
classBubbleSorter
staticpublicvoidSort(object[]sortArray,CompareOpgtMethod)
for(inti=0;
i<
sortArray.Length;
i++)
for(intj=i+1;
j<
j++)
if(gtMethod(sortArray[j],sortArray[i]))
objecttemp=sortArray[i];
sortArray[i]=sortArray[j];
sortArray[j]=temp;
classEmployee
privatestringname;
privatedecimalsalary;
publicEmployee(stringname,decimalsalary)
this.name=name;
this.salary=salary;
publicoverridestringToString()
returnstring.Format(name+"
{0:
C}"
salary);
publicstaticboolRhsIsGreater(objectlhs,objectrhs)
EmployeeempLhs=(Employee)lhs;
EmployeeempRhs=(Employee)rhs;
return(empRhs.salary>
empLhs.salary)?
true:
false;
staticvoidMain(string[]args)
Employee[]employees=
newEmployee("
BugsBunny"
20000),
ElmerFudd"
10000),
DaffyDuck"
25000),
WileyCoyote"
(decimal)100000.38),
FoghornLeghorn"
23000),
RoadRunner"
50000),
};
CompareOpemployeeCompareOp=newCompareOp(Employee.RhsIsGreater);
BubbleSorter.Sort(employees,employeeCompareOp);
foreach(Employeeemployeeinemployees)
Console.WriteLine(employee.ToString());
非递归回溯法
/*
*回溯是一种比较有用的算法,其思想比较简单,也是源于生活。
*若某种猜测行不通,则撤回,并用另一种猜测替换它,这种反向折回并试探新步骤的策略称为回溯。
*其经典实例就是八皇后的问题。
在国际象棋中棋盘有八行八列,其64个方格,其中最强大的莫过于皇后,
*可以攻击同一行,同一列,同一对角线的棋子,八皇后问题要求给出解决方案,八个皇后放在棋盘上,并且她们无法相互功击
*回溯法经常应用于模拟人类思维,即人工智能
*/
namespace非递归回溯
constintSIZE=8;
staticint[]Queen=newint[SIZE];
//皇后的位置
staticintsum=0;
//解决方案的数量
Trial();
staticvoidTrial()
introw=0,column=0;
Queen[0]=-1;
while(true)
for(column=Queen[row]+1;
column<
SIZE;
column++)
//判断新皇后的位置是否合法
if(!
IsConfine(row,column))
//没有合适的位置放皇后,回溯到上一行
if(column==SIZE)
//如果没有合适的位置放皇后并且已回溯到第一行
//退出循环
if(0==row)
//回溯到上一行
Queen[row]=-1;
row--;
else//有合适的位置放皇后
Queen[row]=column;
//进入到下一行
row++;
//如果没有放完皇后,则继续循环
if(row<
SIZE)
else//得到一种解决方案
Console.WriteLine("
第"
+(++sum)+"
种方案"
Write();
//继续回溯寻找下一种解决方案
row=SIZE-1;
//判断皇后的放置是否合法
staticboolIsConfine(introw,intcolumn)
row;
if(Queen[i]==-1)
if(column==Queen[i]||column==Queen[i]+row-i||column==Queen[i]-row+i)
returntrue;
returnfalse;
//输出解决方案
staticvoidWrite()
for(intj=0;
if(Queen[i]==j)
Console.Write("
★"
elseif(i%2==0&
j%2==0||i%2!
=0&
j%2!
□"
else
C