C#的数据结构

本文介绍了使用C#实现的两种基本排序算法:冒泡排序和选择排序,并提供了完整的代码示例。通过这两个示例,读者可以更好地理解这些经典算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif今天看了C的数据结构,无聊写了一些C#的数据结构算法. 
None.gif
class Program
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] list = new int[]dot.gif{10,9,8,7,6,5,4,3,2,1};
InBlock.gif            bubbleSort(list);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//冒泡算法
InBlock.gif
        public static void bubbleSort(int [] list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
int temp;
InBlock.gif            
int count=0;
InBlock.gif            
for (int j = list.Length; j>0;j--)//外循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
for (int i = 0; i <j-1; i++)//内循环
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
if (list[i] > list[i + 1])
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        temp 
= list[i];
InBlock.gif                        list[i] 
= list[i + 1];
InBlock.gif                        list[i 
+ 1= temp;
InBlock.gif                        count
++;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                Console.WriteLine(
"list值:" + list[j-1].ToString ());
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine(count);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//选择算法
InBlock.gif
        public static void selectSort(int [] list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
int temp = 0;
InBlock.gif            
int count = 0;
InBlock.gif            
for (int j = 0; j <list.Length ; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif              
InBlock.gif                
int MinNumber = list[j];
InBlock.gif                
int Index =j;
InBlock.gif                
for (int i = j; i < list.Length; i++)  //找最小值
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    
if (MinNumber > list[i])
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MinNumber 
= list[i];
InBlock.gif                        Index 
= i;
InBlock.gif                        count
++;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                temp 
= list[Index];
InBlock.gif                list[Index] 
= list[j];
InBlock.gif                list[j] 
= temp;
InBlock.gif                Console.WriteLine(
"list值:"+list[j]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine(count);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/heyaowen163/articles/1138399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值