基本信息
源码名称:C# 冒泡排序算法 示例源码
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2017-09-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 排序_冒泡
{
class Program
{
public int[] BubbleSort(int[] DisOrder)
{
int temp;
for (int i = 0; i < DisOrder.Length - 1; i )
{
for (int j = DisOrder.Length - 1; j > i; j--)
{
if (DisOrder[j] > DisOrder[i])
{
temp = DisOrder[i];
DisOrder[i] = DisOrder[j];
DisOrder[j] = temp;
}
}
}
return DisOrder;
}
static void Main(string[] args)
{
SortFunction sort1 = new SortFunction();
//Program abc = new Program();
int[] sort = new int[5]{1,2,5,4,3};
//sort = abc.BubbleSort(sort);
// SortFunction.quicksort(sort,0,4);
Selection.selectionsort(sort);
Console.Write("排序结果为:");
for (int i = 0; i < 5; i )
{
Console.Write(sort[i]);
}
Console.ReadKey();
}
}
/// <summary>
/// 快速排序
/// </summary>
class SortFunction
{
public static int[] quicksort(int[] arr, int low, int heigh)
{
if (low <= heigh)
{
int division = SortFunction.partition(arr, low, heigh);
if (division == 0)
{
// quicksort(arr, low, division - 1);
quicksort(arr, division 1, heigh);
}
quicksort(arr,low,division-1);
quicksort(arr, division 1, heigh);
}
return arr;
}
public static int partition(int[] arr, int low, int heigh)
{
int ba = arr[low];//用子表的第一个记录做枢轴(分水岭)记录
while (low < heigh)
{
//更改下面两个while循环中的<=和>=,即可获取到从大到小排列
//从表的两端交替向中间扫描,从小到大排列
while(low<heigh&&arr[heigh]>=ba)
{
heigh--;
}
swap(arr,heigh,low);
// 如果高位小于base,base 赋值给 当前 heigh 位,base 挪到(互换)到了这里,heigh位右边的都比base大
while (low < heigh && arr[low] <= ba)
{
low ;
}
// 如果低位大有base,
swap(arr,heigh,low);
}
//现在low=heigh
return low;
}
private static void swap(int[] arr, int heigh, int low)
{
int temp = arr[heigh];
arr[heigh] = arr[low];
arr[low] = temp;
}
}
class Selection
{
public static int[] selectionsort(int[] arr)
{
for (int i = 0; i < arr.Length; i )
{
for (int j = i 1; j < arr.Length; j )
{
if (arr[j] > arr[i])
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
}
class Heap
{
//public static int[] HeapSort(int[] arr)
//{
//}
}
class Insert
{
public static int[] InsertSort(int[] arr)
{
for (int i = 1; i < arr.Length; i )
{
//待插入元素
int temp = arr[i];
int j;
for (j = i - 1; j >= 0 && temp < arr[j]; j--)
{
arr[j 1] = arr[j];
}
arr[j 1] = temp;
}
return arr;
}
}
class Shell
{
public static int[] ShellInsert(int[] arr)
{
int step = arr.Length / 2;//取增量
while (step >= 1)
{
for (int i = step; i < arr.Length; i )
{
int temp = arr[i];
int j = 0;
for (j = i - step; j >= 0 && temp < arr[j]; j -= step)
{
arr[j step] = arr[j];
}
arr[j step] = temp;
}
step /= 2;
}
return arr;
}
}
}