Stanford Algorithms: Design and Analysis, Part 1[week 2]
来源:互联网 发布:淘宝618大促是什么 编辑:程序博客网 时间:2024/05/17 05:18
Problem Set-2
Programming Question-2
Question 1
Download the text file here.
The file contains all of the integers between 1 and 10,000 (inclusive, with no repeats) in unsorted order. The integer in the
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
WARNING: The Partition subroutine can be implemented in several different ways, and different implementations can give you differing numbers of comparisons. For this problem, you should implement the Partition subroutine exactly as it is described in the video lectures (otherwise you might get the wrong answer).
DIRECTIONS FOR THIS PROBLEM:
For the first part of the programming assignment, you should always use the first element of the array as the pivot element.
HOW TO GIVE US YOUR ANSWER:
Type the numeric answer in the space provided.
So if your answer is 1198233847, then just type 1198233847 in the space provided without any space / commas / other punctuation marks. You have 5 attempts to get the correct answer.
(We do not require you to submit your code, so feel free to use the programming language of your choice, just type the numeric answer in the following space.)
Question 2
See the first question.
DIRECTIONS FOR THIS PROBLEM:
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.
Question 3
See the first question.
DIRECTIONS FOR THIS PROBLEM:
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
EXAMPLE: For the input array 8 2 4 5 7 1 you would consider the first (8), middle (4), and last (1) elements; since 4 is the median of the set {1,4,8}, you would use 4 as your pivot element.
SUBTLE POINT: A careful analysis would keep track of the comparisons made in identifying the median of the three candidate elements. You should NOT do this. That is, as in the previous two problems, you should simply add
import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;public class QuickSort {private static int a[] = new int[10000]; private static long comparisonsCount = 0;/** * Returns first element as pivot * * @param start * @param end * @return */public static int chooseFirstElementAsPivot(int start, int end) {return start;}/** * Returns the last element as pivot * * @param start * @param end * @return */public static int chooseLastElementAsPivot(int start, int end) {// Move pivot to start index.int temp = a[start];a[start] = a[end];a[end] = temp;return start;}/** * Returns the median of start, end and mid as pivot * * @param start * @param end * @return */public static int chooseMedianOfThreeAsPivot(int start, int end) {int pivotIndex = -1;int mid = ((end - start) / 2) + start;int b[] = new int[3];b[0] = a[start];b[1] = a[mid];b[2] = a[end];Arrays.sort(b);if (b[1] == a[start]) {pivotIndex = start;}else if(b[1] == a[mid]) {pivotIndex = mid;}else {pivotIndex = end;}// Move pivot to start index.int temp = a[start];a[start] = a[pivotIndex];a[pivotIndex] = temp;return start;}/** * Partitions the array about the pivot * * @param start * @param end * @param pivot */public static int partition(int start, int end, int pivotIndex) {// Update comparisonsCountcomparisonsCount += (long)end - start;// Partitionint i = start;int pivot = a[pivotIndex];for (int j = start; j <= end; j++) {if (a[j] < pivot) {i++;int temp = a[i];a[i] = a[j];a[j] = temp;}}// Put pivot into correct positionint temp = a[i];a[i] = pivot;a[pivotIndex] = temp;return i;}/** * Quick sort * * @param start * @param end */public static void sort(int start, int end) {if (end - start == 1) {// Base case, no recursive calls. 2 Element, just sort them.if (a[start] > a[end]) {int temp = a[start];a[start] = a[end];a[end] = temp;}// Increment comparisons count by 1 as there is 1 comparison happening here.comparisonsCount ++;}else if(end <= start) {// Base case, only one element. Nothing to do.}else {//change pivot options hereint pivotIndex = chooseMedianOfThreeAsPivot(start, end);int partitionIndex = partition(start, end, pivotIndex);sort(start, partitionIndex - 1);sort(partitionIndex + 1, end);}}/** * @param args */public static void main(String[] args) {File inputFile = new File("E:\\QuickSort.txt");String inputFilePath = inputFile.getAbsolutePath();// read and parse input filetry {String strLine = "";int count = 0;FileInputStream fstream = new FileInputStream(inputFilePath);DataInputStream in = new DataInputStream(fstream);BufferedReader br = new BufferedReader(new InputStreamReader(in));while ((strLine = br.readLine()) != null) {a[count ++] = Integer.parseInt(strLine);}br.close();in.close();fstream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}sort(0, a.length - 1);for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}System.out.println(comparisonsCount);}}
- Stanford Algorithms: Design and Analysis, Part 1[week 2]
- Stanford Algorithms: Design and Analysis, Part 2[week 1]
- Stanford: Algorithms: Design and Analysis, Part 1 [Week 2]
- Stanford Algorithms: Design and Analysis, Part 2[week 2]
- Stanford Algorithms: Design and Analysis, Part 1[week 1]
- Stanford Algorithms: Design and Analysis, Part 2[week 3]
- Stanford Algorithms: Design and Analysis, Part 2[week 4]
- Stanford Algorithms: Design and Analysis, Part 2[week 5]
- Stanford Algorithms: Design and Analysis, Part 2[week 6]
- Stanford Algorithms: Design and Analysis, Part 1[week 3]
- Stanford Algorithms: Design and Analysis, Part 1[week 4]
- Stanford Algorithms: Design and Analysis, Part 1[week 5]
- Stanford Algorithms: Design and Analysis, Part 1[week 6]
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 1 Assignment: Greedy and Prim
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 2 Assignment: QuickSort
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 6 Assignment: hash table and heap
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions
- C# 根据日期计算日期周数
- thrift使用小记
- asp生成验证码代码
- 四种实现下载功能的方法
- HorizontalScrollView去掉滚动条
- Stanford Algorithms: Design and Analysis, Part 1[week 2]
- 好的博客
- Excel异常Cannot get a text value from a numeric cell
- asp.net Microsoft.Office.Interop.Excel导出Excel
- 根据身份证号码判断性别 java
- vs制作安装包卸载问题
- HTML中A标签有4种状态
- Objective-C中的SEL .
- Android 4.2 InCallScreen UI浅析