Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description In this homework, you will practice sorting algorithms and recursive

ID: 3607590 • Letter: D

Question

Description In this homework, you will practice sorting algorithms and recursive functions. From a text file containing several integers which you'll save into an array, you'll need to print out the original array, sort the array according to provided arguments, and then print the final sorted array Problem You will be given a text file. This text file contains several integers that need to be sorted (in either ascending or descending order). Sort these integers, and then print the sorted array to the screen (cout) in accordance with the arguments provided. Instructions on how to implement the sorting algorithm are listed later in this document. We will be giving you an outline of how it works, but you will be implementing that functionality yourself. We've given you a list of functions (see purple text later in document) that will need to be implemented, but the parameters that you use and the contents are entirely up to you You will be reading your arguments from the command line. The name of the input file will be passed as argument 1, argv [1]. The number of lines in the file will be passed as argument 2, argv [21. The sorting order ("ascending" or "descending") will be passed as argument 3, argv [3]. The string "ascending" or "descending" will be given in lowercase. The sorting type ("normal" or "recursive") will be passed as argument 4, argv [4] Text File Contents The text file will contain several integers, each on a different line. The number of lines passed in as an argument will always be consistent with number of lines in the text file when we grade this.

Explanation / Answer

Code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include<string.h>
using namespace std;
void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
void normalSort(int arr[], int n , bool asc){
    int i, j, min_i;
    for (i = 0; i < n-1; i++)
    {
        min_i = i;
        for (j = i+1; j < n; j++)
        {
          if(asc==true){
                if (arr[j] < arr[min_i])
                    min_i = j;
          }
          else{
            if (arr[j] > arr[min_i])
                    min_i = j;
          }

        }
        swap(&arr[min_i], &arr[i]);
    }
}
int minIndex( bool asc, int a[], int i, int j)
{
    if (i == j)
        return i;
    int k = minIndex(asc,a, i + 1, j);
    if(asc==true)
        return (a[i] < a[k])? i : k;
    else
        return (a[i] > a[k])? i : k;
}
void recursiveSort( bool asc, int a[], int n, int index=0)
{
    if (index == n)
       return;
    int k = minIndex(asc,a, index, n-1);
    if (k != index)
       swap(a[k], a[index]);
    recursiveSort(asc,a, n, index + 1);
}
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf(" ");
}
void printArrayRec(int arr[], int start, int len)
{
    if(start >= len){
        cout << "" << endl;
        return;
    }

    printf("%d ", arr[start]);
    printArrayRec(arr, start + 1, len);
}
int main(int argc, char** argv)
{
    int num_lines = 0;

    std::string line;
    ifstream inFile;
    inFile.open(argv[1]);
    int n = stoi(argv[2]);
    int* arr = new int[n];
    for (; num_lines < n && getline(inFile,line); ++num_lines)
    {
        arr[num_lines]=stoi(line);
    }
    num_lines = 0;
    bool asc=false;
    if(strcmp(argv[3], "ascending")==0)
        asc=true;
    if(strcmp(argv[4], "normal")==0){
        cout << "Before: ";
        printArray(arr,n);
        normalSort(arr,n,asc);
        cout << "After: ";
        printArray(arr,n);
    }
    else{
        cout << "Before: ";
        printArrayRec(arr, 0, n);
        recursiveSort(asc,arr,n);
        cout << "After: ";
        printArrayRec(arr, 0, n);
    }
    return 0;
}


*****************************************************************************************************************************

Sample inputs and outputs:

sortingNumbers.txt 10 ascending normal

sortingNumbers.txt 10 ascending recursive

sortingNumbers.txt 10 descending normal

sortingNumbers.txt 10 descending recursive

************************************************************************************************************************************

I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)