冒泡排序 二分查找
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int[] myArray = new int[] { 30,1,-9,70,25,44,67,34,88,30,23,-18};
for(int i = 0; i < myArray.Length-1; i++)
{
for (int j = 0; j < myArray.Length-1-i; j++)
{
if(myArray[j] > myArray[j+1])
{
int temp = myArray[j+1];
myArray[j+1] = myArray[j];
myArray[j] = temp;
}
}
}
String str="";
for (int i = 0; i < myArray.Length; i++)
{
str = str+" "+myArray[i].ToString();
}
label2.Text = str;
}
}
}
冒泡排序
二分查找
在label中无法显示数组
解决方法:String str="";定义 字符串str并赋初值” ”后
str = str+" "+myArray[i].ToString();用str接收已排序数组再在label中显示。
刚开始写二分查找时找到分支
if (sortArray[mid] == num) labelRes.Text = "已找到";
程序陷入死循环
解决方法:加break;结束