Skip to main content

Posts

Showing posts from 2020

Quicksort in C#

using System; public class Test { public static void Main() { var unsorted = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(unsorted,x=>int.Parse(x)); int n= a.Length; Quicksort(a,0,n-1); for(int i=0;i<n;i++) { Console.WriteLine(a[i]); } } public static void Quicksort(int[] a,int low,int high) { if(low<high) { int pi= Partition(a,low,high); Quicksort(a,low,pi-1); Quicksort(a,pi+1,high); } } public static int Partition(int[]a,int low,int high) { int pivot = a[high]; int i = low-1; for(int j=low;j<=high-1;j++) { if(a[j]<pivot) { i++; int temp=a[i]; a[i]=a[j]; a[j]=temp; } } int temp2 = a[high]; a[high]=a[i+1]; a[i+1]=temp2; return i+1; } }

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);         }         } ...

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); } } }

FIND THE NO OF SUBSTRINGS DIVISIBLE BY 7 --> INPUT -3567 OUTPUT-4 -->(35,56,567,7)

/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner input = new Scanner(System.in); String number = input.nextLine(); String sub; int length=number.length(); int i=0; for(int first=0;first<length;first++) { for(int second=first+1;second<=length;second++) { sub = number.substring(first,second); int a = Integer.parseInt(sub); if((a%7)==0) { i++; } } } System.out.println(i); } }

SWAP EACH CONSECUTIVE PAIRS UNTIL THERE IS NO PAIR and THEN REVERSE THE VOWELS ---> INPUT:-NAGIEM OUTPUT:-ENIGMA

/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*;a /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner input = new Scanner(System.in); String s = input.nextLine(); char[] sCharArray = s.toCharArray(); char[] newWordArray = new char[sCharArray.length]; char temp; int firstletter=0; int secondletter=1; int length = sCharArray.length; for(int i=0,n=length/2;i<n;i++) { temp= sCharArray[firstletter]; sCharArray[firstletter]=sCharArray[secondletter]; sCharArray[secondletter]=temp; firstletter=firstletter+2; secondletter=secondletter+2; } String news = new String(sCharArray); System.out.println(news); int j=0; for(int i=0,n=sCharArray.length;i<n;i++) { if(Character.toLowerCase(sCharArray[i])=='a'||Character.toLowe...