Skip to content

Instantly share code, notes, and snippets.

@rkttu
Created December 8, 2025 15:46
Show Gist options
  • Select an option

  • Save rkttu/719fad34908e3298f093501b6135b2e6 to your computer and use it in GitHub Desktop.

Select an option

Save rkttu/719fad34908e3298f093501b6135b2e6 to your computer and use it in GitHub Desktop.
NativeSdk Tech Demo
#:sdk NativeSdk@0.5.1
unsafe
{
const int ArraySize = 10;
// stackalloc으로 스택에 메모리 할당
int* arr = stackalloc int[ArraySize];
// 랜덤 값으로 배열 초기화
Random rand = new(42);
Console.WriteLine("Before Sort:");
for (int i = 0; i < ArraySize; i++)
{
arr[i] = rand.Next(1, 100);
Console.Write($"{arr[i]} ");
}
Console.WriteLine();
// QuickSort 실행
QuickSort(arr, 0, ArraySize - 1);
// 정렬 결과 출력
Console.WriteLine("\nAfter Sort:");
for (int i = 0; i < ArraySize; i++)
{
Console.Write($"{arr[i]} ");
}
Console.WriteLine();
static void QuickSort(int* arr, int left, int right)
{
if (left >= right) return;
int pivotIndex = Partition(arr, left, right);
QuickSort(arr, left, pivotIndex - 1);
QuickSort(arr, pivotIndex + 1, right);
}
static int Partition(int* arr, int left, int right)
{
int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++)
{
if (arr[j] <= pivot)
{
i++;
(arr[i], arr[j]) = (arr[j], arr[i]); // 튜플 스왑
}
}
(arr[i + 1], arr[right]) = (arr[right], arr[i + 1]);
return i + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment