Question

In: Computer Science

Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers. C++...

Write a program that performs a merge-sort algorithm without using a recursion. Only using pointers.
C++ programming language; #include<iostream>

Solutions

Expert Solution

Code for the Given Problem:


#include<iostream>
using namespace std;
/* merge function to merge the sorted smaller arrays */
void
merge (int **a, int ibegin, int imid, int iend, int **b)
{
  int i = ibegin, j = imid;
  for (int k = ibegin; k < iend; k++)
    b[k] = a[i < imid && (j >= iend || *a[i] <= *a[j]) ? i++ : j++];
}

/* function to split the array  */
void
splitmerge (int **b, int ibegin, int iend, int **a)
{
  if (iend - ibegin < 2)
    return;
  int imid = (iend + ibegin) / 2;
  splitmerge (a, ibegin, imid, b);
  splitmerge (a, imid, iend, b);
  merge (b, ibegin, imid, iend, a);
}

/* mergesort function which sorts and returns the sorted array */
int **
mergesort (int *a, int size)
{
  int **ret = new int *[size];
  int **tmp = new int *[size];
  for (int i = 0; i < size; i++)
    ret[i] = tmp[i] = a + i;
  splitmerge (tmp, 0, size, ret);
  delete (tmp);
  return ret;
}

/* driver program */
int
main ()
{
  /* taking the inputs */
  int n;
  cout << "Enter the size of array :";
  cin >> n;
  int a[n];
  cout << "Enter the array : ";
  for (int i = 0; i < n; i++)
    {
      cin >> a[i];
    }
  int size = sizeof a / sizeof a[0];
  /* calling mergesort function */
  int **ret = mergesort (a, size);
  /* printing the sorted array */
  cout << "Sorted Array is : ";
  for (int i = 0; i < size; i++)
    {
      cout << (*ret[i]) << " ";
    }
  cout << endl;

  delete (ret);
  return 0;
}

Screenshot for reference:

Sample Output:


Related Solutions

Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT