C#中使用快速排序按文件創(chuàng)建時間將文件排序的源碼
更新時間:2007年03月09日 00:00:00 作者:
快速排序類
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// 快速排序算法
/// </summary>
public class MyQuickSort
{
public MyQuickSort()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 快速排序算法
/// </summary>
/// 快速排序?yàn)椴环€(wěn)定排序,時間復(fù)雜度O(nlog2n),為同數(shù)量級中最快的排序方法
/// <param name="arr">劃分的數(shù)組</param>
/// <param name="low">數(shù)組低端上標(biāo)</param>
/// <param name="high">數(shù)組高端下標(biāo)</param>
/// <returns></returns>
static int Partition(FileInfo[] arr, int low, int high)
{
//進(jìn)行一趟快速排序,返回中心軸記錄位置
// arr[0] = arr[low];
FileInfo pivot = arr[low];//把中心軸置于arr[0]
while (low < high)
{
while (low < high && arr[high].CreationTime <= pivot.CreationTime)
--high;
//將比中心軸記錄小的移到低端
Swap(ref arr[high], ref arr[low]);
while (low < high && arr[low].CreationTime >= pivot.CreationTime)
++low;
Swap(ref arr[high], ref arr[low]);
//將比中心軸記錄大的移到高端
}
arr[low] = pivot; //中心軸移到正確位置
return low; //返回中心軸位置
}
static void Swap(ref FileInfo i, ref FileInfo j)
{
FileInfo t;
t = i;
i = j;
j = t;
}
/// <summary>
/// 快速排序算法
/// </summary>
/// 快速排序?yàn)椴环€(wěn)定排序,時間復(fù)雜度O(nlog2n),為同數(shù)量級中最快的排序方法
/// <param name="arr">劃分的數(shù)組</param>
/// <param name="low">數(shù)組低端上標(biāo)</param>
/// <param name="high">數(shù)組高端下標(biāo)</param>
public static void QuickSort(FileInfo[] arr, int low, int high)
{
if (low <= high - 1)//當(dāng) arr[low,high]為空或只一個記錄無需排序
{
int pivot = Partition(arr, low, high);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, high);
}
}
}
如使用其它排序算法請參考: http://www.yaosansi.com/blog/article.asp?id=980
使用方法:
System.IO.DirectoryInfo dir = new DirectoryInfo(currentFolder);
System.IO.FileInfo[] files = dir.GetFiles();
MyQuickSort.QuickSort(files, 0, files.Length - 1);//按時間排序
使用后:
如果files的長度大于0,那么files[0]為創(chuàng)建時間最新的文件.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// 快速排序算法
/// </summary>
public class MyQuickSort
{
public MyQuickSort()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 快速排序算法
/// </summary>
/// 快速排序?yàn)椴环€(wěn)定排序,時間復(fù)雜度O(nlog2n),為同數(shù)量級中最快的排序方法
/// <param name="arr">劃分的數(shù)組</param>
/// <param name="low">數(shù)組低端上標(biāo)</param>
/// <param name="high">數(shù)組高端下標(biāo)</param>
/// <returns></returns>
static int Partition(FileInfo[] arr, int low, int high)
{
//進(jìn)行一趟快速排序,返回中心軸記錄位置
// arr[0] = arr[low];
FileInfo pivot = arr[low];//把中心軸置于arr[0]
while (low < high)
{
while (low < high && arr[high].CreationTime <= pivot.CreationTime)
--high;
//將比中心軸記錄小的移到低端
Swap(ref arr[high], ref arr[low]);
while (low < high && arr[low].CreationTime >= pivot.CreationTime)
++low;
Swap(ref arr[high], ref arr[low]);
//將比中心軸記錄大的移到高端
}
arr[low] = pivot; //中心軸移到正確位置
return low; //返回中心軸位置
}
static void Swap(ref FileInfo i, ref FileInfo j)
{
FileInfo t;
t = i;
i = j;
j = t;
}
/// <summary>
/// 快速排序算法
/// </summary>
/// 快速排序?yàn)椴环€(wěn)定排序,時間復(fù)雜度O(nlog2n),為同數(shù)量級中最快的排序方法
/// <param name="arr">劃分的數(shù)組</param>
/// <param name="low">數(shù)組低端上標(biāo)</param>
/// <param name="high">數(shù)組高端下標(biāo)</param>
public static void QuickSort(FileInfo[] arr, int low, int high)
{
if (low <= high - 1)//當(dāng) arr[low,high]為空或只一個記錄無需排序
{
int pivot = Partition(arr, low, high);
QuickSort(arr, low, pivot - 1);
QuickSort(arr, pivot + 1, high);
}
}
}
如使用其它排序算法請參考: http://www.yaosansi.com/blog/article.asp?id=980
使用方法:
System.IO.DirectoryInfo dir = new DirectoryInfo(currentFolder);
System.IO.FileInfo[] files = dir.GetFiles();
MyQuickSort.QuickSort(files, 0, files.Length - 1);//按時間排序
使用后:
如果files的長度大于0,那么files[0]為創(chuàng)建時間最新的文件.
相關(guān)文章
C# 如何設(shè)置label(標(biāo)簽)控件的背景顏色為透明
這篇文章主要介紹了C# 如何設(shè)置label(標(biāo)簽)控件的背景顏色為透明,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-10-10C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04