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

Need help making changes to my code I have below that has insertion, merge, quic

ID: 3722730 • Letter: N

Question

Need help making changes to my code I have below that has insertion, merge, quick and counting sort to the specifications for part B and part C I need to take out the unsorted.txt file that I have in my code for the screenshot of input files I need to implement. Any help to fit these specifications to adjust my code in c++ would be appreciated.

a)     The program implements insertion sort, merge sort, quick sort, and counting sort as subprograms or classes (radix sort is optional);

b)     The program reads input from a data file specified by user and write the sorted output into another file;

c)     Design your program in the way that it can accept user’s options such as:

*. Which sort algorithm to use;

*. Which sort order to use, that is, non-decreasing order or non-increasing order?

*. Whether should the output remove duplications?

#include<iostream>

#include<fstream>

#include <algorithm>

#include <vector>

#include <cmath>

using namespace std;

void insertionsort(int array[],int n){

int i, key, j;

for (i= 1; i< n; ++i){

key= array[i];

j=i-1;

while (j >= 0 && array[j]>key){

array[j+1] = array[j];

j = j-1;

}

array[j+1] = key;

}

ofstream out("unsorted.txt");

for(i=0;i<n;++i)

out<<array[i]<<endl;

cout<<" Output file successful"<<endl;

out.close();

}

void merge(int arr[], int l, int m, int r){

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++) {

L[i] = arr[l + i];

}

for (j = 0; j < n2; j++) {

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

}

while (i < n1 && j < n2){

if (L[i] <= R[j]){

arr[k] = L[i];

i++;

}

else{

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1){

arr[k] = L[i];

i++;

k++;

}

while (j < n2){

arr[k] = R[j];

j++;

k++;

}

}

void mergeSort(int arr[], int l, int r){

if (l < r){

int m = l+(r-l)/2;

mergeSort(arr, l, m);

mergeSort(arr, m+1, r);

merge(arr, l, m, r);

}

}

void swap(int a, int b){

int x = a;

a = b;

b = x;

}

int partition (int arr[], int low, int high){

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high- 1; j++){

if (arr[j] <= pivot){

i++;

swap(&arr[i], &arr[j]);

}

}

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}

void quickSort(int arr[], int low, int high){

if (low < high){

int z = partition(arr, low, high);

quickSort(arr, low, z - 1);

quickSort(arr, z + 1, high);

}

}

void countingSort(int input, t elements) {

t max = std::max_element(input, input+elements);

std::vector<t> counts(max+1);

for (int i=0; i<elements; i++)

++counts[input[i]];  

int c = input[0];

input[0] = input[elements-1];

int d = input[elements-2];

input[elements-2] = c;

input[elements-1] = d;

int main(int argc,char *argv[]){

int array[20],i=0,j,count=0,k;

ifstream input;

input.open(argv[1]);

if(input.fail()) {

cout<<"File is empty or does not exist";

}

else{

while(input.eof()==0){

input>>array[i];

i=i+1;

count=count+1;

}

string s;

cout << "Would you like to use the insertion, merge, quick, or counting sorting method?:";

cin >> s;

if(s==std::string("insert")){

insertionsort(array,count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

}

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("quick")){

quickSort(array,0,count-1);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

}

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("merge")){

mergeSort(array,0,count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i)

out<<array[i]<<endl;

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("count")){

countingSort(array, count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

cout<<array[i]<<" ";

}

cout<<"Output file successful"<<endl;

out.close();

}

else{

cout<<"Choose either to use insertion, merge, quick or counting sort method";

}

}

return 0;

}

file10 txt (file10.txt), file 100 txt (file100.txt), file 1000 txt (file1000.txt), file 10000 txt (file10000.txt), file100000 txt (file100000.txt), f file1000000.bxt (ile 1000000.txt)

Explanation / Answer

#include<iostream>

#include<fstream>

#include <algorithm>

#include <vector>

#include <cmath>

using namespace std;

void insertionsort(int array[],int n){

int i, key, j;

for (i= 1; i< n; ++i){

key= array[i];

j=i-1;

while (j >= 0 && array[j]>key){

array[j+1] = array[j];

j = j-1;

}

array[j+1] = key;

}

ofstream out("unsorted.txt");

for(i=0;i<n;++i)

out<<array[i]<<endl;

cout<<" Output file successful"<<endl;

out.close();

}

void merge(int arr[], int l, int m, int r){

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++) {

L[i] = arr[l + i];

}

for (j = 0; j < n2; j++) {

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

}

while (i < n1 && j < n2){

if (L[i] <= R[j]){

arr[k] = L[i];

i++;

}

else{

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1){

arr[k] = L[i];

i++;

k++;

}

while (j < n2){

arr[k] = R[j];

j++;

k++;

}

}

void mergeSort(int arr[], int l, int r){

if (l < r){

int m = l+(r-l)/2;

mergeSort(arr, l, m);

mergeSort(arr, m+1, r);

merge(arr, l, m, r);

}

}

void swap(int a, int b){

int x = a;

a = b;

b = x;

}

int partition (int arr[], int low, int high){

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high- 1; j++){

if (arr[j] <= pivot){

i++;

swap(&arr[i], &arr[j]);

}

}

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}

void quickSort(int arr[], int low, int high){

if (low < high){

int z = partition(arr, low, high);

quickSort(arr, low, z - 1);

quickSort(arr, z + 1, high);

}

}

void countingSort(int input, t elements) {

t max = std::max_element(input, input+elements);

std::vector<t> counts(max+1);

for (int i=0; i<elements; i++)

++counts[input[i]];  

int c = input[0];

input[0] = input[elements-1];

int d = input[elements-2];

input[elements-2] = c;

input[elements-1] = d;

int main(int argc,char *argv[]){

int array[20],i=0,j,count=0,k;

ifstream input;

input.open(argv[1]);

if(input.fail()) {

cout<<"File is empty or does not exist";

}

else{

while(input.eof()==0){

input>>array[i];

i=i+1;

count=count+1;

}

string s;

cout << "Would you like to use the insertion, merge, quick, or counting sorting method?:";

cin >> s;

if(s==std::string("insert")){

insertionsort(array,count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

}

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("quick")){

quickSort(array,0,count-1);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

}

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("merge")){

mergeSort(array,0,count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i)

out<<array[i]<<endl;

cout<<"Output file successful"<<endl;

out.close();

}

else if(s==std::string("count")){

countingSort(array, count);

ofstream out("unsorted.txt");

for(i=0;i<count;++i){

out<<array[i]<<endl;

cout<<array[i]<<" ";

}

cout<<"Output file successful"<<endl;

out.close();

}

else{

cout<<"Choose either to use insertion, merge, quick or counting sort method";

}

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote