C#实现的3种排序算法--冒泡排序、选择排序、插入排序
2008-03-12 12:36
846 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
//mpsort(iarrary);
// xzsort(iarrary);
crsort(iarrary);
for (int n = 0; n < iarrary.Length; n++)
{
Console.WriteLine("{0}",iarrary
);
}
Console.Read();
}
public static void mpsort(int [] list)//冒泡排序
{
int i, j, temp;
bool exchange;
for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
{
exchange = false;
for (j = 0; j < list.Length-i;j++ )
{
if(list[j]>list[j+1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
exchange = true;
}
}
if (!exchange)
{
break;
}
}
}
public static void xzsort(int[] list)//选择排序
{
int i,j,temp,min;
for (i = 0; i < list.Length; i++)
{
min = i;
for (j = i + 1; j < list.Length;j++ )
{
if (list[min] > list[j])
{
min = j;
}
}
temp = list[min];
list[min] = list[i];
list[i] = temp;
}
}
public static void crsort(int[] list)//插入排序
{
int i,j,temp;
for (i = 1; i < list.Length; i++)
{
temp = list[i];
j = i;
while((j > 0)&&(list[j-1]>temp))
{
list[j]=list[j-1];
--j;
}
list[j]=temp;
}
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] iarrary = new int[] {1,5,13,6,10,55,99,2,87,12,34,75,33,47};
//mpsort(iarrary);
// xzsort(iarrary);
crsort(iarrary);
for (int n = 0; n < iarrary.Length; n++)
{
Console.WriteLine("{0}",iarrary
);
}
Console.Read();
}
public static void mpsort(int [] list)//冒泡排序
{
int i, j, temp;
bool exchange;
for (i = 1; i < list.Length; i++)//最多做list.Length-1趟排序
{
exchange = false;
for (j = 0; j < list.Length-i;j++ )
{
if(list[j]>list[j+1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
exchange = true;
}
}
if (!exchange)
{
break;
}
}
}
public static void xzsort(int[] list)//选择排序
{
int i,j,temp,min;
for (i = 0; i < list.Length; i++)
{
min = i;
for (j = i + 1; j < list.Length;j++ )
{
if (list[min] > list[j])
{
min = j;
}
}
temp = list[min];
list[min] = list[i];
list[i] = temp;
}
}
public static void crsort(int[] list)//插入排序
{
int i,j,temp;
for (i = 1; i < list.Length; i++)
{
temp = list[i];
j = i;
while((j > 0)&&(list[j-1]>temp))
{
list[j]=list[j-1];
--j;
}
list[j]=temp;
}
}
}
}
相关文章推荐
- C#实现的3种排序算法--冒泡排序、选择排序、插入排序
- C#实现所有经典排序算法(选择排序,冒泡排序,快速排序,插入排序,希尔排序)
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- 各种排序算法实现——基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- 元素排序几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- C#实现所有经典排序算法(选择排序 冒泡排序 快速排序)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- C#实现所有经典排序算法(选择排序 冒泡排序 快速排序)
- C#的排序算法:冒泡排序、选择排序、插入排序和希尔排序
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- PHP四种排序算法实现及效率分析【冒泡排序,插入排序,选择排序和快速排序】
- C语言单链表的3种排序算法,插入排序,冒泡排序,选择排序
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序
- C#实现所有经典排序算法(选择排序 冒泡排序 快速排序)
- 排序算法一:冒泡排序,插入排序以及选择排序原理与MATLAB实现
- C语言单链表的3种排序算法,插入排序,冒泡排序,选择排序