Skip to content

Instantly share code, notes, and snippets.

@jagmeet-chaudhary
Created June 30, 2020 02:55
Show Gist options
  • Save jagmeet-chaudhary/9644a67952a6e7a00ed62422dcb40224 to your computer and use it in GitHub Desktop.
Save jagmeet-chaudhary/9644a67952a6e7a00ed62422dcb40224 to your computer and use it in GitHub Desktop.
Example demonstrating difference between a stable vs unstable sort.
[{"Name":"Dix","Age":27,"Designation":"Analyst","Paygrade":"Leve1","Department":"Marketing"},
{"Name":"Alphonso","Age":48,"Designation":"Director","Paygrade":"Executive","Department":"Product Management"},
{"Name":"Lester","Age":36,"Designation":"Manager","Paygrade":"Level3","Department":"Services"},
{"Name":"Lennard","Age":30,"Designation":"Director","Paygrade":"Level3","Department":"Legal"},
{"Name":"Bryn","Age":27,"Designation":"Sr Analyst","Paygrade":"Executive","Department":"Product Management"},
{"Name":"Mac","Age":40,"Designation":"Director","Paygrade":"Leve1","Department":"Training"},
{"Name":"Enid","Age":34,"Designation":"Director","Paygrade":"Level3","Department":"Services"},
{"Name":"Nanon","Age":44,"Designation":"Analyst","Paygrade":"Executive","Department":"Engineering"},
{"Name":"Booth","Age":45,"Designation":"Director","Paygrade":"Level3","Department":"Human Resources"},
{"Name":"Elsinore","Age":46,"Designation":"Manager","Paygrade":"Level2","Department":"Product Management"}]
using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
namespace SortStability
{
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Designation { get; set; }
public string Paygrade { get; set; }
public string Department { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("------------------------------------------------------------------");
sb.AppendLine($"Name : {this.Name}");
//sb.AppendLine($"Age : {this.Age}");
//sb.AppendLine($"Designation : {this.Designation}");
//sb.AppendLine($"Paygrade : {this.Paygrade}");
sb.AppendLine($"Department : {this.Department}");
return sb.ToString();
}
}
public class NameBasedCompare : IComparer<Employee>
{
public int Compare([AllowNull] Employee x, [AllowNull] Employee y)
{
return string.Compare(x.Name, y.Name);
}
}
public class DepartmentBasedCompare : IComparer<Employee>
{
public int Compare([AllowNull] Employee x, [AllowNull] Employee y)
{
//return string.Compare(x.Department, y.Department) == 0 ? x.Age.CompareTo(y.Age) : 0;
return string.Compare(x.Department, y.Department );
}
}
class AgeBasedCompare : IComparer<Employee>
{
public int Compare([AllowNull] Employee x, [AllowNull] Employee y)
{
return x.Age.CompareTo(y.Age);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1. Stable sort 2. Unstable sort");
var choice = Console.ReadLine();
var employees = JsonConvert.DeserializeObject<Employee[]>(File.ReadAllText("Employee data.json"));
Console.WriteLine("***********INPUT LIST***********");
PrintEmployees(employees);
Employee[] sortedByDepartment;
Employee[] sortedByAge;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("------------------------------------------------------------------");
Console.WriteLine("***********SORTING BY NAME***********");
if (choice == "1")
{
sortedByAge = StableSort(employees, new NameBasedCompare());
}
else
{
sortedByAge = UnstableSort(employees, new NameBasedCompare());
}
PrintEmployees(sortedByAge);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("------------------------------------------------------------------");
Console.WriteLine("***********SORTING BY DEPARTMENT***********"); ;
if (choice == "1")
{
sortedByDepartment = StableSort(sortedByAge, new DepartmentBasedCompare());
}
else
{
sortedByDepartment = UnstableSort(sortedByAge, new DepartmentBasedCompare());
}
PrintEmployees(sortedByDepartment);
}
static void PrintEmployees(Employee[] employees)
{
foreach(var employee in employees)
{
Console.Write(employee);
}
}
static Employee[] UnstableSort(Employee[] employees,IComparer<Employee> comparer)
{
//Array.Sort(employees, comparer);
Sorter<Employee>.QuickSort(employees, 0, employees.Length - 1, comparer);
return employees;
}
static Employee[] StableSort(Employee[] employees, IComparer<Employee> comparer)
{
//return employees.OrderBy(x => x, comparer).ToArray();
Sorter<Employee>.MergeSort(employees, new Employee[employees.Length], 0,employees.Length -1, comparer);
return employees;
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SortStability
{
public class Sorter<T>
{
public static void QuickSort(T[] arr,int low ,int high,IComparer<T> comparer)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
var pi = Partition(arr, low, high,comparer);
// Recursively sort elements before
// partition and after partition
QuickSort(arr, low, pi - 1,comparer);
QuickSort(arr, pi + 1, high, comparer);
}
}
static int Partition(T[] arr, int low,int high,IComparer<T> comparer)
{
T pivot = arr[high];
// index of smaller element
int i = (low - 1);
for (int j = low; j < high; j++)
{
// If current element is smaller
// than the pivot
if (comparer.Compare(arr[j],pivot) < 0)
{
i++;
// swap arr[i] and arr[j]
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// swap arr[i+1] and arr[high] (or pivot)
var temp1 = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp1;
return i + 1;
}
public static void MergeSort(T[] arr, T[] aux, int lo, int hi,IComparer<T> comparer)
{
if (lo >= hi) return;
int mid = lo + (hi - lo) / 2;
MergeSort(arr, aux, lo, mid, comparer);
MergeSort(arr, aux, mid + 1, hi, comparer);
Merge(arr, aux, lo, mid, hi, comparer);
}
static void Merge(T[] arr, T[] aux, int lo, int mid, int hi,IComparer<T> comparer)
{
//copy the items into auxiliary array
for (int i = lo; i <= hi; i++)
{
aux[i] = arr[i];
}
int firstArrayIndex = lo;
int secondArrayIndex = mid + 1;
for (int k = lo; k <= hi; k++)
{
if (firstArrayIndex > mid) arr[k] = aux[secondArrayIndex++];
else if (secondArrayIndex > hi) arr[k] = aux[firstArrayIndex++];
else if (comparer.Compare(aux[firstArrayIndex],aux[secondArrayIndex]) <= 0) arr[k] = aux[firstArrayIndex++];
else arr[k] = aux[secondArrayIndex++];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment