Click here to Skip to main content
15,895,011 members
Home / Discussions / Java
   

Java

 
GeneralRe: Connecting to website and checking elements Pin
Valentinor15-Dec-18 5:54
Valentinor15-Dec-18 5:54 
GeneralRe: Connecting to website and checking elements Pin
jschell15-Dec-18 7:05
jschell15-Dec-18 7:05 
AnswerRe: Connecting to website and checking elements Pin
jschell15-Dec-18 7:07
jschell15-Dec-18 7:07 
GeneralRe: Connecting to website and checking elements Pin
Valentinor15-Dec-18 9:30
Valentinor15-Dec-18 9:30 
QuestionHandle Multiple Threads With One Server Pin
Member 1408670212-Dec-18 0:45
Member 1408670212-Dec-18 0:45 
AnswerRe: Handle Multiple Threads With One Server Pin
Richard MacCutchan12-Dec-18 21:54
mveRichard MacCutchan12-Dec-18 21:54 
AnswerRe: Handle Multiple Threads With One Server Pin
jschell15-Dec-18 7:12
jschell15-Dec-18 7:12 
QuestionMerge sort question Pin
Member 1408406810-Dec-18 5:31
Member 1408406810-Dec-18 5:31 
Hello,

I am studying this merge sort algorithm and a mystery still going on.

this merge sort algorithm :

Java
/* Java program for Merge Sort */
class MergeSort 
{ 
	// Merges two subarrays of arr[]. 
	// First subarray is arr[l..m] 
	// Second subarray is arr[m+1..r] 
	void merge(int arr[], int l, int m, int r) 
	{ 
		// Find sizes of two subarrays to be merged 
		int n1 = m - l + 1; 
		int n2 = r - m; 

		/* Create temp arrays */
		int L[] = new int [n1]; 
		int R[] = new int [n2]; 

		/*Copy data to temp arrays*/
		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]; 


		/* Merge the temp arrays */

		// Initial indexes of first and second subarrays 
		int i = 0, j = 0; 

		// Initial index of merged subarry array 
		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++; 
		} 

		/* Copy remaining elements of L[] if any */
		while (i < n1) 
		{ 
			arr[k] = L[i]; 
			i++; 
			k++; 
		} 

		/* Copy remaining elements of R[] if any */
		while (j < n2) 
		{ 
			arr[k] = R[j]; 
			j++; 
			k++; 
		} 
	} 

	// Main function that sorts arr[l..r] using 
	// merge() 
	void sort(int arr[], int l, int r) 
	{ 
		if (l < r) 
		{ 
			// Find the middle point 
			int m = (l+r)/2; 

			// Sort first and second halves 
			sort(arr, l, m); 
			sort(arr , m+1, r); 

			// Merge the sorted halves 
			merge(arr, l, m, r); 
		} 
	} 

	/* A utility function to print array of size n */
	static void printArray(int arr[]) 
	{ 
		int n = arr.length; 
		for (int i=0; i<n; ++i) 
			System.out.print(arr[i] + " "); 
		System.out.println(); 
	} 

	// Driver method 
	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); 
	} 
} 
/* This code is contributed by Rajat Mishra */



The problem is ubicating on the Main function that sorts arr[l..r] using merge() , here :

Java
// Main function that sorts arr[l..r] using 
	// merge() 
	void sort(int arr[], int l, int r) 
	{ 
		if (l < r) 
		{ 
			// Find the middle point 
			int m = (l+r)/2; 

			// Sort first and second halves 
			sort(arr, l, m); 
			sort(arr , m+1, r); 

			// Merge the sorted halves 
			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
AnswerRe: Merge sort question Pin
Richard MacCutchan10-Dec-18 6:32
mveRichard MacCutchan10-Dec-18 6:32 
GeneralRe: Merge sort question Pin
Member 1408406810-Dec-18 11:37
Member 1408406810-Dec-18 11:37 
GeneralRe: Merge sort question Pin
Richard MacCutchan10-Dec-18 21:50
mveRichard MacCutchan10-Dec-18 21:50 
GeneralRe: Merge sort question Pin
Member 1408406811-Dec-18 0:30
Member 1408406811-Dec-18 0:30 
QuestionBuffer reading is hanging while reading ? Pin
GiteHrudaya10-Dec-18 1:28
GiteHrudaya10-Dec-18 1:28 
AnswerRe: Buffer reading is hanging while reading ? Pin
Richard MacCutchan10-Dec-18 4:41
mveRichard MacCutchan10-Dec-18 4:41 
AnswerRe: Buffer reading is hanging while reading ? Pin
jschell15-Dec-18 7:17
jschell15-Dec-18 7:17 
QuestionJava Gui Project Pin
Member 140826818-Dec-18 22:26
Member 140826818-Dec-18 22:26 
AnswerRe: Java Gui Project Pin
Peter_in_27808-Dec-18 23:09
professionalPeter_in_27808-Dec-18 23:09 
AnswerRe: Java Gui Project Pin
Richard MacCutchan9-Dec-18 2:00
mveRichard MacCutchan9-Dec-18 2:00 
QuestionDatabase Connection from Java Pin
prithaa8-Dec-18 6:07
prithaa8-Dec-18 6:07 
GeneralRe: Database Connection from Java Pin
Richard MacCutchan8-Dec-18 6:54
mveRichard MacCutchan8-Dec-18 6:54 
Questionhelp turning code into gui Pin
Member 140790227-Dec-18 11:55
Member 140790227-Dec-18 11:55 
AnswerRe: help turning code into gui Pin
Richard MacCutchan7-Dec-18 22:21
mveRichard MacCutchan7-Dec-18 22:21 
QuestionObjectInputStream block on server side Pin
Valentinor4-Dec-18 4:42
Valentinor4-Dec-18 4:42 
Questionresearch methodology Pin
Member 140745404-Dec-18 1:45
Member 140745404-Dec-18 1:45 
AnswerRe: research methodology Pin
Richard MacCutchan4-Dec-18 2:40
mveRichard MacCutchan4-Dec-18 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.