Question

write a bash program to do merge sort. in that program make sure, you read the...

write a bash program to do merge sort. in that program make sure, you read the array of size 20 using a function, and another function for output of the array and use separate functions to do merge sort

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello!

Below is your code

run the script and enter your 20 numbers in next line

-------------------------------
#!/bin/bash

input(){
read -a a
}

merge() {
local first=2
local second=$(( $1 + 2 ))
for i in ${@:2}
do
if [[ $first -eq $(( $1 + 2 )) ]]
then
echo ${@:$second:1} ; ((second += 1))
else
if [[ $second -eq $(( ${#@} + 1 )) ]]
then
echo ${@:$first:1} ; ((first += 1))
else
if [[ ${@:$first:1} -lt ${@:$second:1} ]]
then
echo ${@:$first:1} ; ((first += 1))
else
echo ${@:$second:1} ; ((second += 1))
fi
fi
fi
done
}

mergesort() {
if [[ $1 -ge 2 ]]
then
local med=$(( $1 / 2 ))
local first=( $(mergesort $med ${@:2:$med}) )
local second=( $(mergesort $(( $1 - $med )) ${@:$(( $med + 2 )):$(( $1 - $med ))}) )
echo $(merge $med ${first[@]} ${second[@]})
else
echo $2
fi
}

output(){
echo ${a[@]} ; echo $(mergesort 20 ${a[@]})
}

input

output
-----------------------------------

Output:

Hoping it will help!

Add a comment
Know the answer?
Add Answer to:
write a bash program to do merge sort. in that program make sure, you read the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • USING C++ write a program that creates a modified version of Merge sort in which insertion...

    USING C++ write a program that creates a modified version of Merge sort in which insertion sort will be used for merging array elements.

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that...

    Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that can efficiently be solved with merge sort. Implement the merge sort algorithm Determine and summarize the algorithmic run-time of the merge sort algorithm. Specifications: As you saw from your readings and exercises, Merge Sort is based upon the idea of divide-and-conquer. If implemented correctly, your merge sort algorithm should have a time complexity of O(n*log(n)). Your task for this lab is to write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT