Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

快排,不过还有好些地方要优化

Posted by 110120_119 at 2019-04-01 15:45:10 on Problem 2388
#include<cstdio>
#pragma warning (disable:4996)
using namespace std;

int n;
int arr[10010];

void quickSort(int start,int end)
{
	if (start >= end)
		return;

	int first, last, key;
	first = start;
	last = end;
	key = arr[first];

	while (first < last)
	{
		while (first < last && arr[last] >= key)
			last--;
		arr[first] = arr[last];

		while (first < last && arr[first] <= key)
			first++;
		arr[last] = arr[first];
	}
	arr[first] = key;
	quickSort(start, first - 1);
	quickSort(first + 1, end);
}

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)
			scanf("%d", &arr[i]);

		quickSort(0, n - 1);

		printf("%d\n", arr[n / 2]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator