Skip to main content

Posts

Showing posts from March, 2020

Merge sort in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Merge_sort {        class Program     {         static void Main(string[] args)         {         int T = Convert.ToInt32(Console.ReadLine());         while(T>0)         {         int a;             List<int> unsorted = new List<int>();             List<int> sorted;         var s = Console.ReadLine().Split(' ');         for(int i=0;i<s.Length;i++)         {         if(int.TryParse(s[i],out a))         {         unsorted.Add(a);         }         }             Console.WriteLine("Original array elements:" );             for(int i = 0; i< unsorted.Count;i++){                               Console.Write(unsorted[i]+" ");             }             Console.WriteLine();             sorted = MergeSort(unsorted);             Console.WriteLine("Sorted array elements: &q

INSERTION SORT in C#

using System; public class Test { public static void Main() { int T = Convert.ToInt32(Console.ReadLine()); for(int i=0;i<T;i++) { string[] unsorted = Console.ReadLine().Split(' '); int[] numbers = Array.ConvertAll(unsorted,int.Parse); InsertionSort(numbers); Print(numbers); } } public static void InsertionSort(int[] a) { int n= a.Length; int j=0,flag=0; for(int i=1;i<n;i++) { int key = a[i]; j=i-1; while(j>=0 && a[j]>key) { a[j+1]=a[j]; j--; } a[j+1]=key; } } public static void Print(int[] a) { foreach(int i in a) { Console.WriteLine(i); } } }

SELECTION SORT with stability

using System; public class Test { public static void Main() { string [] UnsortedNumbers = Console.ReadLine().Split(' '); int[] intUnsorted = Array.ConvertAll(UnsortedNumbers,int.Parse); SelectionSort(intUnsorted); printarr(intUnsorted); } public static void SelectionSort(int[] a) { int n= a.Length; for(int i=0;i<n-1;i++) { int min = i; for(int j=i+1;j<n;j++) { if(a[j]<a[min]) { min = j; } } int temp=a[min]; while(min>i) { a[min]=a[min-1]; min--; } a[i]=temp; } } public static void printarr(int[] array) { foreach(int a in array) { Console.WriteLine(a); } } }

SELECTION SORT in C#

using System; public class Test { public static void Main() { string [] UnsortedNumbers = Console.ReadLine().Split(' '); int[] intUnsorted = Array.ConvertAll(UnsortedNumbers,int.Parse); SelectionSort(intUnsorted); printarr(intUnsorted); } public static void SelectionSort(int[] a) { int n= a.Length; for(int i=0;i<n-1;i++) { int min = i; for(int j=i+1;j<n;j++) { if(a[j]<a[min]) { min = j; } } Console.WriteLine("min "+min+" i-"+i+"a[min] and a[i] are respectively "+a[min]+" "+a[i]); int temp=a[min]; a[min]= a[i]; a[i]=temp; Console.WriteLine("a[min] and a[i] are respectively "+a[min]+" "+a[i]); } } public static void printarr(int[] array) { foreach(int a in array) { Console.WriteLine(a); } } }

Beginner's bubble sort in C#-Optimised

using System; public class Test { public static void Main() { int T = Convert.ToInt32(Console.ReadLine()); for(int i=0;i<T;i++) { string[] unSorted = Console.ReadLine().Split(' '); int[] unSortedNumberArray = Array.ConvertAll(unSorted,int.Parse); int n = unSortedNumberArray.Length; BubbleSort(unSortedNumberArray,n); } } public static void BubbleSort(int[] numbers,int n) { bool swapped = false; for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) {  int temp=-1; if(numbers[j+1]<numbers[j]) { temp = numbers[j]; numbers[j] = numbers[j+1]; numbers[j+1] = temp; swapped = true; } } if(swapped==false) { break; } } foreach(int a in numbers ) { Console.WriteLine(a); } } }