C# 数组排序方式总结
2014-01-09 18:16
369 查看
在C#中常用的数组排序的方法有:选择排序法、冒泡排序法、插入排序法和希尔排序法等。
一、选择排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
// C#选择排序法
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int min;
for (int i = 0; i < array.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[min])
min = j;
}
int t = array[min];
array[min] = array[i];
array[i] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
二、冒泡排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length; j++)
{
if (array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:26 18 10 9 7 5 3 2
三、插入排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 1; i < array.Length; i++)
{
int t = array[i];
int j = i;
while ((j > 0) && (array[j - 1] > t))
{
array[j] = array[j - 1];
--j;
}
array[j] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
四、希尔排序法
希尔排序是将组分段,然后进行插入排序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int inc;
for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= array.Length; i += inc)
{
int t = array[i - 1];
int j = i;
while ((j > inc) && (array[j - inc - 1] > t))
{
array[j - 1] = array[j - inc - 1];
j -= inc;
}
array[j - 1] = t;
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
一、选择排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
// C#选择排序法
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int min;
for (int i = 0; i < array.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[j] < array[min])
min = j;
}
int t = array[min];
array[min] = array[i];
array[i] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
二、冒泡排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length; j++)
{
if (array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:26 18 10 9 7 5 3 2
三、插入排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
for (int i = 1; i < array.Length; i++)
{
int t = array[i];
int j = i;
while ((j > 0) && (array[j - 1] > t))
{
array[j] = array[j - 1];
--j;
}
array[j] = t;
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
四、希尔排序法
希尔排序是将组分段,然后进行插入排序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { 2, 18, 9, 26, 3, 7, 5, 10 };
Console.Write("数组排序前的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.WriteLine();
int inc;
for (inc = 1; inc <= array.Length / 9; inc = 3 * inc + 1) ;
for (; inc > 0; inc /= 3)
{
for (int i = inc + 1; i <= array.Length; i += inc)
{
int t = array[i - 1];
int j = i;
while ((j > inc) && (array[j - inc - 1] > t))
{
array[j - 1] = array[j - inc - 1];
j -= inc;
}
array[j - 1] = t;
}
}
Console.Write("数组排序后的结果为:");
foreach (int n in array)
{
Console.Write("{0}", n + " ");
}
Console.ReadLine();
}
}
}
运行结果:
数组排序前的结果为:2 18 9 26 3 7 5 10
数组排序后的结果为:2 3 5 7 9 10 18 26
相关文章推荐
- Java集合和数组的排序方式总结
- Java集合和数组的排序方式总结
- Java集合和数组的排序方式总结
- Java集合和数组的排序方式总结
- c#例题:输入学生的学号,姓名,分数,然后根据分数进行排序再输出最终结果。重点学习用结构体定义数组的表达方式
- Java集合和数组的排序方式总结
- Java集合和数组的排序方式总结
- 【C#基础知识】之结构、数组及常用的几种排序方法总结
- 面试总结之数组的四种排序方式
- 数组的几种常见排序方式总结(Java程序)
- Java集合和数组的排序方式总结
- C#程序设计(二)----数组排序
- c#文件读取和写入的方式总结
- [黑马程序员]数组排序--冒泡排序原理总结
- PHP数组总结:多维数组排序
- C# 拷贝数组的几种方式
- Object-C,NSArraySortTest,数组排序3种方式
- 工作总结 string数组 排序 string数组 比较
- 选择法对数据进行排序 指针和数组两种方式 经典
- c# 使用linq查询子句方式实现 字符串数组统计操作