|
Hello,
I am studying this merge sort algorithm and a mystery still going on.
this merge sort algorithm :
class MergeSort
{
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("Given Array");
printArray(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length-1);
System.out.println("\nSorted array");
printArray(arr);
}
}
The problem is ubicating on the Main function that sorts arr[l..r] using merge() , here :
void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
}
}
It's impossible to understand where is defined the value of "int l" and "int r" ?
Is it a question impossible to answer or will take time ?
Thanks anyway,
Best regard,
Intelego
|
|
|
|
|
Member 14084068 wrote: It's impossible to understand where is defined the value of "int l" and "int r" ? They are the parameters to the sort method and are defined at the call:
ob.sort(arr, 0, arr.length-1);
|
|
|
|
|
Thanks,
of course, I was sure it was simple.
Just last question maybe easier :
In this part of the code :
void sort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort(arr, l, m);
sort(arr , m+1, r);
merge(arr, l, m, r);
I wonder if the result of (l+r)/2 is not an integer number (by example: 10,5), then is not it possible the machine become stuck?
I know I am wrong but what I ask is how what I said is made impossible inner the code?
in another way what the code have invented to prevent this?
Thanks very much,
Intelego
|
|
|
|
|
int m = (l+r)/2;
The variables m , l and r are all declared as integer types. So the result of any calculations with their values will always be integers. In the case of division, if there is a remainder then that will be discarded. For example the result of 7 divided by 2 will always be 3.
|
|
|
|
|
|
Quote: I am using BufferedReader for reading a response from the server.
while((String line = br.readLine()) != null)
System.out.println(line);
Above is my code. When I run I am getting only few lines but not all the lines.
If br completes reading a line then it should return null and come out of the loop. That is also not happening. It is just waiting for the line to read.
Any idea why this is happening ?
|
|
|
|
|
GiteHrudaya wrote: Any idea why this is happening ? Because that is how networking works. Data is sent in packets which have nothing to do with "lines", it is just packets of bytes. You may get a complete message in a packet, but most of the time you will just get part of it, or part of some and all of another. So if you are trying to read a line at a time the receiver must wait until it receives the line end character(s). Until then it is in a wait condition.
|
|
|
|
|
GiteHrudaya wrote: If br completes reading a line then it should return null and come out of the loop.
Perhaps you misstated something you already know but that statement is not true.
It will exit the loop when the stream is done. Since it is not exiting the loop that means your stream is not done.
Or in other words you problem is not in that loop but rather how you are getting the stream in the first place.
I would suggest, unless you have a real reason otherwise that you should get the entire result first, probably buffered as a memory/byte stream, and then pass it to your line reading loop.
|
|
|
|
|
Please Guys i need some help for my Java GUI Project can u message me if u want to help this poor guy
|
|
|
|
|
|
|
Hello,
I am trying to connect to mysql from Java with the following but getting this error
Exception in thread "main" Communications link failure
package testPackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=true","root","success");
System.out.print("Database is connected !");
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
throw e;
}
}
}
|
|
|
|
|
That message most likely means that JDBC cannot connect through port 3306, the mysql driver is not listening there, or some part of that path is not valid.
|
|
|
|
|
1
modified 9-Dec-18 15:29pm.
|
|
|
|
|
|
Hi,
I'll start with the code for both server and client, it is just a simple example:
Server
serverSocket = new ServerSocket(4554);
serverRunning = true;
while (serverRunning) {
socket = serverSocket.accept();
ObjectOutputStream dataOut = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream dataIn = new ObjectInputStream(socket.getInputStream());
System.out.println("Done 1");
String string = dataIn.readUTF();
System.out.println("Done 2");
int number = dataIn.readInt();
System.out.println("Done 3");
dataOut.writeUTF("String from server");
System.out.println("Done 4");
dataOut.writeInt(1);
System.out.println("Done 5");
System.out.println(string + " | " + number);
}
Client
Socket socket = new Socket("localhost", 4554);
ObjectOutputStream dataOut = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream dataIn = new ObjectInputStream(socket.getInputStream());
System.out.println("Done 1");
dataOut.writeUTF("String from client");
System.out.println("Done 2");
dataOut.writeInt(2);
System.out.println("Done 3");
String string = dataIn.readUTF();
System.out.println("Done 4");
int number = dataIn.readInt();
System.out.println("Done 5");
socket.close();
System.out.println(string + " | " + number);
According to java Object Serialization, you need to create the ObjectOutputStream before ObjectInputStream in both Server and Client side, which I did, but the Client will run until it gets to the point of having to wait for data from the server (print the message "Done 3" then wait), but on the Server side, the code only runs until it gets to the point of reading the first thing from the Client (print "Done 1" then wait).
What else I need to do, beside creating the ObjectOutputStream before ObjectInputStream?
modified 4-Dec-18 10:49am.
|
|
|
|
|
how to trace local pc by IP address and and analysis data collection
|
|
|
|
|
|
Quote: When should i use and what? Where as i know we can connect MQ queue and JMS queue by using JMS API.
cabn you please help me out for this?
|
|
|
|
|
GiteHrudaya wrote: help me out for this? With what? your question is not clear.
|
|
|
|
|
Hi,
My project depends highly to database as it reads a lot of configs or metadata (some times constant values!) from data base so if some of this values are missing in DB the code fails,I'm looking for a best practice to write a test(or may be a kind of unit test) to check if these values exist in db (on customer servers)
any help or idea would be appreciated
modified 1-Dec-18 3:53am.
|
|
|
|
|
|
thank you for reply, by now I'm using TestNg, my problem is this case seems to be some thing between integration test and unit test and I have no idea how to test this issue
|
|
|
|
|
If this is to check the customer's database then the application just needs a list of elements that should exist. It then requests each one from the database and raises some form of exception for each missing item. You could write a separate application that runs through the database and produces a report for any errors it finds.
|
|
|
|
|
thank you
It sounds a good way
|
|
|
|