数据结构、排序算法、快速排序

//快速排序
void quickSort(int *a, int start, int end)
{
	if (start>=end)
	{
		return;
	}
	int pivot = a[start];   //选取第一个元素为基准
	int left_index = start;
	int right_index = end;
	bool flag_left_or_right = true;   //0-表示计算坐标的,1-表示计算右边的

	while (right_index>left_index)
	{
		if (flag_left_or_right==1)
		{
			if (a[right_index] >= pivot)
			{
				right_index--;
				continue;
			}
			else
			{
				int temp = a[right_index];
				a[right_index] = a[left_index];
				a[left_index] = temp;
				left_index++;
				flag_left_or_right = false;
			}
		}
		else
		{
			if (a[left_index]<=pivot)
			{
				left_index++;
				continue;
			}
			else
			{
				int temp = a[left_index];
				a[left_index] = a[right_index];
				a[right_index] = temp;
				right_index--; 
				flag_left_or_right = true;
			}
		}
	}
	quickSort(a, start, right_index);
	quickSort(a, right_index + 1, end);

}

猜你喜欢

转载自my.oschina.net/u/3397950/blog/1791214