using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SortCollection
{
class Program
{
static void Main(string[] args)
{
int[] intArray = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
int[] nums = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
//InsertSort(intArray);
SelectSort(nums);
//BubbleSort(intArray);
foreach (int item in nums)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
//插入排序
public static void InsertSort(int[] nums)
{
for (int i = 1; i < nums.Length; i++)
{
int temp = nums[i];
int j = i;
while (j > 0 && nums[j - 1] > temp)
{
nums[j] = nums[j - 1];
--j;
}
nums[j] = temp;
}
}
//选择排序
public static void SelectSort(int[] nums)
{
int min;
for (int i = 0; i < nums.Length - 1; i++)
{
min = i;
for (int j = i + 1; j < nums.Length; j++)
{
if (nums[j] < nums[min])
{
min = j;
}
int temp = nums[min];
nums[min] = nums[i];
nums[i] = temp;
}
}
}
//冒泡排序
public static void BubbleSort(int[] nums)
{
int j = 1, temp;
bool flag = false;
while (j < nums.Length && !flag)
{
flag = true;
for (int i = 0; i < nums.Length - 1; i++)
{
if (nums[i] > nums[i + 1])
{
flag = false;
temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
j++;
}
}
}
}
转载于:https://www.cnblogs.com/eva_2010/articles/2045176.html