Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Sort
- {
- class Program
- {
- public static void AddToSortedArray(int[] a, int n)
- {
- int i = 0;
- int sorted = 0;
- for (int j = 0; j < a.Length - 1; j++)
- if (a[i + 1] > a[i])
- sorted++;
- while (i < sorted && a[i] < n)
- {
- for (int j = sorted; j > i; j--)
- a[j] = a[j - 1];
- i++;
- }
- a[i] = n;
- }
- public static void CommonCells(int[] a, int[] b)
- {
- int[] c = new int[a.Length + b.Length];
- for (int i = 0; i < a.Length; i++)
- c[i] = a[i];
- for(int i = a.Length; i < c.Length; i++)
- c[i] = 0;
- for (int i = 0; i < b.Length; i++)
- AddToSortedArray(c, b[i]);
- foreach (int i in c)
- Console.Write(i + " ");
- Console.WriteLine();
- }
- public static bool RowNColumnSort(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0) - 1; i++)
- {
- for (int k = 0; k < arr.GetLength(1) - 1; k++)
- {
- if (arr[i, k] > arr[i, k + 1])
- return false;
- if (arr[i, k] > arr[i + 1, k])
- return false;
- }
- }
- return true;
- }
- public static bool DiagonalSort(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0) - 1; i++)
- {
- if (arr[i, i] > arr[i + 1, i + 1])
- return false;
- if (arr[i, arr.GetLength(0) - i - 1] > arr[i, arr.GetLength(0) - i])
- return false;
- }
- return true;
- }
- public static bool AbsoluteSort(int[,] arr)
- {
- return RowNColumnSort(arr) && DiagonalSort(arr);
- }
- public static void BubbleSort(int[] arr)
- {
- int k, i = 0, temp;
- bool isSorted = false;
- while (!isSorted)
- {
- k = 0;
- isSorted = true;
- while (k < arr.Length - i - 1)
- {
- if (arr[k] > arr[k + 1])
- {
- temp = arr[k + 1];
- arr[k + 1] = arr[k];
- arr[k] = temp;
- isSorted = false;
- foreach (int m in arr)
- Console.Write(m + " ");
- Console.WriteLine();
- }
- k++;
- }
- i++;
- }
- }
- static void Main(string[] args)
- {
- Random rnd = new Random();
- //int[] arr = new int[10];
- //for (int i = 0; i < arr.Length; i++)
- // arr[i] = rnd.Next(0, arr.Length * 4);
- //foreach (int i in arr)
- // Console.Write(i + " ");
- //Console.WriteLine();
- //BubbleSort(arr);
- //foreach (int i in arr)
- // Console.Write(i + " ");
- //Console.WriteLine();
- int[] a = new int[5];
- int[] b = new int[5];
- for (int i = 0; i < a.Length; i++)
- {
- a[i] = rnd.Next(1, 7);
- b[i] = rnd.Next(1, 7);
- }
- CommonCells(a, b);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment