AvengersAssemble

SortMerge19-02-14

Feb 19th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Sort
  7. {
  8.     class Program
  9.     {
  10.         public static void AddToSortedArray(int[] a, int n)
  11.         {
  12.             int i = 0;
  13.             int sorted = 0;
  14.             for (int j = 0; j < a.Length - 1; j++)
  15.                 if (a[i + 1] > a[i])
  16.                     sorted++;
  17.             while (i < sorted && a[i] < n)
  18.             {
  19.                 for (int j = sorted; j > i; j--)
  20.                     a[j] = a[j - 1];
  21.                 i++;
  22.             }
  23.             a[i] = n;
  24.         }
  25.         public static void CommonCells(int[] a, int[] b)
  26.         {
  27.             int[] c = new int[a.Length + b.Length];
  28.             for (int i = 0; i < a.Length; i++)
  29.                 c[i] = a[i];
  30.             for(int i = a.Length; i < c.Length; i++)
  31.                 c[i] = 0;
  32.             for (int i = 0; i < b.Length; i++)
  33.                 AddToSortedArray(c, b[i]);
  34.             foreach (int i in c)
  35.                 Console.Write(i + " ");
  36.             Console.WriteLine();
  37.  
  38.         }
  39.         public static bool RowNColumnSort(int[,] arr)
  40.         {
  41.             for (int i = 0; i < arr.GetLength(0) - 1; i++)
  42.             {
  43.                 for (int k = 0; k < arr.GetLength(1) - 1; k++)
  44.                 {
  45.                     if (arr[i, k] > arr[i, k + 1])
  46.                         return false;
  47.                     if (arr[i, k] > arr[i + 1, k])
  48.                         return false;
  49.                 }
  50.             }
  51.             return true;
  52.         }
  53.         public static bool DiagonalSort(int[,] arr)
  54.         {
  55.             for (int i = 0; i < arr.GetLength(0) - 1; i++)
  56.             {
  57.                 if (arr[i, i] > arr[i + 1, i + 1])
  58.                     return false;
  59.                 if (arr[i, arr.GetLength(0) - i - 1] > arr[i, arr.GetLength(0) - i])
  60.                     return false;
  61.             }
  62.             return true;
  63.         }
  64.         public static bool AbsoluteSort(int[,] arr)
  65.         {
  66.             return RowNColumnSort(arr) && DiagonalSort(arr);
  67.         }
  68.         public static void BubbleSort(int[] arr)
  69.         {
  70.             int k, i = 0, temp;
  71.             bool isSorted = false;
  72.             while (!isSorted)
  73.             {
  74.                 k = 0;
  75.                 isSorted = true;
  76.                 while (k < arr.Length - i - 1)
  77.                 {
  78.                     if (arr[k] > arr[k + 1])
  79.                     {
  80.                         temp = arr[k + 1];
  81.                         arr[k + 1] = arr[k];
  82.                         arr[k] = temp;
  83.                         isSorted = false;
  84.                         foreach (int m in arr)
  85.                             Console.Write(m + " ");
  86.                         Console.WriteLine();
  87.                     }
  88.                     k++;
  89.                 }
  90.                 i++;
  91.             }
  92.         }
  93.         static void Main(string[] args)
  94.         {
  95.             Random rnd = new Random();
  96.             //int[] arr = new int[10];
  97.             //for (int i = 0; i < arr.Length; i++)
  98.             //    arr[i] = rnd.Next(0, arr.Length * 4);
  99.             //foreach (int i in arr)
  100.             //    Console.Write(i + " ");
  101.             //Console.WriteLine();
  102.             //BubbleSort(arr);
  103.             //foreach (int i in arr)
  104.             //    Console.Write(i + " ");
  105.             //Console.WriteLine();
  106.             int[] a = new int[5];
  107.             int[] b = new int[5];
  108.             for (int i = 0; i < a.Length; i++)
  109.             {
  110.                 a[i] = rnd.Next(1, 7);
  111.                 b[i] = rnd.Next(1, 7);
  112.             }
  113.             CommonCells(a, b);
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment