Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can i get help with my java program. Im trying to use mergesort to sort numbers in 5 text files. MERGESORT package Project1; import java.util.ArrayList;

Can i get help with my java program. Im trying to use mergesort to sort numbers in 5 text files.

MERGESORT

package Project1;

import java.util.ArrayList;

public class Mergesort

{

int inversioncount = 0;

//merge sort

public ArrayList mergesort(ArrayList whole) {

ArrayList left = new ArrayList();

ArrayList right = new ArrayList();

int center;

if (whole.size() == 1) {

return whole;

} else {

center = whole.size()/2;

// copy the left half of whole into the left.

for (int i=0; i

left.add(whole.get(i));

}

//copy the right half of whole into the new arraylist.

for (int i=center; i

right.add(whole.get(i));

}

// Sort the left and right halves of the arraylist.

left = mergesort(left);

right = mergesort(right);

// Merge the results back together.

merge(left, right, whole);

}

return whole;

}

public void merge(ArrayList left, ArrayList right, ArrayList whole) {

int leftIndex = 0;

int rightIndex = 0;

int wholeIndex = 0;

// As long as neither the left nor the right ArrayList has

// been used up, keep taking the smaller of left.get(leftIndex)

// or right.get(rightIndex) and adding it at both.get(bothIndex).

while (leftIndex < left.size() && rightIndex < right.size()) {

if ( (left.get(leftIndex).compareTo(right.get(rightIndex))) < 0) {

whole.set(wholeIndex, left.get(leftIndex));

leftIndex++;

} else {

whole.set(wholeIndex, right.get(rightIndex));

//inversions += leftIndex.size

rightIndex++;

}

wholeIndex++;

}

ArrayList rest;

int restIndex;

if (leftIndex >= left.size()) {

// The left ArrayList has been used up...

rest = right;

restIndex = rightIndex;

} else {

// The right ArrayList has been used up...

rest = left;

restIndex = leftIndex;

}

// Copy the rest of whichever ArrayList (left or right) was not used up.

for (int i=restIndex; i

whole.set(wholeIndex, rest.get(i));

wholeIndex++;

}

}

}

RANKPACKAGES

package Project1;

import java.util.ArrayList;

public class rankpages

{

public static void main(String[] args)

{

Quicksort Quick = new Quicksort();

Mergesort Merge = new Mergesort();

Bubblesort Bubble = new Bubblesort();

//arraylist that contains websites 1-10,000

ArrayList websites = new ArrayList();

for(int i=0;i<10000;i++) {websites.add("Website "+(i+1));}

//Create variables for sources and store the information from the files

Source source1 = new Source("Source1.txt");

Source source2 = new Source("Source2.txt");

Source source3 = new Source("Source3.txt");

Source source4 = new Source("Source4.txt");

Source source5 = new Source("Source5.txt");

//Make an array that contains the sum of all sources

ArrayList sumofsources = new ArrayList();

for(int i=0;i

{

int a = source1.nums.get(i)+ source2.nums.get(i)+ source3.nums.get(i)+ source4.nums.get(i)+ source5.nums.get(i);

sumofsources.add(a);

}

//create a copy of sumofsources to sort

ArrayList sortedlist= new ArrayList(); for(int i=0;i

//Merge.mergesort(sumofsources);

//Quick.sort(sortedlist, 0, sortedlist.size()-1);

int invs = Bubble.bubbleSort(sortedlist);

//Print sumofsources before being sorted and number of inversions in list counted using the simple inversion counter

System.out.println(sumofsources);

System.out.println("Inversions counted with simple inversion count: "+ getInvCount(sumofsources));

//Print sumofsources after being sorted with bubblesort and number of inversions counted during sorting

System.out.println(sortedlist);

System.out.println("Inversions counted with bubblesort: "+invs);

}

//simple inversion counter for checking inversion count of sort methods

static int getInvCount(ArrayList arr)

{

int n = arr.size();

int inv_count = 0;

for (int i = 0; i < n - 1; i++)

for (int j = i+1; j < n; j++)

if (arr.get(i) > arr.get(j))

inv_count++;

return inv_count;

}

}

SOURCE

package Project1;

import java.util.ArrayList;

public class rankpages

{

public static void main(String[] args)

{

Quicksort Quick = new Quicksort();

Mergesort Merge = new Mergesort();

Bubblesort Bubble = new Bubblesort();

//arraylist that contains websites 1-10,000

ArrayList websites = new ArrayList();

for(int i=0;i<10000;i++) {websites.add("Website "+(i+1));}

//Create variables for sources and store the information from the files

Source source1 = new Source("Source1.txt");

Source source2 = new Source("Source2.txt");

Source source3 = new Source("Source3.txt");

Source source4 = new Source("Source4.txt");

Source source5 = new Source("Source5.txt");

//Make an array that contains the sum of all sources

ArrayList sumofsources = new ArrayList();

for(int i=0;i

{

int a = source1.nums.get(i)+ source2.nums.get(i)+ source3.nums.get(i)+ source4.nums.get(i)+ source5.nums.get(i);

sumofsources.add(a);

}

//create a copy of sumofsources to sort

ArrayList sortedlist= new ArrayList(); for(int i=0;i

//Merge.mergesort(sumofsources);

//Quick.sort(sortedlist, 0, sortedlist.size()-1);

int invs = Bubble.bubbleSort(sortedlist);

//Print sumofsources before being sorted and number of inversions in list counted using the simple inversion counter

System.out.println(sumofsources);

System.out.println("Inversions counted with simple inversion count: "+ getInvCount(sumofsources));

//Print sumofsources after being sorted with bubblesort and number of inversions counted during sorting

System.out.println(sortedlist);

System.out.println("Inversions counted with bubblesort: "+invs);

}

//simple inversion counter for checking inversion count of sort methods

static int getInvCount(ArrayList arr)

{

int n = arr.size();

int inv_count = 0;

for (int i = 0; i < n - 1; i++)

for (int j = i+1; j < n; j++)

if (arr.get(i) > arr.get(j))

inv_count++;

return inv_count;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

Writing a Strong Introduction

Answered: 1 week ago