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

Write a working C++ program to ask the user for a set of grades which are...
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently, each grade is provided, one at a time. Allow for a maximum of 30 grades in defining the array....
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,...
Use C++ language Create a program which will ask the user to input three songs for...
Use C++ language Create a program which will ask the user to input three songs for a playlist (you may use TV shows or movies, if you prefer). Declare three strings to store each of the songs. Use getline to receive the input. Display output which lists each of the songs (or movies or tv shows), on separate lines, with a title on the first line: My Playlist. Insert three lines of comments at the beginning of the program for...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
"After the first calculation is performed, the program should ask the user if they would like...
"After the first calculation is performed, the program should ask the user if they would like to continue. They will type in the word “quit” if they wish to end the program. If they wish to continue, they will enter yes than enter a new length of a triangle side and the string they would like on their sign." How could I show this in code? Please give examples asap. I don't understand loops at all. In java please
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 that demonstrate tree traversal. A. first is to ask the user to...
Write a C program that demonstrate tree traversal. A. first is to ask the user to enter data B. ask how many nodes THIS IS JUST A SAMPLE. PLEASE ANSWER IT IN COMPLETE CODE. See for the sample output. Sample Output: How many node do you have in your tree : 5 Enter root: 20 if you want to add to the left press 0 otherwise press 1 answer: 0 Enter next node: 10 if you want to add to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT