Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1 {
  7.     class Program {
  8.         static void Main(string[] args) {
  9.             int[] tokuda = { 1, 4, 9, 20, 46, 103, 233, 525, 1182, 2660, 5985, 13467, 30301, 68178, 153401, 345152, 776591 };
  10.             int[] s248 = { 1, 3, 7, 16, 38, 94, 233, 577, 1431, 3549, 8801, 21826, 54128, 134237, 332908, 825611 };
  11.             int[] sizes = {10000, 50000, 100000, 200000, 300000, 500000, 1000000};
  12.  
  13.             string csv = "";
  14.             foreach(int size in sizes) {
  15.                 // foreach size, try 100 times.
  16.                 for (int k = 0; k < 100; k++) {
  17.                     int[] list = new int[size];
  18.                     Random rnd = new Random();
  19.  
  20.                     for (int i = 0; i < list.Length; i++) {
  21.                         list[i] = rnd.Next(0, Int32.MaxValue);
  22.                     }
  23.  
  24.                     int[] tmp = (int[])list.Clone();
  25.                     int c = shellsort(tmp, tokuda);
  26.  
  27.                     // Writing the CSV in the following format
  28.                     //  Size of Array, Tokuda's comparison, Tokuda's swap
  29.                     csv += size + "," + c + "," + swap;
  30.                     tmp = (int[])list.Clone();
  31.  
  32.                     c = shellsort(tmp, s248);
  33.                     csv += "," + c + "," + swap + "\n";
  34.                 }
  35.             }
  36.  
  37.             System.IO.File.WriteAllText("result.csv", csv);
  38.         }
  39.  
  40.         public static int compare = 0;
  41.         public static int swap = 0;
  42.  
  43.         public static bool greaterthan(int a, int b) {
  44.             compare++;
  45.             return a > b;
  46.         }
  47.  
  48.         public static int shellsort(int[] a, int[] gaps) {
  49.             // For keeping track of number of swap and comparison
  50.             compare = 0;
  51.             swap = 0;
  52.  
  53.             int temp, gap, i, j;
  54.  
  55.             // Finding a gap that is smaller than the length of the array
  56.             int gap_index = gaps.Length - 1;
  57.             while (gaps[gap_index] > a.Length) gap_index--;
  58.  
  59.             while (gap_index >= 0) {
  60.                
  61.                 // h-sorting
  62.                 gap = gaps[gap_index];
  63.                 for (i = gap; i < a.Length; i++) {
  64.                     temp = a[i];
  65.                     for(j = i; (j >= gap) && (greaterthan(a[j - gap], temp)); j -= gap) {
  66.                         a[j] = a[j - gap];
  67.                         swap++;
  68.                     }
  69.  
  70.                     // swapping
  71.                     a[j] = temp;
  72.                     swap++;
  73.                 }
  74.  
  75.                 gap_index--;
  76.             }
  77.  
  78.             return compare;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement