Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void extreme_to_middle(int[] a, int n)
  11.         {
  12.             if (n == 1)
  13.             {
  14.                 Console.Write(a[0]);
  15.                 return;
  16.             }
  17.             Console.Write(a[0] + ", " + a[n - 1] + " ,");
  18.             int[] b = new int[a.Length-1];
  19.             for(int i = 1;i < a.Length;i++)
  20.                 b[i-1] = a[i];
  21.             extreme_to_middle(b, n - 2);
  22.             return;
  23.         }
  24.         static int bcd(int a, int b)
  25.         {
  26.             if (b == 0) return a;
  27.             return bcd(b, a % b);
  28.         }
  29.         static void rec_sort(int[] arr,int n)
  30.         {
  31.             if (n+1 >= arr.Length) return;
  32.             if (arr[n] > arr[n + 1])
  33.             {
  34.                 int tmp = arr[n + 1];
  35.                 arr[n + 1] = arr[n];
  36.                 arr[n] = tmp;
  37.                 rec_sort(arr, n-1);
  38.             }
  39.             else
  40.                 rec_sort(arr, n+1);
  41.         }
  42.         static void print(int[] arr,int n)
  43.         {
  44.             if (arr.Length == n + 1) Console.Write(arr[n]);
  45.             else
  46.             {
  47.                 Console.Write(arr[n] + ", ");
  48.                 print(arr, n + 1);
  49.             }
  50.         }
  51.         static bool find(int[] arr, int num, int left, int right)
  52.         //          left = 0;
  53.         //          right = arr.Length;
  54.         {
  55.             if (arr[right] < num || arr[left] > num || left == right) return false;
  56.             int mid = (left+right)/2+1;
  57.             if (arr[mid] < num)
  58.             {
  59.                 if (mid == left) return false;
  60.                 left = mid;
  61.             }
  62.             else if (arr[mid] > num)
  63.             {
  64.                 if (mid == right) return false;
  65.                 right = mid;
  66.             }
  67.             else
  68.                 return true;
  69.             return find(arr, num, left, right);
  70.         }
  71.         static void Main(string[] args)
  72.         {
  73.             int[] a = new int[10];
  74.             a[0] = 1;
  75.             a[1] = 9;
  76.             a[2] = 8;
  77.             a[3] = 3;
  78.             a[4] = 2;
  79.             a[5] = 7;
  80.             a[6] = 6;
  81.             a[7] = 4;
  82.             a[8] = 7;
  83.             a[9] = 7;
  84.             print(a,0);
  85.             Console.WriteLine();
  86.             rec_sort(a,0);
  87.             print(a,0);
  88.             Console.WriteLine();
  89.             int n = 5;
  90.             Console.WriteLine("Does " + n + " exist in the array? " + find(a,n,0,a.Length-1));
  91.             Console.ReadLine();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement