My desktop averages about 24.9 seconds, which, of course, depends on how scattered the integers are in the array.
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace InsertionSorting
7: {
8: class Program
9: {
10: static void Main(string[] args)
11: {
12: // int[] A = {5, 2, 4, 6, 1, 3};
13: int initializer = 100000;
14: int[] A = new int[initializer];
15:
16: for (int c = 0; c < A.Length; c++)
17: {
18: A[c] = c;
19: }
20:
21: // Random shuffle
22: Random rand = new Random();
23: for (int d = 0; d < A.Length; d++)
24: {
25: int firstInt = A[d];
26: int swapIndex = rand.Next(initializer);
27: int secondInt = A[swapIndex];
28:
29: A[d] = secondInt;
30: A[swapIndex] = firstInt;
31: }
32:
33: //Console.Write("Array before sorting: ");
34: //foreach (int number in A)
35: //{
36: // Console.Write("{0} ", number);
37: //}
38: //Console.WriteLine("");
39:
40: int key;
41: int i;
42: int j;
43:
44: Console.Write("Insertion Sorting array of {0} numbers, please wait...", initializer);
45:
46: DateTime start = DateTime.Now;
47: for (j = 1; j < A.Length; j++)
48: {
49: key = A[j];
50: i = j;
51:
52: while (i > 0 && A[i - 1] > key)
53: {
54: A[i] = A[i-1];
55: i--;
56: };
57:
58: A[i] = key;
59: }
60: DateTime finish = DateTime.Now;
61:
62: //Console.Write("Array after sorting: ");
63: //foreach (int number in A)
64: //{
65: // Console.Write("{0} ", number);
66: //}
67:
68: Console.WriteLine("");
69: Console.WriteLine("Duration: {0} seconds", (finish - start).ToString());
70: }
71: }
72: }
73:
No comments:
Post a Comment