// 2013年3月13日19:19:01 吴新强
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
namespace Chapter3_SelectionSort
{
class CArray
{
static void Main(string[] args)
{
CArray ca = new CArray(10);
Timing tObj = new Timing();
Random random = new Random(100);//随机数
for (int i = 0; i < 10; i++)
{
ca.Insert(random.Next(0, 100));
}
Console.WriteLine("InsertionSort Before Sorting:");//排序前
Console.WriteLine();
ca.DisplayElements();//显示数据
Console.WriteLine(); // 换行
tObj.StartTime();// 测试排序时间的开始时间处
Console.WriteLine();
Console.WriteLine("InsertionSort During Sorting:");//排序中
Console.WriteLine();
ca.InsertionSort();// 调用选择排序法进行排序
Console.WriteLine();
tObj.StopTime();// 测试排序时间的截止时间
Console.WriteLine("InsertionSort After Sorting:");//排序后
Console.WriteLine();
ca.DisplayElements();//显示数据
Console.WriteLine();
Console.WriteLine("InsertionSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");// 显示选择排序法共使用了多少时间
Console.WriteLine();
ca.Clear();
for (int i = 0; i < 10; i++)
{
ca.Insert(random.Next(0, 100));
}
// Console.WriteLine("请输入查找的值:");
Console.WriteLine();
ca.DisplayElements();//显示数据
Console.WriteLine(); // 换行
tObj.StartTime();// 测试排序时间的开始时间处
Console.WriteLine();
Console.WriteLine("请输入查找的值:");
ca.RbinSearch(5,1,10 );// 调用折半查找法进行查找
int v=Convert .ToInt32(Console .ReadLine());
// if(value==mid)
// Console.WriteLine("你查找的值存在!");
// else
// Console.WriteLine("你查找的值不存在!");
Console.WriteLine();
tObj.StopTime();// 测试查找时间的截止时间
Console.WriteLine();
ca.DisplayElements();//显示数据
Console.WriteLine();
Console.WriteLine("RbinSearch time( .net):" + tObj.Result().TotalMilliseconds + " MS");// 显示选择排序法共使用了多少时间
}
public void InsertionSort()// 插入排序法
{
int temp, inner;
for (int outer = 1; outer <= upper; outer++)
{
inner = outer;
temp = array[outer];
while (inner >0 &&array [inner -1]>=temp)
{
array [inner]=array [inner -1];
inner -= 1;
}
array[inner] = temp;
this.DisplayElements();
}
}
public int RbinSearch(int value, int lower, int upper)
{
int mid;
if (lower > upper)
return -1;
else
{
// int mid;
mid = (int)(lower + upper) / 2;
if (value < array[mid])
return RbinSearch(value, lower, mid - 1);
else if (value == mid)
return mid;
else
return RbinSearch(value, mid + 1, upper);
}
if (value == mid)
Console.WriteLine("你查找的值存在!");
else
Console.WriteLine("你查找的值不存在!");
}
private int[] array;
private int upper;
private int numElements;
public CArray(int Size)
{
array = new int[Size];
upper = Size - 1; // upper 最大长度
numElements = 0;// 数组变量
}
public void Insert(int item)
{
array[numElements] = item;
numElements++;
}
public void DisplayElements()// 显示数据
{
for (int i = 0; i <= upper; i++)
Console.Write(array[i] + " ");
}
public void Clear()// 删除数据
{
for (int i = 0; i < upper; i++)
{
array[i] = 0;
numElements = 0;
}
}
}
public class Timing // 时间测试类
{
TimeSpan duration;
public Timing()
{
duration = new TimeSpan(0);
}
public void StopTime()
{
duration=Process.GetCurrentProcess().TotalProcessorTime;
}
public void StartTime()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
public TimeSpan Result()
{
return duration;
}
}
}
实验结果: