对数据进行sorting的低效algorithm

我目前有一个包含许多列的Excel电子表格。

例如

Id Name Address Class School SchoolAddress 

我想使用C#脚本将数据拆分成多个表格,其中Class School和School Address将被用作分组。 类似于SQL GROUP BY

我目前有2个循环。

 (for int i = 1; i <= range.rows.count; i++) { (for int a = 1; a <= range.rows.count; a++) //stores them in an array list and splits them //takes the Class school and school Address column to compare against } 

目前在O(n ^ 2)时间运行时间太长。 有没有人有更快的解决scheme?

道歉。 我的问题实际上是逻辑效率,而不是代码的确切实现

algorithm的名字是BubbleSort,速度很慢。 这是quickSortalgorithm,是最快的分类器algorithm之一,它的复杂度是O(n log n)。 我只用这个sorting数组。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Quicksort { class Program { static void Main(string[] args) { // Create an unsorted array of string elements string[] unsorted = { "z","e","x","c","m","q","a"}; // Print the unsorted array for (int i = 0; i < unsorted.Length; i++) { Console.Write(unsorted[i] + " "); } Console.WriteLine(); // Sort the array Quicksort(unsorted, 0, unsorted.Length - 1); // Print the sorted array for (int i = 0; i < unsorted.Length; i++) { Console.Write(unsorted[i] + " "); } Console.WriteLine(); Console.ReadLine(); } public static void Quicksort(IComparable[] elements, int left, int right) { int i = left, j = right; IComparable pivot = elements[(left + right) / 2]; while (i <= j) { while (elements[i].CompareTo(pivot) < 0) { i++; } while (elements[j].CompareTo(pivot) > 0) { j--; } if (i <= j) { // Swap IComparable tmp = elements[i]; elements[i] = elements[j]; elements[j] = tmp; i++; j--; } } // Recursive calls if (left < j) { Quicksort(elements, left, j); } if (i < right) { Quicksort(elements, i, right); } } } } 

有关QuickSort的更多信息,您可以查看此链接: http : //en.wikipedia.org/wiki/Quicksort