Question

In: Computer Science

Write a C++ program, ask the user about which sorting algorithm they would like to use...

Write a C++ program, ask the user about which sorting algorithm they would like to use in order to sort the vector. The four sorting algorithms your program should implement are: selection sort, merge sort, quick sort, and insertion sort. Once a sorting algorithm is selected, your program should display step by step every change that the sorting algorithm does on its journey to completely sorting the integer values of the vector

Your output file should then contain the sorted values of the vector with a single integer on each line.

For example:

Enter a file for input: Input1.txt

Enter a file for output: Output1.txt

What sorting algorithm would you like to use? 1. Selection Sort 2. Merge Sort 3. Quick Sort 4. Insertion Sort

(Enter an integer value for your selection): 2

Original Vector: [14, 7, 3, 12, 9, 11, 6, 2]

Vector is now being sorted:

[14, 7, 3, 12] [9, 11, 6, 2]

[14, 7] [3, 12] [9, 11] [6, 2]

[14] [7] [3] [12] [9] [11] [6] [2]

[7, 14] [3, 12] [9, 11] [2, 6]

[3, 7, 12, 14] [2, 6, 9, 11]

[2, 3, 6, 7, 9, 11, 12, 14]

Solutions

Expert Solution

// SpecialSort.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// ------------------------------UTILITY FUNCTIONS-------------------------//

void swap(int *xp, int *yp)
{
   int temp = *xp;
   *xp = *yp;
   *yp = temp;
}
void printArray(int arr[], int size)
{
   int i;
   cout << endl;
   for (i = 0; i < size; i++)
       cout << arr[i] << " ";
   cout << endl;
}

// --------------------------------QUICK SORT ------------------------------//

int partition(int *&arr, int low, int high)
{
   int pivot = arr[high]; // pivot
   int i = (low - 1); // Index of smaller element

   for (int j = low; j <= high - 1; j++)
   {
      
       if (arr[j] < pivot)
       {
           i++; // increment index of smaller element
           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)
   {
       /* pi is partitioning index, arr[p] is now
       at right place */
       int pi = partition(arr, low, high);
       printArray(arr, high + 1);
      
       quickSort(arr, low, pi - 1);
       printArray(arr, high + 1);
      
       quickSort(arr, pi + 1, high);
       printArray(arr, high + 1);
   }
}

//------------------------------MERGE SORT --------------------------------//

void merge(int *&arr, int l, int m, int r)
{
   int i, j, k;
   int n1 = m - l + 1;
   int n2 = r - m;

   /* create temp arrays */
   int *L = new int[n1];
   int *R = new int[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;
       int n = r + 1;
  
       mergeSort(arr, l, m);
       printArray(arr, n);

       mergeSort(arr, m + 1, r);
       printArray(arr, n);

       merge(arr, l, m, r);
       printArray(arr, n);
   }
}

// -----------------------------INSERTION SORT-------------------------------//

   void insertionSort(int *&arr, int n){
       int i, key, j;
       for (i = 1; i < n; i++)
       {
           key = arr[i];
           j = i - 1;

          
           while (j >= 0 && arr[j] > key)
           {
               arr[j + 1] = arr[j];
               j = j - 1;
           }
           arr[j + 1] = key;
       }
       printArray(arr, n);
   }

  
//------------------------------SELECTION SORT ----------------------------//

   void selectionSort(int *&arr, int n)
   {
       int i, j, min_idx;

  
       for (i = 0; i < n - 1; i++)
       {
          
           min_idx = i;
           for (j = i + 1; j < n; j++)
               if (arr[j] < arr[min_idx])
                   min_idx = j;

          
           swap(&arr[min_idx], &arr[i]);
           printArray(arr, n);
       }
      
   }

  

int main()
{
   int *a = new int[2];
   cout << "Welcome to the sorting program. Enter your choice to sort." << endl;
   cout << "1 for Selection sort" << endl << "2 for Insertion sort"<<endl << "3 for Merge sort" << endl;
   cout << "4 for Quick Sort"<<endl << "Enter 0 to quit "<<endl;
  
   int choice;
   cin >> choice;
   cout << "Enter filename where Array/Vector is stored" << endl;
   string filename;
   cin >> filename;
  
  
   int size = 0;
   string line;
   ifstream myfile(filename);
  
   if (myfile.is_open())
   {
       while (getline(myfile, line))
       {
           size++;
       }
       //delete[]a;
       a = new int[size];
       int i = 0;
       myfile.close();
       myfile.open(filename);
       while (getline(myfile, line)) {
       //   cout << "hey";
               int temp = stoi(line.c_str());
               if (temp) {
                   a[i] = temp;
                   i++;
               }
               else {
                   cout << "File character invalid";
               }
          
       }
       myfile.close();
   }
   else cout << "Unable to open file";
   printArray(a, size);

   if (choice == 1) {
       cout << endl<<"Working on selection sort.." << endl;
       selectionSort(a, size);
   }
   if (choice == 2) {
       cout << endl << "Working on insertion sort.." << endl;
       insertionSort(a, size);

   }
   if (choice == 3) {
       cout << endl << "Working on merge sort.." << endl;
       mergeSort(a, 0, size - 1);

   }
   if (choice == 4) {
       cout << endl << "Working on quick sort.." << endl;
       quickSort(a, 0, size - 1);

   }
   else if (choice == 0) {
       exit(0);
   }

  
   //-------------------------WRITING TO FILE---------------//
   ofstream file;
   cout << "Enter name of file for writing"<<endl;
   string filenamew;
   cin >> filenamew;
   file.open(filenamew);
   if (file.is_open()) {
       for (int i = 0; i < size; i++) {
           file << a[i]<<endl;
       }
   }
   file.close();
}

COMMENT DOWN FOR ANY QUERIES AND<

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.


Related Solutions

USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Write a C# program to ask the user for an undetermined amount ofnumbers (until 0...
Write a C# program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum.
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a program using C++ that ask the user for the flowing: How many shares to...
Write a program using C++ that ask the user for the flowing: How many shares to be bought: The price per share: Percent commission for the broker for each transaction: Average annual return as of percentage: The program should calculate and display the following: The amount paid for the stock alone (without the commission) The amount of the commissionThe total amount of paid (the payment for stock plus the commission) After TEN years, your shares will worth:
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT