Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Algorithms
- {
- class Program
- {
- static void Sort(int[] arr, int startIndex, int endIndex)
- {
- if (startIndex >= endIndex)
- {
- return;
- }
- int midIndex = (startIndex + endIndex) / 2;
- Sort(arr, startIndex, midIndex);
- Sort(arr, midIndex + 1, endIndex);
- Merge(arr, startIndex, midIndex, endIndex);
- }
- private static void Merge(int[] arr, int startIndex, int midIndex, int endIndex)
- {
- if (midIndex < 0 || midIndex + 1 >= arr.Length
- || arr[midIndex] <= arr[midIndex + 1])
- {
- return;
- }
- int[] tempArr = new int[arr.Length];
- for (int i = startIndex; i <= endIndex; i++)
- {
- tempArr[i] = arr[i];
- }
- int leftIndex = startIndex;
- int rightIndex = midIndex + 1;
- for (int i = startIndex; i <= endIndex; i++)
- {
- if (leftIndex > midIndex)
- {
- arr[i] = tempArr[rightIndex];
- rightIndex++;
- // arr[i] = tempArr[rightIndex++]; - кратък запис на горните 2
- }
- else if (rightIndex > endIndex)
- {
- arr[i] = tempArr[leftIndex++];
- }
- else if (tempArr[leftIndex] <= tempArr[rightIndex])
- {
- arr[i] = tempArr[leftIndex++];
- }
- else
- {
- arr[i] = tempArr[rightIndex++];
- }
- }
- }
- static void Main(string[] args)
- {
- var numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
- Sort (numbers, 0, numbers.Length - 1);
- Console.WriteLine(string.Join(" ", numbers));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement