For example, I have a List
List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(7);
list.Add(3);
list.Add(17);
list.Add(10);
list.Add(13);
list.Add(9);
-
I use List.Sort method like this
private static int Compare(int x, int y) { if (x == y) return 0; else if (x > y) return -1; else return 1; } List.Sort(Compare); -
I use bubble sort like this
private static void Sort(List<int> list) { int size = list.Capacity; for (int i = 1; i < size; i++) { for (int j = 0; j < (size - i); j++) { if (list[j] > list[j+1]) { int temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; } } } }
My question like the title, I wonder that which is faster?
Thank you
On the whole, bubble sort will be slower than almost anything else, including
List.Sortwhich is implemented with a quick sort algorithm.