您的位置:首页 > 编程语言 > Go语言

Stanford Algorithms: Design and Analysis, Part 1[week 1]

2013-07-06 22:57 591 查看

Problem Set-1











Programming Question-1

Question 1

Download the text file here. (Right click and save link as)
This file contains all of the 100,000 integers between 1 and 100,000 (inclusive) in some order, with no integer repeated.
Your task is to compute the number of inversions in the file given, where the ith row
of the file indicates the ith entry
of an array.

Because of the large size of this array, you should implement the fast divide-and-conquer algorithm covered in the video lectures. The numeric answer for the given input file should be typed in the space below.

So if your answer is 1198233847, then just type 1198233847 in the space provided without any space / commas / any other punctuation marks. You can make up to 5 attempts, and we'll use the best one for grading.

(We do not require you to submit your code, so feel free to use any programming language you want --- just type the final numeric answer in the following space.)
[TIP: before submitting, first test the correctness of your program on some small test files or your own devising. Then post your best test cases to the discussion forums to help
your fellow students!]

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;

/**
* Count number of split inversions in an array
*
* @author anuragkapur
*/
public class CountInversions {

static int a[] = new int[100000];

/**
* Assumes the 2 parts of the array are sorted, and merges them. While merging, count split inversions
*
* @param start
* @param end
* @param leftStart
* @param leftEnd
* @param rightStart
* @param rightEnd
* @return
*/
public static long countSplitInversionsAndMerge(int start, int end, int leftStart, int leftEnd, int rightStart, int rightEnd) {
int subArray1[] = new int[leftEnd - leftStart + 1];
int subArray2[] = new int[rightEnd - rightStart + 1];

int count = 0;
for (int i = leftStart; i <= leftEnd; i++) {
subArray1[count++] = a[i];
}
count = 0;
for (int i = rightStart; i <= rightEnd; i++) {
subArray2[count++] = a[i];
}

// merge and count inversions
int leftPointer = 0, rightPointer = 0;
long inversions = 0;
for (int i = start; i <= end; i++) {
if(leftPointer >= subArray1.length) {
a[i] = subArray2[rightPointer++];

}else if(rightPointer >= subArray2.length) {
a[i] = subArray1[leftPointer++];

}else if (subArray1[leftPointer] <= subArray2[rightPointer]) {
a[i] = subArray1[leftPointer++];

}else if(subArray1[leftPointer] > subArray2[rightPointer]) {
for (int j = leftPointer; j < subArray1.length; j++) {
//System.out.println(subArray1[j] + "," + subArray2[rightPointer]);
}
a[i] = subArray2[rightPointer++];
inversions = inversions + subArray1.length - leftPointer;
}
}
return inversions;
}

/**
* Recursive method to count left and right inversions
*
* @param start
* @param end
* @return
*/
public static long countInversionsAndSort(int start, int end) {
//System.out.println("start :: " + start + " end :: " + end);
if(end - start == 1) {
// two elements in array, just sort them and return if this is an inversion
if (a[start] > a[end]) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
return 1;
}else {
return 0;
}
}else if(end == start) {
// one element in array, no sorting required, can be a left / right inversion
return 0;
}else {
int leftStart = start;
int leftEnd = ((end - start) / 2 ) + start;
int rightStart = ((end - start) / 2 ) + start +1;
int rightEnd = end;
long leftInversions = countInversionsAndSort(leftStart, leftEnd);
long rightInversions = countInversionsAndSort(rightStart, rightEnd);
long splitInversions = countSplitInversionsAndMerge(start, end, leftStart, leftEnd, rightStart, rightEnd);

return leftInversions + rightInversions + splitInversions;
}
}

/**
* @param args
*/
public static void main(String[] args) {

File inputFile = new File("E:\\IntegerArray.txt");
String inputFilePath = inputFile.getAbsolutePath();

// read and parse input file
try {
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);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(countInversionsAndSort(0, a.length-1));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: