using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/********冒泡排序算法*********/
namespace MaoPaoApplication
{
class Program
{
static void Main(string[] args)
{
SortedNumbers();
}
public static void SortedNumbers()
{
int numberCount = 1;//預設升序
int arryLength; //聲明數組長度變量
Console.WriteLine("----------冒泡排序法----------");
Console.WriteLine("請問輸入你想要排序的數組:");
string s = Console.ReadLine();
arryLength = s.Length;
char [] chars=s.ToCharArray();
Console.WriteLine("\n您要對這{0}個數字進行什麼排序?(1表示升序,2表示降序)", numberCount);
int method = Convert.ToInt32(Console.ReadLine());
while (method != 1 && method != 2)
{
Console.WriteLine("隻能輸入1或者2,請您重新輸入!");
method = Convert.ToInt32(Console.ReadLine());
}
//調用排序方法
ExecuteSortedMethod(chars, method);
Console.WriteLine("排序後的結果為:");
for (int i = 0; i < numberCount; i++)
{
Console.Write(chars[i].ToString() + "\t");
}
Console.WriteLine("\n----------剛剛冒泡排序法----------");
Console.ReadKey();
}
/// <summary>
/// 排序算法
/// </summary>
/// <param name="num"></param>
/// <param name="sortedMethod"></param>
public static void ExecuteSortedMethod(char[] num, int sortedMethod)
{
//升序排列
if (sortedMethod == 1)
{
for (int i = 0; i < num.Length; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
char temp = num[j];
num[i] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
//降序排列
if (sortedMethod == 2)
{
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] < num[j + 1])
{
char temp = num[j];
num[j] = num[j + 1];
num[j + 1] = temp;
}
}
}
}
}
}
}