我认为这将是非常重要的,但是因为我已经完成了任何java,所以它已经有一段时间了。我的合并排序和快速排序算法(课程分配)不起作用!数组保持与调用方法之前的顺序相同。任何帮助表示赞赏!
import java.io.IOException;
public class Main {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//
Lab1Methods lab1 = new Lab1Methods();
lab1.fillArray();
lab1.testProblem1(lab1.integers);
lab1.auxMergeSort(lab1.integers, lab1.startIndex, lab1.endIndex);
lab1.testProblem1(lab1.integers);
lab1.auxQuickSort(lab1.integers, lab1.startIndex, lab1.endIndex);
lab1.testProblem1(lab1.integers);
}
}
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Lab1Methods {
//**************************************************fields
int[] integers;
int startIndex;
int endIndex;
//**************************************************methods
//****************************************constructors
public Lab1Methods(){
integers = new int [10000000];
startIndex = integers[0];
endIndex = integers[9999999];
}
//****************************************1***********************
public void fillArray() throws IOException{
FileReader file = new FileReader("lab1_data.txt");
int i = 0;
try {
Scanner input = new Scanner(file);
while(input.hasNext())
{
integers[i] = input.nextInt();
i++;
}
input.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//**********test the first problem**********
public void testProblem1(int[] array){
int [] testIntegers = array;
System.out.println("The first ten integers: ");
for(int j = 0;j<10;j++)
{
System.out.println(testIntegers[j]);
}
int sum = 0;
System.out.println("Sum of all 10,000,000 integers: ");
for(int k = 0;k <= testIntegers.length; k++)
{
sum += k;
}
System.out.println(sum);
}
//****************************************2****************************
public void DoMerge(int [] numbers, int left, int mid, int right)
{
int [] temp = new int[25];
int i, left_end, num_elements, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_elements = (right - left + 1);
while ((left <= left_end) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
temp[tmp_pos++] = numbers[left++];
else
temp[tmp_pos++] = numbers[mid++];
}
while (left <= left_end)
temp[tmp_pos++] = numbers[left++];
while (mid <= right)
temp[tmp_pos++] = numbers[mid++];
for (i = 0; i < num_elements; i++)
{
numbers[right] = temp[right];
right--;
}
}
public void auxMergeSort(int[] array, int left, int right){
int mid;
if (right > left){
mid = (right + left) / 2;
auxMergeSort(array, left, mid);
auxMergeSort(array, (mid + 1), right);
DoMerge(array, left, (mid+1), right);
}
}
//**********************************3************************
public void auxQuickSort(int [] arr, int startIndex, int endIndex){
// For Recusrion
if(startIndex < endIndex)
{
int pivot = Partition(arr, startIndex, endIndex);
if(pivot > 1)
auxQuickSort(arr, startIndex, pivot - 1);
if(pivot + 1 < endIndex)
auxQuickSort(arr, pivot + 1, endIndex);
}
}
public int Partition(int[] numbers, int startIndex, int endIndex){
int pivot = numbers[startIndex];
while (true){
while (numbers[startIndex] < pivot)
startIndex++;
while (numbers[endIndex] > pivot)
endIndex--;
if (startIndex < endIndex)
{
int temp = numbers[endIndex];
numbers[endIndex] = numbers[startIndex];
numbers[startIndex] = temp;
}
else
{
return endIndex;
}
}
}