荷兰国旗问题(c语言实现)

  • Post author:
  • Post category:其他


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable:4996)
void print(int array[], int n)
{
	if (array == NULL)
	{
		return;
	}
	for (int i = 0; i < n; i++)
	{
		printf("%d  ",*(array+i));
	}
	printf("\n");
}
void swap(int array[], int i, int j)
{
	if (array == NULL)
	{
		return;
	}
	int temp = array[i];
	array[i] = array[j];
	array[j] = temp;
}
void partition(int array[], int L, int R,int num)
{ 
	if (array == NULL)
	{
		return;
	}
	int less = L - 1;
	int more = R + 1;
	int cur = L;
	while (cur < more)
	{
		if (array[cur] < num)
		{
			swap(array,++less,cur++);
		}
		else if (array[cur] > num)
		{
			swap(array, --more, cur);
		}
		else
		{
			cur++;
		}
	}
}
int main()
{
	int array[] = {6,9,8,7,2,3,1,4,6,5,6};
	int n = sizeof(array) / sizeof(array[0]);
	partition(array, 0, n - 1,6);
	print(array,n);
	printf("\n");
	system("pause");
	return 0;
}



版权声明:本文为NULLprointex原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。