Python3
I am having trouble with my indentation while running this program. Thanks for the help.
#4-way mergeSort
def merge(A, l,m2,m1, m3, h):
n1 = m2 - l + 1#calculating length of subarrays
n2 = m1- m2
n3 = m3-m1
n4 = h-m3
Ln = m1-l+1
Rn = h-m1
# creating temporary arrays
a = [0] * (n1)
b = [0] * (n2)
c = [0] * (n3)
d = [0] * (n4)
L = [0] * (Ln)
R = [0] * (Rn)
# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
a[i] = A[l + i]
for i in range(0 , n2):
b[i] = A[m2+1 + i]
for i in range(0 , n3):
c[i] = A[m1+1 + i]
for i in range(0 , n4):
d[i] = A[m3+1 + i]
i1=0
i2=0
k=0
while i1 < n1 and i2 < n2 :#merging first two part
if a[i1] <= b[i2]:
L[k] = a[i1]
i1 += 1
else:
L[k] = b[i2]
i2 += 1
k += 1
while i1 < n1:
L[k] = a[i1]
i1 += 1
k += 1
while i2 < n2:
L[k] = b[i2]
i2 += 1
k += 1
i1=0
i2=0
k=0
while i1 < n3 and i2 < n4 :
if c[i1] <= d[i2]:
R[k] = c[i1]
i1 += 1
else:
R[k] = d[i2]
i2 += 1
k += 1
while i1 < n3:
R[k] = c[i1]
i1 += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while i2 < n4:
R[k] = d[i2]
i2 += 1
k += 1
i=0
j=0
k=l
while i < Ln and j < Rn :
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
k += 1
while i < Ln:
A[k] = L[i]
i += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while j < Rn:
A[k] = R[j]
j += 1
k += 1
def mergeSort(A,low,high):
if low < high:
m1 = int((low+(high))/2)
m2 = int((low+m1)/2)
m3 = int(((m1+1)+high)/2)
mergeSort(A, low, m2)
mergeSort(A, m2+1, m1)
mergeSort(A, m1+1, m3)
mergeSort(A, m3+1, high)
merge(A, low,m2, m1,m3, high)
# New Code
# Creating a new list for input
A = []
# Read input from the file
with open('input.txt','r') as inp_f:
for line in inp_f:
for word in line.split():
A.append(int(word))
# Setting array size i.e. the first value of input in the file
arr_size = A[0]
# Creating a new array by removing first value of size.
A = A[1:]
mergeSort(A,0,arr_size-1)
print ("Your sorted result is stored in output.txt")
# Writing to the file.
with open('output.txt', 'w') as out_f:
for item in A:
out_f.write("%s " % item)
def merge(A, l,m2,m1, m3, h):
n1 = m2 - l + 1#calculating length of subarrays
n2 = m1- m2
n3 = m3-m1
n4 = h-m3
Ln = m1-l+1
Rn = h-m1
# creating temporary arrays
a = [0] * (n1)
b = [0] * (n2)
c = [0] * (n3)
d = [0] * (n4)
L = [0] * (Ln)
R = [0] * (Rn)
# Copy data to temp arrays L[] and R[]
for i in range(0 , n1):
a[i] = A[l + i]
for i in range(0 , n2):
b[i] = A[m2+1 + i]
for i in range(0 , n3):
c[i] = A[m1+1 + i]
for i in range(0 , n4):
d[i] = A[m3+1 + i]
i1=0
i2=0
k=0
while i1 < n1 and i2 < n2 :#merging first two part
if a[i1] <= b[i2]:
L[k] = a[i1]
i1 += 1
else:
L[k] = b[i2]
i2 += 1
k += 1
while i1 < n1:
L[k] = a[i1]
i1 += 1
k += 1
while i2 < n2:
L[k] = b[i2]
i2 += 1
k += 1
i1=0
i2=0
k=0
while i1 < n3 and i2 < n4 :
if c[i1] <= d[i2]:
R[k] = c[i1]
i1 += 1
else:
R[k] = d[i2]
i2 += 1
k += 1
while i1 < n3:
R[k] = c[i1]
i1 += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while i2 < n4:
R[k] = d[i2]
i2 += 1
k += 1
i=0
j=0
k=l
while i < Ln and j < Rn :
if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
k += 1
while i < Ln:
A[k] = L[i]
i += 1
k += 1
# Copy the remaining elements of R[], if there
# are any
while j < Rn:
A[k] = R[j]
j += 1
k += 1
def mergeSort(A,low,high):
if low < high:
m1 = int((low+(high))/2)
m2 = int((low+m1)/2)
m3 = int(((m1+1)+high)/2)
mergeSort(A, low, m2)
mergeSort(A, m2+1, m1)
mergeSort(A, m1+1, m3)
mergeSort(A, m3+1, high)
merge(A, low,m2, m1,m3, high)
# New Code
# Creating a new list for input
A = []
# Read input from the file
with open('input.txt','r') as inp_f:
for line in inp_f:
for word in line.split():
A.append(int(word))
# Setting array size i.e. the first value of input in the
file
arr_size = A[0]
# Creating a new array by removing first value of size.
A = A[1:]
mergeSort(A,0,arr_size-1)
print ("Your sorted result is stored in output.txt")
# Writing to the file.
with open('output.txt', 'w') as out_f:
for item in A:
out_f.write("%s " % item)


Python3 I am having trouble with my indentation while running this program. Thanks for the help....
How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private: float array[1000] ; int n = 1000; int i=0; public: void fillArray() { for(i=1;i<=n;i++) { array[i-1]= ( rand() % ( 1000) )+1; } } void arrayout () { ...
I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...
I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...
Hello, I want to check if my C++ code is correct and follows the
requeriments described thanks.
Requeriments:
Assignment Sorting
Benchmark each of the sorting methods listed below.
Insertion Sort
Bubble Sort
Selection Sort
Heap Sort.
Quick Sort.
Merge Sort.
Benchmark each of the above sorting methods for data sizes of
10000, 20000, 30000, 40000 and 50000. Display the results in a
table as shown below. The table should have rows and columns.
However, the rows and columns need not...
Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...
Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...
This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...
you will analyse two algorithms for finding the median of an array of integers. You will compare both algorithms in terms of timing, and hopefully design a hybrid algorithm that uses both, depending on input size. You will write a report describing your experiments and results. the following Java program implements two algorithms for finding the median of an array of integers. The first uses merge sort, and the other implements the recursive linear time selection algorithm, the task is...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...
C++: Need help debugging my code
I am writing this and using some other examples as reference code
while rewriting it with my own understanding of the material but am
having trouble making it finally compile. Below is a picture of the
error messages.
//main.cpp
//Semester Project
//Created by J---on 5/6/2019
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
void instructions(); //displays program details and
instructions
void openFile();
void takeInput(int*);
void switchBoard(int*);
struct price
{...