C# 常用排序方法 冒泡排序,直接选择排序,直接插入排序
2007-08-11 15:29
495 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace Sorts
{
class Sort
{
public void swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
public void outlist(int[] arr)
{
foreach (int x in arr) //输出
{
Console.Write("{0}t", x);
}
}
public void BubbleSort(int[] arr) //冒泡排序
{
int i, j, change=0,loop=0;
bool exchange;
for (i = 0 ; i < arr.Length ; i++) //最多做R.Length-1次排序
{
exchange = false;
// ***从大到小排列***
//for (j = arr.Length - 2; j >= i; j--)
//{
// if (arr[ j + 1 ] > arr[ j ])
// {
// swap(ref arr[j], ref arr[j + 1]);
// exchange = true;
// }
//***从小到大排列***
for (j = 0; j < arr.Length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(ref arr[j], ref arr[j + 1]);
change++;
exchange = true;
}
}
if (!exchange) //本次未发生排序,提前终止算法
{ break; }
loop++;
}
Console.WriteLine("冒泡排序结果:");
outlist(arr);
Console.WriteLine("本次排序交换{0}次,循环{1}次n",change,loop);
}
public void SelectSort(int[] arr) //直接选择排序
{
int i,j,min,change=0,loop=0;
for ( i = 0; i < arr.Length-1; i++)
{
min = i;
for ( j = i+1; j < arr.Length; j++)
{
if (arr[j] < arr[min]) //此处改为">"则从大到小排列
min = j;
}
swap(ref arr[min], ref arr[i]);
change++;
loop++;
}
Console.WriteLine("直接排序结果");
outlist(arr);
Console.WriteLine("本次排序交换{0}次,循环{1}次n", change, loop);
}
public void StraightSort(int[] arr) //直接插入排序
{
int i, j,temp;
for (i = 1; i < arr.Length; i++)
{
j = i ;
temp = arr[i];
while ((j > 0) && (arr[j-1] >= temp))
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
Console.WriteLine("直接插入结果");
outlist(arr);
}
static void Main()
{
Sort obj = new Sort();
int[] myarr = new int[10] ;
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
myarr[i] = rand.Next(0, 100);
}
Console.WriteLine("随机数组是:");
foreach (int x in myarr) //输出
{
Console.Write("{0}t", x);
}
Console.WriteLine();
obj.BubbleSort(myarr);
obj.SelectSort(myarr);
obj.StraightSort(myarr);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Sorts
{
class Sort
{
public void swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
public void outlist(int[] arr)
{
foreach (int x in arr) //输出
{
Console.Write("{0}t", x);
}
}
public void BubbleSort(int[] arr) //冒泡排序
{
int i, j, change=0,loop=0;
bool exchange;
for (i = 0 ; i < arr.Length ; i++) //最多做R.Length-1次排序
{
exchange = false;
// ***从大到小排列***
//for (j = arr.Length - 2; j >= i; j--)
//{
// if (arr[ j + 1 ] > arr[ j ])
// {
// swap(ref arr[j], ref arr[j + 1]);
// exchange = true;
// }
//***从小到大排列***
for (j = 0; j < arr.Length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(ref arr[j], ref arr[j + 1]);
change++;
exchange = true;
}
}
if (!exchange) //本次未发生排序,提前终止算法
{ break; }
loop++;
}
Console.WriteLine("冒泡排序结果:");
outlist(arr);
Console.WriteLine("本次排序交换{0}次,循环{1}次n",change,loop);
}
public void SelectSort(int[] arr) //直接选择排序
{
int i,j,min,change=0,loop=0;
for ( i = 0; i < arr.Length-1; i++)
{
min = i;
for ( j = i+1; j < arr.Length; j++)
{
if (arr[j] < arr[min]) //此处改为">"则从大到小排列
min = j;
}
swap(ref arr[min], ref arr[i]);
change++;
loop++;
}
Console.WriteLine("直接排序结果");
outlist(arr);
Console.WriteLine("本次排序交换{0}次,循环{1}次n", change, loop);
}
public void StraightSort(int[] arr) //直接插入排序
{
int i, j,temp;
for (i = 1; i < arr.Length; i++)
{
j = i ;
temp = arr[i];
while ((j > 0) && (arr[j-1] >= temp))
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
Console.WriteLine("直接插入结果");
outlist(arr);
}
static void Main()
{
Sort obj = new Sort();
int[] myarr = new int[10] ;
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
myarr[i] = rand.Next(0, 100);
}
Console.WriteLine("随机数组是:");
foreach (int x in myarr) //输出
{
Console.Write("{0}t", x);
}
Console.WriteLine();
obj.BubbleSort(myarr);
obj.SelectSort(myarr);
obj.StraightSort(myarr);
}
}
}
相关文章推荐
- java中各种常用排序实现(直接插入排序、直接选择排序、堆排序、冒泡排序、快速排序和归并排序)
- Java 选择排序、冒泡排序、快速排序、直接插入排序、Java自带的排序等方法的使用和总结
- 冒泡排序 快速排序 选择排序 堆排序 直接插入排序 希尔排序 归并排序
- 冒泡排序-直接选择排序-直接插入排序-希尔排序-java实现
- 排序算法汇总(选择排序 ,直接插入排序,冒泡排序,希尔排序,快速排序,堆排序)
- 冒泡排序、选择排序、直接插入排序(java实现)
- 直接插入排序,选择排序,冒泡排序
- 排序算法---基础算法(冒泡排序,快速排序,选择排序,直接插入排序,桶排序)
- java实现三种简单排序以及改良:冒泡排序,选择排序,直接插入排序
- 冒泡排序、直接插入排序、选择插入排序
- 菜鸟学编程之三:三种最基本排序算法的实现(冒泡排序、选择排序、直接插入排序)
- 常用的选择排序.Shell排序.快速排序.冒泡排序.插入排序的算法
- c语言各种常见排序(直接插入排序、折半插入排序、冒泡排序、选择排序、堆排序)
- 随机产生及冒泡排序、选择排序、直接插入排序
- Java程序实现冒泡排序、直接排序、插入排序方法
- 三种最简单基础的排序 选择排序 冒泡排序 直接插入排序 运用了函数指针数组
- 插入排序,选择排序,冒泡排序等常用排序算法(java实现)
- 基本排序方法:冒泡排序、简单选择排序、直接插入排序
- C语言常用排序——直接插入,冒泡,选择,希尔,快速,堆排序
- java版排序算法简介及冒泡排序以及优化,选择排序,直接插入排序,希尔排序,堆排序,快速排序及其优化前言 2 分类 2 稳定性 3 时间复杂度 4 Java实现版本 5 1、冒泡排序 6 2、选择排序