Advertisement
NPSF3000

Insertion Sort vs Sorting After Fill

Apr 3rd, 2016
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. static class Program
  6. {
  7.  
  8.     static Random random = new Random();
  9.     [STAThread]
  10.     static void Main()
  11.     {
  12.         int loopTimes = 1000;
  13.         int listSize = 10000;
  14.         Console.WriteLine("Testing performance of filling a list to {0} items {1} times, discarding 1/2 of items after every fill!", listSize, loopTimes);
  15.         Console.WriteLine();
  16.         Random r = new Random();
  17.  
  18.         List<int> oldList = new List<int>();
  19.         List<int> newList = new List<int>();
  20.  
  21.         Stopwatch watch = new Stopwatch();
  22.  
  23.         watch.Start();
  24.         OldListTimes(loopTimes, listSize, oldList);
  25.         watch.Stop();
  26.  
  27.         Console.WriteLine("Old list: {0}ms", watch.ElapsedMilliseconds);
  28.  
  29.         watch.Restart();
  30.  
  31.         NewListTimes(loopTimes, listSize, oldList);
  32.         watch.Stop();
  33.  
  34.         Console.WriteLine("New list: {0}ms", watch.ElapsedMilliseconds);
  35.  
  36.         Console.WriteLine("DONE");
  37.         Console.ReadKey();
  38.     }
  39.  
  40.     private static void OldListTimes(int loopTimes, int listSize, List<int> list)
  41.     {
  42.  
  43.         for (var fillLoop = 0; fillLoop < loopTimes; fillLoop++)
  44.         {
  45.             for (int i = list.Count; i < listSize; i++)
  46.                 AddToSortedList(list, random.Next(100000));
  47.             //list.Sort();
  48.             list.RemoveRange(listSize / 2, listSize / 2);
  49.         }
  50.     }
  51.  
  52.     private static void NewListTimes(int loopTimes, int listSize, List<int> list)
  53.     {
  54.         for (var fillLoop = 0; fillLoop < loopTimes; fillLoop++)
  55.         {
  56.             for (int i = list.Count; i < listSize; i++)
  57.                 list.Add(random.Next(100000));
  58.             list.Sort();
  59.             list.RemoveRange(listSize / 2, listSize / 2);
  60.         }
  61.     }
  62.  
  63.     private static void AddToSortedList(List<int> sortedList, int item)
  64.     {
  65.         int min = 0;
  66.         int max = sortedList.Count;
  67.         int index = 0;
  68.         while (min < max)
  69.         {
  70.             index = (min + max) / 2;
  71.             if (item < sortedList[index])
  72.             {
  73.                 min = index + 1;
  74.                 index = min;
  75.             }
  76.             else
  77.             {
  78.                 if (item > sortedList[index])
  79.                 {
  80.                     max = index;
  81.                     index = max;
  82.                 }
  83.                 else
  84.                     break;
  85.             }
  86.         }
  87.         sortedList.Insert(index, item);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement