c#常见算法

本文介绍了一款使用 C# 实现的多种排序算法的应用程序,包括冒泡排序、二分法排序、插入法排序和选择排序。通过随机生成整数数组并展示排序过程,演示了各种排序算法的工作原理及其在实际应用中的效果。

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyPro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] arrayRand = new int[10];
private void btnRandomArray_Click(object sender, EventArgs e)
{
Random r = new Random();
for (int i = 0; i < arrayRand.Length; i++)
{
arrayRand[i] = r.Next(0, 101);
}
ShowArrayToUI(rtxtRand, arrayRand);
}

private void ShowArrayToUI(RichTextBox t, int[] array)
{
string text = "";
for (int i = 0; i < array.Length; i++)
{
text += array[i].ToString() + "\n";
}
t.Text = text;
}
/// <summary>
/// 冒泡排序
/// </summary>
private void btnMaopaoSort_Click(object sender, EventArgs e)
{
int[] array = new int[arrayRand.Length];
Array.Copy(arrayRand, array, arrayRand.Length);
int temp;
for (int i = array.Length - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}

ShowArrayToUI(this.rtxtSort, array);
}

/// <summary>
/// 二分法排序
/// </summary>
private void btnMidSort_Click(object sender, EventArgs e)
{
int[] array = new int[arrayRand.Length];
Array.Copy(arrayRand, array, arrayRand.Length);

int temp;
int start, end, mid;
for (int i = 0; i < array.Length; i++)
{
start = 0;
end = i - 1;
temp = array[i];
while (start <= end)
{
mid = (start + end) / 2;
if (array[mid] < temp)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
for (int j = i - 1; j > end; j--)
{
array[j + 1] = array[j];
}
array[end + 1] = temp;
}
ShowArrayToUI(this.rtxtSort, array);
}

/// <summary>
/// 插入法排序 每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止
/// </summary>
private void btnInsertSort_Click(object sender, EventArgs e)
{
int[] array = new int[arrayRand.Length];
Array.Copy(arrayRand, array, arrayRand.Length);
int temp;
int counter;
for (int i = 1; i < array.Length; i++)
{
counter = i;
temp = array[i];
while (counter > 0 && (array[counter - 1] > temp))
{
array[counter] = array[counter - 1];
counter--;
}
array[counter] = temp;
}


ShowArrayToUI(this.rtxtSort, array);
}

/// <summary>
/// 选择排序 选择排序是从冒泡排序演化而来的,每一轮比较得出最小的那个值,然后依次和每轮比较的第一个值进行交换。
/// </summary>
private void btnChooseSort_Click(object sender, EventArgs e)
{
int[] array = new int[arrayRand.Length];
Array.Copy(arrayRand, array, arrayRand.Length);

int temp;
int tempIndex;
for (int i = 0; i < array.Length; i++)
{
temp = array[i];
tempIndex = i;
//找出最小的一个
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < temp)
{
temp = array[j];
tempIndex = j;
}
}

//交换位置
temp = array[i];
array[i] = array[tempIndex];
array[tempIndex] = temp;
}

this.ShowArrayToUI(this.rtxtSort, array);
}
}
}

转载于:https://www.cnblogs.com/chenzhou-blog/archive/2012/06/20/2556405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值