-
-
Save unilecs/7bd37816d1b1ea9b7fee9c4fcb3cd9a4 to your computer and use it in GitHub Desktop.
Задача: сортировка слиянием без использования дополнительной памяти
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
private static void MergeSortInPlace(ref int[] A, ref int[] B) | |
{ | |
int m = A.Length; | |
int n = B.Length; | |
// Перебираем A[i]: | |
// игнорируем элемент, если он уже находится в правильном порядке. | |
// в противном случае меняем его местами со следующим меньшим элементом, | |
// который оказывается первым элементом B. | |
for (int i = 0; i < m; i++) | |
{ | |
// сравниваем текущий элемент A[i] с первым элементом массива B | |
if (A[i] > B[0]) | |
{ | |
int temp = A[i]; | |
A[i] = B[0]; | |
B[0] = temp; | |
// перемещаем B[0] в правильное положение, | |
// чтобы сохранить отсортированный порядок B[]. | |
int k; int first = B[0]; | |
for (k = 1; k < n && B[k] < first; k++) | |
{ | |
B[k - 1] = B[k]; | |
} | |
B[k - 1] = first; | |
} | |
} | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
int[] A = new int[] { 1, 4, 7, 9, 10 }; | |
int[] B = new int[] { 2, 3, 5, 8 }; | |
MergeSortInPlace(ref A, ref B); | |
Console.WriteLine(string.Format("Answer = {0}", string.Join(", ", A))); | |
Console.WriteLine(string.Format("Answer = {0}", string.Join(", ", B))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment