Question

In: Computer Science

Iterative Merge Sort is not working #include #include #include int* b; void merge(int a[], int left,...

Iterative Merge Sort is not working

#include
#include
#include
int* b;
void merge(int a[], int left, int mid, int right)
{
   int i = left, j = mid + 1, k = left;

   while (i <= mid && j <= right)
   {
       if (a[i] < a[j])
           b[k++] = a[i++];
       else
           b[k++] = a[j++];
   }
   for (; i <= mid; i++)
   {
       b[k++] = a[i];
   }
   for (; j <= right; j++)
   {
       b[k++] = a[j];
   }
   for (int i = left; i <= right; i++)
   {
       a[i] = b[i];
   }

}
void mergesort(int a[], int n)
{
   int p, left, right, mid, i;
   for (p = 2; p <= n; p = p * 2)
   {
       for (i = 0; i + p - 1 < n; i = i + p)
       {
           left = i;
           right = i + p - 1;
           mid = (left + right) / 2;
           merge(a, left, mid, right);
       }
   }
   if (p / 2 < n)
   {
       merge(a, 0, (p / 2) - 1, n - 1);
   }
}
void main()
{
   int max, * a;
   scanf("%d", &max);
   a = (int*)malloc(sizeof(int) * max);
   b = (int*)malloc(sizeof(int) * max);
   for (int i = 0; i < max; i++)
   {
       scanf("%d", &a[i]);
   }
   mergesort(a, max);
   for (int i = 0; i < max; i++)
   {
       printf(" %d", a[i]);
   }
}

input 50

50 49 48 47 46 45 ~ 5 4 3 2 1

output

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 19 20 21 22 23 24 ~ 50

why 1,2 are between in 18 and 19?

please answer and correct code

Solutions

Expert Solution

#include <stdio.h>
#include<iostream>

int* b;
void m(int a[], int l1, int m1, int r1)
{
int i = l1, j = m1 + 1, q = l1;//intialise the values

while (i <= m1 && j <= r1)//iterate until one of the array finished
{
if (a[i] < a[j])//compare both sub sorted array values
b[q++] = a[i++]; //update to new array
else
b[q++] = a[j++]; //compare both sub sorted array values and update to new array
}
for (; i <= m1; i++)//iterate until large sub array is finished
{
b[q++] = a[i]; //update to new array
}
for (; j <= r1; j++) //iterate until large sub array is finished
{
b[q++] = a[j]; //update to new array
}
for (int i = l1; i <= r1; i++)//copy new array to old array
{
a[i] = b[i];
}

}
void msort(int a[], int n)
{
int p, l1, r1, m1, i;//initialised
for (p = 2; p <= n; p = p * 2)//step size for loop this is 2 times
{
for (i = 0; i + p - 1 < n; i = i + p) //step size for loop this is 3,5,7.... times
{
l1 = i;
r1 = i + p - 1;
m1 = (l1 + r1) / 2;
m(a, l1, m1, r1);
}
}
if (p / 2 < n)
{
m(a, 0, (p / 2) - 1, n - 1);
}
}
void main()
{
int max, * a;//variables
scanf("%d", &max);//getting value of array size
a = (int*)malloc(sizeof(int) * max);//creating array
b = (int*)malloc(sizeof(int) * max);//creating array
for (int i = 0; i < max; i++)
{
scanf("%d", &a[i]);//getting values from user
}
msort(a, max); //calling sort function
for (int i = 0; i < max; i++)
{
printf(" %d", a[i]);//printing the values if sorted array
}
}

in this as loop start with p=2 it will sort all the 2 elements start with 50 49 47 48 45 46,,,,,,,2 1 and after p=3 it going to sort 3 elements 47 49 50 48 45 46,,,,,,,2 1 and in this way it will the whole list and it result into neglect last 2 elemets and they sort at some value of p in between and in that 1 and 2 become before all the sort element that why it comes after 18 in your output.

If you found this answer helpful please give a thumbs up.


Related Solutions

   private static void merge(int arr[], int l, int m, int r) {        //...
   private static 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...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT