Stanford: Algorithms: Design and Analysis, Part 1 [Week 2]

问题的答案第一个是随机的

第二个是
Θ(log(b))

第三个是



1-2*a

第四个是


第五个是


下面是关于程序;



Your task is to compute the total number of comparisons used to sort the given input file by QuickSort. As you know, the number of comparisons depends on which elements are chosen as pivots, so we'll ask you to explore three different pivoting rules.
You should not count comparisons one-by-one. Rather, when there is a recursive call on a subarray of length m, you should simply add m−1 to your running total of comparisons. (This is because the pivot element is compared to each of the other m−1 elements in the subarray in this recursive call.)

记住一定要把枢纽元交换成第一个元素

第一个要求是用第一个当枢纽元


first programme:For the first part of the programming assignment, you should always use the first element of the array as the pivot element.
第二个要求是用最后一个元素当枢纽元
second programme:Compute the number of comparisons (as in Problem 1), always using the final element of the given array as the pivot element. Again, be sure to implement the Partition subroutine exactly as it is described in the video lectures. Recall from the lectures that, just before the main Partition subroutine, you should exchange the pivot element (i.e., the last element) with the first element.
第三个要求是选用第一个、中间、最后一个元素中处于中间大小的数当枢纽元,即所谓的三分中值法
third programme:Compute the number of comparisons (as in Problem 1), using the "median-of-three" pivot rule. [The primary motivation behind this rule is to do a little bit of extra work to get much better performance on input arrays that are nearly sorted or reverse sorted.] In more detail, you should choose the pivot as follows. Consider the first, middle, and final elements of the given array. (If the array has odd length it should be clear what the "middle" element is; for an array with even length 2k, use the kth element as the "middle" element. So for the array 4 5 6 7, the "middle" element is the second one ---- 5 and not 6!) Identify which of these three elements is the median (i.e., the one whose value is in between the other two), and use this as your pivot. As discussed in the first and second parts of this programming assignment, be sure to implement Partition exactly as described in the video lectures (including exchanging the pivot element with the first element just before the main Partition subroutine).


#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void swap(vector<int> &vec,int a,int b);
void QuickSort(vector<int> &,int);
int Qsort(vector<int> &vec,int left,int right);
int ChosePivot1(vector<int> &vec,int left,int right);//use the first number
int ChosePivot2(vector<int> &vec,int left,int right);//use the final number
int ChosePivot3(vector<int> &vec,int left,int right);//use Median of three Partion

int main()
{
	ifstream fin("QuickSort.txt");
	vector<int> init;
	int temp;
	
	while(fin>>temp)
	{
		init.push_back(temp);
	}
	
	int DataSize = init.size();
	cout<<"DataSize: "<<DataSize<<endl;
	
	QuickSort(init,DataSize);	
	
	return 0;
}



void QuickSort(vector<int> &vec,int size)
{
	int num;
	num = Qsort(vec,0,size-1);
	cout<<"the total number of comparisons: "<<num<<endl;
}

int Qsort(vector<int> &vec,int left,int right)
{
	int i,j;
	int result,c1,c2,c3;
	
	if((right - left) == 1)
	{
		if(vec[right] < vec[left])
			swap(vec,left,right);
		return 1;
	}
	else if(right - left < 1)
	{
		return 0;
	}
	else{
		
//	int pivot = ChosePivot1(vec,left,right);
//	int pivot = ChosePivot2(vec,left,right);
	int pivot = ChosePivot3(vec,left,right);

	i = left + 1;
	for(j = left + 1;j <= right; j++)
	{
		if(vec[j] < pivot)
		{
			swap(vec,i,j);
			i++;
		}
	}
	swap(vec[left],vec[i-1]);
	}
	
	c2 = Qsort(vec,left,i-2);
	c3 = Qsort(vec,i,right);
	
	c1 = right - left;
	
	result = c1 + c2 + c3;
	
	return result;
	
}

int ChosePivot1(vector<int> & vec,int left,int right)
{
	return vec[left];
}

int ChosePivot2(vector<int> & vec,int left,int right)
{
	swap(vec,left,right);
	return vec[left];
}

int ChosePivot3(vector<int> & vec,int left,int right)
{
	int mid = (right + left) / 2;
	int midnum;
	if(vec[left] > vec[right] && vec[right] > vec[mid]||vec[left] < vec[right] && vec[right] < vec[mid])
		midnum = right;
	if(vec[right] > vec[mid] && vec[mid] > vec[left]||vec[right] < vec[mid] && vec[mid] < vec[left])
		midnum = mid;
	if(vec[mid] > vec[left] && vec[left] > vec[right]||vec[mid] < vec[left] && vec[left] < vec[right])
		midnum = left;
	
	swap(vec,left,midnum);
		
	return vec[left];
}



void swap(vector<int> &vec,int a,int b)
{
	int temp;
	temp = vec[a];
	vec[a] = vec[b];
	vec[b] = temp;
} 
这段程序写了很长时间,因为对语言不熟再加上对算法了解也不够深入吧,以后课一定要认真上了。

两天将近10小时。。才写100多行。。我也是醉了。。不过我相信每一次都是进步,加油加油加油!!!

Θ下面是


12α
Θ ( log ( b ) )
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值