CShap 双调排序 源代码
时间: 2023-11-09 10:34:53 浏览: 115
以下是C#语言的双调排序源代码:
using System;
public class BitonicSort
{
public static void BitonicMerge(int[] input, int low, int count, bool direction)
{
if (count > 1)
{
int k = count / 2;
for (int i = low; i < low + k; i++)
{
if ((input[i] > input[i + k]) == direction)
{
int temp = input[i];
input[i] = input[i + k];
input[i + k] = temp;
}
}
BitonicMerge(input, low, k, direction);
BitonicMerge(input, low + k, k, direction);
}
}
public static void BitonicSortRecursive(int[] input, int low, int count, bool direction)
{
if (count > 1)
{
int k = count / 2;
BitonicSortRecursive(input, low, k, true);
BitonicSortRecursive(input, low + k, k, false);
BitonicMerge(input, low, count, direction);
}
}
public static void BitonicSort(int[] input, bool direction)
{
BitonicSortRecursive(input, 0, input.Length, direction);
}
public static void Main()
{
int[] array = { 5, 2, 9, 1, 5, 6, 3 };
BitonicSort(array, true);
Console.WriteLine("Sorted Array:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
}
}
阅读全文
相关推荐















