Question

In: Computer Science

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>)

Solutions

Expert Solution

#include<iostream>
using namespace std;
#define MAX 30

int main()
{
        int arr[MAX],temp[MAX],i,j,k,n,size,l1,h1,l2,h2;

        cout<<"Enter the number of elements : "; // To accept number of elements from user
        cin>>n;
  cout<<"\nEnter the elements:\n";
        for(i=0;i<n;i++) //To accept elements to be sorted
        {
                cin>>arr[i];
        }


        /*l1 lower bound of first pair and so on*/
        for(size=1; size < n; size=size*2 )
        {
                l1=0;
                k=0;  //Index for temp array
                while( l1+size < n)
                {
                        h1=l1+size-1;
                        l2=h1+1;
                        h2=l2+size-1;
                        // h2 exceeds the limlt of arr
                        if( h2>=n ) 
                                h2=n-1;
                        
                        /*Merge the two pairs with lower limits l1 and l2*/
                        i=l1;
                        j=l2;
                        
                        while(i<=h1 && j<=h2 )
                        {
                                if( arr[i] <= arr[j] )
                                        temp[k++]=arr[i++];
                                else
                                        temp[k++]=arr[j++];
                        }
                        
                        while(i<=h1)
                                temp[k++]=arr[i++];
                        while(j<=h2)
                                temp[k++]=arr[j++];
                        //Merging complete
                        /*Take the next two pairs for merging */
                        l1=h2+1; 
                }//End of while

                //any pair left
                for(i=l1; k<n; i++) 
                        temp[k++]=arr[i];

                for(i=0;i<n;i++)
                        arr[i]=temp[i];

                
        }//End of for loop
        //Displaying the sorted elements
        cout<<"\nSorted list is :\n";
        for( i = 0 ; i<n ; i++)
                cout<<arr[i]<<" ";
}

Output screenshot:-


Related Solutions

(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...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
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 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 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.
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT