Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Last active December 11, 2015 17:08
Show Gist options
  • Save gogsbread/4632205 to your computer and use it in GitHub Desktop.
Save gogsbread/4632205 to your computer and use it in GitHub Desktop.
Testing Sort algorithms -Insertion & Quicksort . Tested with Sorted,ReverseSorted and Random suites of n=2^3 to 2^8. Results show that insertionsort beats quicksort for lower n and things change between n = 64 to 256
/*
Testing Sort algorithms -Insertion & Quicksort . Tested with Sorted,ReverseSorted and Random suites of n=2^3 to 2^8. Results show that insertionsort beats quicksort for lower n and things change between n = 64 to 256
---Testing with n=8---
QuickSort(sorted,reversesorted,random): 5322 11 6
InsertionSort(sorted,reversesorted,random): 1745 4 3
---Testing with n=16---
QuickSort(sorted,reversesorted,random): 30 19 19
InsertionSort(sorted,reversesorted,random): 13 11 8
---Testing with n=32---
QuickSort(sorted,reversesorted,random): 38 36 36
InsertionSort(sorted,reversesorted,random): 16 31 20
---Testing with n=64---
QuickSort(sorted,reversesorted,random): 45 56 61
InsertionSort(sorted,reversesorted,random): 13 62 42
---Testing with n=128---
QuickSort(sorted,reversesorted,random): 111 161 175
InsertionSort(sorted,reversesorted,random): 25 331 149
---Testing with n=256---
QuickSort(sorted,reversesorted,random): 152 230 295
InsertionSort(sorted,reversesorted,random): 21 1077 720
*/
using System;
using System.Diagnostics;
using Sorting;//https://github.com/antonydeepak/Practical_DSA/tree/master/basic_algorithms
delegate void SortFunction(int[] list);
class SortTester
{
static void Main(){
Console.WriteLine();
Tuple<string,string,string> elapsedTimes;
for(int i=8;i<257;i=i<<1)
{
Console.WriteLine("---Testing with n={0}---",i);
elapsedTimes = TestSort(QuickSorted.QuickSort,i);
Console.WriteLine("QuickSort(sorted,reversesorted,random): {0} {1} {2}",elapsedTimes.Item1,elapsedTimes.Item2,elapsedTimes.Item3);
elapsedTimes = TestSort(InsertionSorted.InsertionSort,i);
Console.WriteLine("InsertionSort(sorted,reversesorted,random): {0} {1} {2}",elapsedTimes.Item1,elapsedTimes.Item2,elapsedTimes.Item3);
Console.WriteLine();
}
}
static Tuple<string,string,string> TestSort(SortFunction sf,int n){
int[] sorted = CreateSorted(n);
int[] rsorted = CreateReverseSorted(n);
int[] random = CreateRandom(n);
string sortedTime = TimeFunction(sf,sorted);
string rSortedTime = TimeFunction(sf,rsorted);
string randomTime = TimeFunction(sf,random);
return new Tuple<string,string,string>(sortedTime,rSortedTime,randomTime);
}
static string TimeFunction(SortFunction func,int[] list){
Stopwatch timer = new Stopwatch();
timer.Start();
func(list);
timer.Stop();
TimeSpan elapsed = timer.Elapsed;
return elapsed.Ticks.ToString();
}
static int[] CreateSorted(int n){
int[] sorted = new int[n];
int count=0;
int i=-(n/2);
while(count<n){
sorted[count] = i;
count++;
i++;
}
return sorted;
}
static int[] CreateReverseSorted(int n){
int[] sorted = new int[n];
int count=0;
int i=-(n/2);
while(count<n){
sorted[n-count-1] = i;
count++;
i++;
}
return sorted;
}
static int[] CreateRandom(int n){
int[] random = new int[n];
Random r = new Random();
int count=0;
while(count < n){
random[count] = r.Next();
count++;
}
return random;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment