Please tell me why this code is refusing garbage. Well compiled, it is implemented on the basis of the Coromandel Algorithm, taking the given numbers originally given two zeros functions are taken in a dynamic array. One is to merge two sub arrays through merge sort and the other to divide the array into the array.
# include & lt; Iostream & gt; using namespace std; In zero, two merge (int * A, int p, int q, int r) // to merge a function two / sub arrays {int n1 = q-p + 1; Int N2 = R-Q; Int L [N1]; Int r [n 2]; For (int i = 0; i & lt; n1; i ++) {l [i] = a [p + i]; } Int m = 1; (Int j = 0; j & lt; n2; j ++) for (r [j] = a [q + m]; m = m + 1;) int i = 0; int j = 0; For (int k = 0; k & lt; r; k ++) {if (l [i] & lt; = r [j]) {a [k] = l [i]; i = i + 1; } And {a [k] = r [ja] j = j + 1;}}} zero merge (int * a, int p, int r) // sort the sub arrays {if (p & gt; n; Int * a = NULL; A = new int [n]; int temp {Cin> gt; & gt; temp; cout & lt; & lt; for "enter number" & lt; endl; (int i = 0; i & lt; n; i ++) * (A + I) = floating; // insert the given numbers into the dynamic array cout
You are getting your gaps inaccurately in your code everywhere For example:
- Based on your usage,
main
,mergeresort
is considered to sort the codeal of indices[0, n -1]
.
In this sense, your recursion index of [p, r-1]
, in mergeresort
, first of all you [P, q-1]
sorting [q + 1, r-1]
should: completely ignore the index
. . . Similarly, merge
is confused: when you compete in L
( A [i],
should be
A [p + I]
), takes it as [p, q]
as a list, and [q, r]
as another list: note That you copy the entry to q
twice, and you probably also copy r
when you probably should not be.
To fix your code, you need to fix that it should be working at all intervals. It's not an hard problem, you just brought yourself to clearly to write all your functions and loops and accessories It is supposed to do this.
Typical conference these days are half open intervals: You should generally think of taking the [p, q]
indicator from the list. <<> [p, q]
is same [p, q-1]
) Here are several examples why it is preferred:
[p, r]
number of entries for rp
for
through the loop >> > > [p, r]
gives you interval in parts [p, q]
and [q, R]
- - No worries about adding 1
to places Is. For example merge
will usually be designed to take the first list index comes from [p, q]
and index Second list from [q, r]
.
No comments:
Post a Comment