using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsolePractice
{
class CArray
{
private int[] arr;
//数组大小
private int upper;
//下标
private int numElements; /// <summary>
/// 初始化数组参数
/// </summary>
/// <param name="size"></param>
public CArray(int size)
{
arr = new int[size];
upper = size - ;
numElements = ;
} /// <summary>
/// 插入方法
/// </summary>
/// <param name="item">存储的数</param>
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
} /// <summary>
/// 输出方法
/// </summary>
public void DisplayElements()
{
for (int i = ; i <= upper; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
} /// <summary>
/// 清除数组
/// </summary>
public void Clear()
{
for (int i = ; i <= upper; i++)
{
arr[i] = ;
}
numElements = ;
} #region 快速排序算法
/// <summary>
/// 快速排序算法
/// </summary>
public void QSort()
{
RecQSort(, numElements - );
} public void RecQSort(int first, int last)
{
if ((last - first) <= )
return;
else
{
int part = this.Partition(first, last);
RecQSort(first, part - );
RecQSort(part + , last);
}
} public int Partition(int first, int last)
{
int pivotVal = arr[first];
int theFirst = first;
bool okSide;
first++;
do
{
okSide = true;
while (okSide)
{
if (arr[first] > pivotVal)
okSide = false;
else
{
first++;
okSide = (first <= last);
}
}
okSide = true;
while (okSide)
{
if (arr[last] <= pivotVal)
okSide = false;
else
{
last--;
okSide = (first <= last);
}
}
if (first < last)
{
Swap(first, last);
this.DisplayElements();
first++;
last--;
} } while (first <= last);
Swap(theFirst, last);
this.DisplayElements();
return last;
} public void Swap(int item1, int item2)
{
int temp = arr[item1];
arr[item1] = arr[item2];
arr[item2] = temp;
}
#endregion
} class C_shape
{
static void Main()
{
CArray nums = new CArray();
Random rnd = new Random();
for (int i = ; i < ; i++)
{
nums.Insert(rnd.Next(, ));
}
Console.WriteLine("Before sorting:");
nums.DisplayElements();
Console.WriteLine("During sorting:");
nums.QSort();
Console.WriteLine("After sorting:");
nums.DisplayElements();
Console.ReadKey();
}
}
}