Tutorialspoint

C# Shell Sort program

using System;

namespace ShellSortDemo {
   public class Example {
      static void shellSort(int[] arr, int n) {
         int i, j, pos, temp;

         pos = 3;

         while (pos > 0) {
            for (i = 0; i < n; i++) {
               j = i;
               temp = arr[i];
               while ((j >= pos) && (arr[j - pos] > temp)){
                  arr[j] = arr[j - pos];
                  j = j - pos;
               }
               arr[j] = temp;
            }
            if (pos / 2 != 0)
               pos = pos / 2;
            else if (pos == 1)
               pos = 0;
            else
               pos = 1;
         }
      }

      static void Main(string[] args) {
         int[] arr = new int[] { 56, 12, 99, 32, 1, 95, 25, 5, 100, 84 };
         int n = arr.Length;
         int i;

         Console.WriteLine("Shell Sort");
         Console.Write("Initial array is: ");

         for (i = 0; i < n; i++){
            Console.Write(arr[i] + " ");
         }
         
         shellSort(arr, n); 
         Console.Write("\nSorted Array is: ");
         for (i = 0; i < n; i++){
            Console.Write(arr[i] + " "); 
         }

      }
   }
}

C# Bitwise Operators

using System;
 
namespace BitwiseDemo {
   class Example {
      static void Main(string[] args) {
         int num = 25, result;   
         result = num << 1;
         Console.WriteLine("The original number is: {0}", num);
         Console.WriteLine("The number multiplied by two is: {0}", result);
      }
   }
}

C# Merge Sort

using System;

namespace QuickSortDemo {
   class Example {
      static public void merge(int[] arr, int p, int q, int r) {
         int i, j, k;
         int n1 = q - p + 1;
         int n2 = r - q;
         int[] L = new int[n1];
         int[] R = new int[n2];
         for (i = 0; i < n1; i++) {
            L[i] = arr[p + i];
         }       
         for (j = 0; j < n2; j++) {
            R[j] = arr[q + 1 + j];
         }
         i = 0;
         j = 0;
         k = p;   
         while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
               arr[k] = L[i];
               i++;
            } else {
               arr[k] = R[j];
               j++;
            }
            k++;
         } 
         while (i < n1){
            arr[k] = L[i];
            i++;
            k++;
         }
         while (j < n2){
            arr[k] = R[j];
            j++;
            k++;
         }
      }
      static public void mergeSort(int[] arr, int p, int r) {
         if (p < r) {
            int q = (p + r) / 2;
            mergeSort(arr, p, q);
            mergeSort(arr, q + 1, r);
            merge(arr, p, q, r);
         }
      }
      static void Main(string[] args) {
         int[] arr = {76, 89, 23, 1, 55, 78, 99, 12, 65, 100}; 
         int n = 10, i;   
         Console.WriteLine("Merge Sort");
         Console.Write("Initial array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
         }
         mergeSort(arr, 0, n-1); 
         Console.Write("\nSorted Array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " "); 
         }
      }
   }
}

C# program to multiply two matrices

using System;
 
namespace MatrixMultiplicationDemo {
   class Example {
      static void Main(string[] args) {
         int m = 2, n = 3, p = 3, q = 3, i, j;   
         int[,] a = {{1, 4, 2}, {2, 5, 1}};
         int[,] b = {{3, 4, 2}, {3, 5, 7}, {1, 2, 1}};
         Console.WriteLine("Matrix a:");   
         for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
               Console.Write(a[i, j] + " ");
            }
            Console.WriteLine();
         }
         Console.WriteLine("Matrix b:");
         for (i = 0; i < p; i++) {
            for (j = 0; j < q; j++) {
               Console.Write(b[i, j] + " ");
            }
            Console.WriteLine();
         }
         if(n!=p) {
            Console.WriteLine("Matrix multiplication not possible");
         } else{   
            int[,] c = new int[m, q];
            for (i = 0; i < m; i++) {
               for (j = 0; j < q; j++) {
                  c[i, j] = 0;
                  for (int k = 0; k < n; k++) {
                     c[i, j] += a[i, k] * b[k, j];
                  }
               }
            }
            Console.WriteLine("The product of the two matrices is :");
            for (i = 0; i < m; i++) {
               for (j = 0; j < n; j++) {
                  Console.Write(c[i, j] + "\t");
               }
               Console.WriteLine();
            }
         }
      }
   }
}

C# Quicksort

using System;

namespace QuickSortDemo {
   class Example {
      static public int Partition(int[] arr, int left, int right) {
         int pivot; 
         pivot = arr[left];
         while (true) {
            while (arr[left] < pivot) {
               left++;
            }    
            while (arr[right] > pivot) {
               right--;
            }    
            if (left < right) {
               int temp = arr[right];
               arr[right] = arr[left];
               arr[left] = temp;
            } else {
               return right;
            }
         }

      }
      static public void quickSort(int[] arr, int left, int right) {   
         int pivot;   
         if (left < right) {
            pivot = Partition(arr, left, right);
            if (pivot > 1) {
               quickSort(arr, left, pivot - 1);
            }
            if (pivot + 1 < right) {
               quickSort(arr, pivot + 1, right);
            }
         }
      }
      static void Main(string[] args) {
         int[] arr = {67, 12, 95, 56, 85, 1, 100, 23, 60, 9}; 
         int n = 10, i;   
         Console.WriteLine("Quick Sort");
         Console.Write("Initial array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
         }
         quickSort(arr, 0, 9); 
         Console.Write("\nSorted Array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " "); 
         }
      }
   }
}

C# Validate IP Address

using System;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;

namespace IPaddressDemo {
   class Example {
      public static void Main() {
         IPAddress IP;  
         Console.WriteLine("Enter the IP Address: ");  
         string ipAddr = Console.ReadLine();  
         bool flag = IPAddress.TryParse(ipAddr, out IP);  
         if (flag)  
            Console.WriteLine("{0} is a valid IP address", ipAddr);  
         else  
            Console.WriteLine("{0} is not a valid IP address", ipAddr);  
      }
   }
}

C# Program to create Pascal’s Triangle

using System;

namespace PascalTriangleDemo {
   class Example {
      public static void Main() {
         int rows = 5, val = 1, blank, i, j;
         Console.WriteLine("Pascal's triangle");  	
         for(i=0; i<rows; i++) {
            for(blank = 1; blank <= rows-i; blank++)
            Console.Write("  ");
            for(j = 0; j <= i; j++) {
               if (j==0||i==0)
                  val = 1; 
               else
                  val = val*(i-j+1)/j;
               Console.Write(val + "   ");
            }
            Console.WriteLine();
         }
      }
   }
}

C# program to implement FizzBuzz

using System; 

namespace FizzBuzzDemo {
   public class example { 
      static void Main(string[] args) {
         for (int i = 1; i <= 100; i++) {  
            if (i % 3 == 0 && i % 5 == 0) {  
               Console.WriteLine("FizzBuzz");  
            }    
            else if (i % 3 == 0) {  
               Console.WriteLine("Fizz");  
            } else if (i % 5 == 0)  {  
               Console.WriteLine("Buzz");  
            } else {  
               Console.WriteLine(i);  
            }  
         }  
      } 
   }
} 

C# Heap Sort

using System; 

namespace HeapSortDemo {
   public class example {     
      static void heapSort(int[] arr, int n) { 
         for (int i = n / 2 - 1; i >= 0; i--) 
            heapify(arr, n, i); 
         for (int i = n-1; i>=0; i--) { 
            int temp = arr[0]; 
            arr[0] = arr[i]; 
            arr[i] = temp; 
            heapify(arr, i, 0); 
         } 
      } 
      static void heapify(int[] arr, int n, int i) { 
         int largest = i; 
         int left = 2*i + 1;
         int right = 2*i + 2; 
         if (left < n && arr[left] > arr[largest]) 
         largest = left; 
         if (right < n && arr[right] >    arr[largest]) 
            largest = right; 
            if (largest != i) { 
               int swap = arr[i]; 
               arr[i] = arr[largest]; 
               arr[largest] = swap; 
               heapify(arr, n, largest); 
            } 
      } 
      public static void Main() { 
         int[] arr = {55, 25, 89, 34, 12, 19, 78, 95, 1, 100}; 
         int n = 10, i;  
         Console.WriteLine("Heap Sort");
         Console.Write("Initial array is: ");      
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
         }
         heapSort(arr, 10); 
         Console.Write("\nSorted Array is: ");      
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " "); 
         }
      } 
   }
} 

C# Insertion Sort

using System;

namespace InsertionSortDemo {
   class Example {
      static void Main(string[] args) {
         int[] arr = new int[10] { 23, 9, 85, 12, 99, 34, 60, 15, 100, 1 };
         int n = 10, i, j, val, flag; 
         Console.WriteLine("Insertion Sort");
         Console.Write("Initial array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
         }   
         for (i = 1; i < n; i++) {
            val = arr[i];
            flag = 0;    
            for (j = i - 1; j >= 0 && flag != 1; ){
               if (val < arr[j]) {
                  arr[j + 1] = arr[j];
                  j--;
                  arr[j + 1] = val;
               }
               else flag = 1;
            }
         }    
         Console.Write("\nSorted Array is: ");   
         for (i = 0; i < n; i++) {
            Console.Write(arr[i] + " "); 
         }          
      }        
   }   
}

1 2 3 4 5 6 7 ... 117 Next
Advertisements
Loading...