![]() |
|
![]() | ||
|
Subject:
Sorting algorithm using recursion
Category: Computers > Programming Asked by: tiecoon-ga List Price: $5.00 |
Posted:
08 Apr 2003 23:48 PDT
Expires: 09 Apr 2003 16:09 PDT Question ID: 188125 |
Please help me solve the following programming problem, using C, or C++, and recursion: We are given an array A of integers, and 6 integer parameters: lo1, hi1, lo2, hi2, lo3, h3. Both A[lo1 ... hi1] and A[lo2 ... hi2] are disjoint and are in non-descending order. The procedure should merge A[lo1 ... hi1] and A[lo2 ... hi2] into A[lo3 ... hi3] so that is in non-decreasing order. (we must have hi3 - lo3 = hi1 - lo1 + hi2 - lo2 + 1) Test your procedure by implementing a program which drives the procedure. The program should invoke merge(A, 1, 25, 26, 50, 1, 50) where A is an integer array and both A[1...25] and A[26...50] are ordered segments of the array. Temporary array must not be used for this purpose. | |
| |
| |
| |
| |
|
![]() | ||
|
There is no answer at this time. |
![]() | ||
|
Subject:
Re: Sorting algorithm using recursion
From: acdbddh-ga on 09 Apr 2003 06:46 PDT |
#include <stdio.h> void Merge (int *A, int p, int q, int r) { int i,j,l; int C; i=p; j=q+1; while ((i<=p)||(j<=r)) { C=A[j]; if (A[i] <= C) { i++; } else { for (l=j; l>=i+1; l--) A[l]=A[l-1]; A[i]=C; i++; j++; } } } int Merge (int *A, int lo1, int hi1, int lo2, int hi2, int lo3, int hi3) { if ((lo2!=hi1+1) || (lo3!=lo1) || (hi2!=hi3)) { // wrong parametrs return -1; } Merge(A,lo1,hi1,hi2); return 0; } int main() { int i; int A[50]={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49, 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48}; for (int i=0; i<50; i++) printf("%i ",A[i]); printf ("\n\n"); // printf ("Merge(A,0,24,49);\n"); // Merge(A,0,24,49); printf ("Merge(A,0,24,25,49,0,49);\n"); Merge(A,0,24,25,49,0,49); for (int i=0; i<50; i++) printf("%i ",A[i]); printf ("\n\n"); return 0; } |
Subject:
Re: Sorting algorithm using recursion
From: mathtalk-ga on 09 Apr 2003 07:17 PDT |
Hi, acdbddh-ga: You are welcome to post proposed solutions to the programming problem here, although you appear to have posted without reading or addressing the difficulty noted in my request for clarification above. What you did afterwards (posting an email address and soliciting payments) is not permitted by Google Answers' Terms of Service. Please review them here to avoid future infractions (see esp. Parts 2 and 4): http://answers.google.com/answers/termsofservice.html regards, mathtalk-ga |
Subject:
Re: Sorting algorithm using recursion
From: mojo069-ga on 09 Apr 2003 07:18 PDT |
The solution by acdbddh-ga is not recursive. void merge(int A*, int low1, int high1, int low2, int high2, int low3, int high3) { if (A[low1] > A[low2]) { A = swap(A, low1, low2); } if (low1 < low2) { low1++; merge(A, low1, high1, low2, high2, low3, high3); } if (low2 < high3) { low2++; low1 = low3; merge(A, low1, high1, low2, high2, low3, high3); } } void swap(int A*, int index1, int index2) { int tmp; tmp = A[index1]; A[index1] = A[index2]; A[index2] = tmp; } int main() { int A[10] = { 1, 2, 5, 7, 9, 2, 4, 6, 8, 10 }; merge(A, 0, 4, 5, 9, 0, 9); for (int i = 0; i < 9; i++) { printf("%i ", A[i]); } return 0; } |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |