把自己面试时候看到的有关排序的题总结一下:
主要的有冒泡和选择排序,其它的还有插入排序等.
protected void Page_Load(object sender, EventArgs e)
{
// Class1 a = new Class1();
//int[] a= new int[5]{1,2,3,4,5};
this.maopaoOrder();
this.selectOrder();
}
/// <summary>
/// /冒泡排序
/// </summary>
private void maopaoOrder()
{
//冒泡排序
int[] list = new int[5] { 111, 12, 223, 854, -5655 };//初始化数组
int i, j, temp;
for (j = 1; j < list.Length;j++ )
{
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
int ii = 0;
}
/// <summary>
/// 选择排序
/// </summary>
private void selectOrder()
{
//选择排序
int[] a = new int[5] { 111, 12, 223, 854, -5655 };//初始化数组
int min, min_k;//定义最小数,和最小数的下标
for (int i = 0; i < 5; i++)
{
min = a[i];//将当前循环的数设置成最小数
min_k = i;
for (int j = i + 1; j < 5; j++)
{
if (a[j] < min)
{
min = a[j];
min_k = j;
int tem = a[min_k];
a[min_k] = a[i];
a[i] = tem;
}
}
}
int ii = 0;
}
{
// Class1 a = new Class1();
//int[] a= new int[5]{1,2,3,4,5};
this.maopaoOrder();
this.selectOrder();
}
/// <summary>
/// /冒泡排序
/// </summary>
private void maopaoOrder()
{
//冒泡排序
int[] list = new int[5] { 111, 12, 223, 854, -5655 };//初始化数组
int i, j, temp;
for (j = 1; j < list.Length;j++ )
{
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
}
int ii = 0;
}
/// <summary>
/// 选择排序
/// </summary>
private void selectOrder()
{
//选择排序
int[] a = new int[5] { 111, 12, 223, 854, -5655 };//初始化数组
int min, min_k;//定义最小数,和最小数的下标
for (int i = 0; i < 5; i++)
{
min = a[i];//将当前循环的数设置成最小数
min_k = i;
for (int j = i + 1; j < 5; j++)
{
if (a[j] < min)
{
min = a[j];
min_k = j;
int tem = a[min_k];
a[min_k] = a[i];
a[i] = tem;
}
}
}
int ii = 0;
}