using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SortMethod
{
internal class Program
{
private static void Main(string[] args)
{
//1.冒泡排序
//BubbleSort();
//2.插入排序,属于稳定的排序
//InsertionSort();
//3.二分法排序
//4.希尔排序
//5.快速排序
//6.堆排序
//7.归并排序
//8.基数排序
//9.选择排序,属于不稳定的排序
int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };
SelectSort(arr);
Console.ReadLine();
//
}
public static void BubbleSort()
{
int temp = 0;
int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };
#region "该段与排序无关"
Console.WriteLine("排序前的数组:");
foreach (int item in arr)
{
Console.Write(item + "");
}
Console.WriteLine();
#endregion
for (int i = 0; i < arr.Length - 1; i++)
{
#region "将大的数字移到数组的arr.Length-1-i"
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
#endregion
}
Console.WriteLine("排序后的数组:");
foreach (int item in arr)
{
Console.Write(item + "");
}
Console.WriteLine();
Console.ReadKey();
}
///
///插入排序法,从大到小
///
private static void InsertionSort()
{
Console.WriteLine("插入排序法");
int temp = 0;
int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };
Console.WriteLine("排序前的数组:");
foreach (int item in arr)
{
Console.Write(item + ",");
}
Console.WriteLine();
var length = arr.Length;
for (int i = 1; i < length; i++)
{
for (int j = i; j > 0; j--)
{
if (arr[j] > arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
//每次排序后数组
PrintResult(arr);
}
Console.ReadKey();
}
///
///打印结果
///
///
private static void PrintResult(IEnumerable
{
foreach (int item in arr)
{
Console.Write(item + ",");
}
Console.WriteLine();
}
///
/// 选择排序
///
///
private static void SelectSort(int[] group)
{
int temp;
int pos = 0;
for (int i = 0; i < group.Length - 1; i++)
{
pos = i;
for (int j = i + 1; j < group.Length; j++)
{
if (group[j] < group[pos])
{
pos = j;
}
//第i个数与最小的数group[pos]交换
temp = group[i];
group[i] = group[pos];
group[pos] = temp;
}
PrintResult(group);
}
}
}
}