quicksortarray.cs

来自「classic tsp analysis method with c++ !」· CS 代码 · 共 81 行

CS
81
字号
using System;

namespace HRD.Core
{
	internal class DataElement
	{
		public int index;
		public int value;
		
		public DataElement()
		{
		}
		
		public DataElement(int index, int value)
		{
			this.index = index;
			this.value = value;
		}
	}

	internal class QuickSortArray
	{
		public static DataElement[] list = new DataElement[12];

		static QuickSortArray()
		{
			for(int i = 0; i<12; i++)
				list[i] = new DataElement(0, 0);
		}

		public static void quickSort()
		{
			recQuickSort(0, list.Length - 1);
		}

		private static void recQuickSort(int first, int last)
		{
			int pivotLocation;

			if(first < last)
			{
				pivotLocation = partition(first, last);
				recQuickSort(first, pivotLocation - 1);
				recQuickSort(pivotLocation + 1, last);
			}
		}

		private static int partition(int first, int last)
		{
			DataElement pivot;

			int index, smallIndex;

			swap(first, (first + last) / 2);

			pivot = list[first];
			smallIndex = first;

			for(index = first + 1; index <= last; index++)
				if(list[index].value < pivot.value)
				{
					smallIndex++;
					swap(smallIndex, index);
				}

			swap(first, smallIndex);

			return smallIndex;
		}

		private static void swap(int first, int second)
		{
			DataElement temp;

			temp = list[first];
			list[first] = list[second];
			list[second] = temp;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?