Implement a bash script that will contain the following:
(a) Your script will receive two input arguments. The first argument will be a filename that stores numbers that are going to be sorted, and the second argument will be an integer that will be used to select a sorting algorithm. The input file can contain the numbers in one column with each line containing a different number. Your script can have the following command line usage:
./bash_sort -f | --filename <input filename> -a | --algorithm <sorting algorithm> [-h | --help]
An example command-line for calling the script can be:
./bash_sort -f input.dat -a 1
which will sort the numbers in input.dat using one of the sorting algorithms (e.g. insertion sort). The same script can also be executed using long parameter options as:
./bash_sort --filename input.dat --algorithm 1
If the user selects the help option as:
./bash_sort -h
or
./bash_sort --help
then your script will display a help menu explaining the parameter options and the available sorting algorithms.
(b) Implement a bash function inside your script that prints a usage statement. Your main script will call this function if the number of input arguments is not 2 or if the help option is not selected.
(c) Implement the following sorting algorithms as shell functions inside your bash script:
- Insertion sort (parameter option 1)
- Merge sort (parameter option 2)
- Heap sort (parameter option 3)
- Quick sort (parameter option 4)
Your functions will be able to process real-valued numbers. You can consider using the bc utility for this purpose. You can use the following command to round the division result to a smaller integer for bucket sort:
echo "scale=0;3/2" | bc
You can use the pseudo-codes in Cormen's Introduction to Algorithms course to implement the algorithms.
(d) Randomly generate 10,000, 100,000, and 1,000,000 real-valued numbers and store them in a file. Sort your numbers using the functions you implemented and record the running times. To test counting sort, generate integers instead of real numbers. You can call the date command before and after calling each sorting algorithm and take the difference between the starting and ending times.
(e) Sort the numbers you generated using the sort command, record the running times, and compare with the best running times you obtained in part (d). Which algorithm is used in the sort command?