namespace BubbleSort
{
class BubbleSortTest
{
/// <summary>
/// 使用嵌套迴圈實現升序排序
/// </summary>
/// <param name="arrary"></param>
public static void Sort(int[] arrary)
{
int temp = 0;
for (int i = 0; i < arrary.Length - 1; i++)
{
for (int j = 0; j < arrary.Length - 1 - i; j++)
{
if (arrary[j] > arrary[j + 1]) //將“>”改成“<”就實現降序排序
{
temp = arrary[j];
arrary[j] = arrary[j+1];
arrary[j + 1] = temp;
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
int[] arrary = { 6, 4, 7, 2, 9, 1, 5, 12, 35, 74, 14, 57, 25, 75, 26, 95, 74, 35, 38, 73 };
BubbleSortTest.Sort(arrary);
foreach (int index in arrary)
{
Console.Write(index + "\t");
}
Console.ReadKey();
}
}
}
隊列程式
namespace Queue
{
class QueueTest
{
int[] arrary;
int length = 0;
public QueueTest(int num)
{
arrary = new int[num];
}
public int Length
{
get
{
return this.length;
}
}
public void Push(int num) //資料進隊列的方法
{
if (length > 0)
{
for (int i = length - 1; i >= 0; i--) //使用迴圈將已有的資料後移一個位置,將第一個位置騰出來放置剛進入隊列的資料,讀資料時從後面讀取實現資料的先進先出,後進後出
{
arrary[i + 1] = arrary[i];
}
}
arrary[0] = num;
length++;
}
public int Pop() //資料出隊列的方法
{
return arrary[--length]; //取出最後面的一個數
}
}
class Program
{
static void Main(string[] args)
{
QueueTest queuetest = new QueueTest(100);
queuetest.Push(4);
queuetest.Push(8);
queuetest.Push(6);
queuetest.Push(17);
queuetest.Push(2);
queuetest.Push(1);
queuetest.Push(9);
while (queuetest.Length > 0)
{
Console.Write(queuetest.Pop() + "\t");
}
Console.ReadKey();
}
}
}
堆棧程式
namespace Stack
{
class StackTest
{
int[] arrary;
int length = 0;
public StackTest(int num)
{
arrary = new int[num];
}
public int Length
{
get
{
return this.length;
}
}
public void Push(int num) //資料進堆棧的方法,將新資料放入舊資料的後面,Pop()時從後面讀取數,實現先進後出,後進先出
{
arrary[length] = num;
length++;
}
public int Pop() //資料出堆棧的方法
{
return arrary[--length]; //取出最後面的一個數
}
}
class Program
{
static void Main(string[] args)
{
StackTest stacktest = new StackTest(100);
stacktest.Push(4);
stacktest.Push(8);
stacktest.Push(6);
stacktest.Push(17);
stacktest.Push(2);
stacktest.Push(1);
stacktest.Push(9);
while (stacktest.Length > 0)
{
Console.Write(stacktest.Pop() + "\t");
}
Console.ReadKey();
}
}
}