For C++
(Need both flowcharts and program)
Code -
#include <iostream>
#include <fstream>
using namespace std;
// Sort a[] of size n using Bubble Sort.
void bubbleSort (int a[], int n)
{
int i, j,counterBubble=0;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1;
++j)
{
// Comparing
consecutive data and switching values if value at j > j+1.
if (a[j] >
a[j+1])
{
counterBubble+=1;
a[j] = a[j]+a[j+1];
a[j+1] = a[j]-a[j + 1];
a[j] = a[j]-a[j + 1];
}
}
// Value at n-i-1 will be maximum
of all the values below this index.
}
cout<<"\nBubble Sort needs to compares and swaps
"<< counterBubble<<" times "<<endl;
}
void selectionSort (int a[], int n)
{
int i, j,counterSelection=0;
for (i = 0; i < n; ++i)
{
for (j = i+1; j < n; ++j)
{
// Comparing
consecutive data and switching values if value at i > j.
if (a[i] >
a[j])
{
counterSelection+=1;
a[i] = a[i]+a[j];
a[j] = a[i]-a[j];
a[i] = a[i]-a[j];
}
}
// Value at i will be minimum of
all the values above this index.
}
cout<<"Selection Sort needs
to compares and swaps "<< counterSelection<<" times
"<<endl;
}
void insertionSort(int arr[], int n){
int i,j,temp,counterInsertion=0;
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
counterInsertion+=1;
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
cout<<"Insertion Sort needs
to compares and swaps "<< counterInsertion<<" times
"<<endl;
}
// A function to merge the two half into a sorted data.
void Merge(int *a, int low, int high, int mid)
{
// We have low to mid and mid+1 to high already
sorted.
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] =
a[i];
k++;
i++;
}
else
{
temp[k] =
a[j];
k++;
j++;
}
}
// Insert all the remaining values from i to mid into
temp[].
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
// Insert all the remaining values from j to high into
temp[].
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}
// A function to split array into two parts.
int counterMerge=0;
void MergeSort(int *a, int low, int high)
{
int mid;
if (low < high)
{
counterMerge++;
mid=(low+high)/2;
// Split the data into two
half.
MergeSort(a, low, mid);
MergeSort(a, mid+1, high);
// Merge them to get sorted
output.
Merge(a, low, high, mid);
}
}
int main()
{
int i;
//cout<<"\nEnter the number of data element to
be sorted: ";
//cin>>n;
std::fstream myfile("input.txt", std::ios_base::in);
int arr,temp= 0;
int n= 20;
int a[n];
while (myfile >> arr)
{
a[temp] = arr;
temp++;
}
int bubbleArr[n];
int insertionArr[n];
int selectionArr[n];
int mergeArr[n];
for(i = 0; i < n; i++)
{
bubbleArr[i] = a[i];
insertionArr[i] = a[i];
selectionArr[i] = a[i];
mergeArr[i] = a[i];
}
cout<<"\n 1st unorder set of data \n";
bubbleSort(bubbleArr, n);
selectionSort(selectionArr, n);
insertionSort(insertionArr, n);
MergeSort(mergeArr, 0, n-1);
cout<<"Merge Sort needs to
compares and swaps "<< counterMerge<<" times
\n"<<endl;
// Display the sorted data.
cout<<"\nBubble sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<bubbleArr[i];
cout<<"\nSelection sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<selectionArr[i];
cout<<"\nInsertion sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<insertionArr[i];
cout<<"\nMerge sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<mergeArr[i];
counterMerge=0;
int a2[20] = { 42, 13, 75, 25, 64, 23, 12, 60, 41, 65, 43, 32, 23,
86, 44, 23, 65, 26, 36, 53};
for(i = 0; i < n; i++)
{
bubbleArr[i] = a2[i];
insertionArr[i] = a2[i];
selectionArr[i] = a2[i];
mergeArr[i] = a2[i];
}
cout<<"\n\n 2nd unorder set of data \n";
bubbleSort(bubbleArr, n);
selectionSort(selectionArr, n);
insertionSort(insertionArr, n);
MergeSort(mergeArr, 0, n-1);
cout<<"Merge Sort needs to
compares and swaps "<< counterMerge<<" times
\n"<<endl;
// Display the sorted data.
cout<<"\nBubble sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<bubbleArr[i];
cout<<"\nSelection sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<selectionArr[i];
cout<<"\nInsertion sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<insertionArr[i];
cout<<"\nMerge sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<mergeArr[i];
counterMerge=0;
int a3[20] = {86, 75, 67, 65, 65, 64, 60, 53, 44, 42, 41, 36, 32,
26, 25, 23, 23, 23, 13, 12 };
for(i = 0; i < n; i++)
{
bubbleArr[i] = a3[i];
insertionArr[i] = a3[i];
selectionArr[i] = a3[i];
mergeArr[i] = a3[i];
}
cout<<"\n\n Reverse set of data \n";
bubbleSort(bubbleArr, n);
selectionSort(selectionArr, n);
insertionSort(insertionArr, n);
MergeSort(mergeArr, 0, n-1);
cout<<"Merge Sort needs to
compares and swaps "<< counterMerge<<" times
\n"<<endl;
// Display the sorted data.
cout<<"\nBubble sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<bubbleArr[i];
cout<<"\nSelection sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<selectionArr[i];
cout<<"\nInsertion sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<insertionArr[i];
cout<<"\nMerge sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<mergeArr[i];
counterMerge=0;
int a4[20] = {12, 13, 23, 23, 23, 25, 26, 32, 36, 41, 42, 43, 44,
53, 60, 64, 65, 65, 75, 86 };
for(i = 0; i < n; i++)
{
bubbleArr[i] = a4[i];
insertionArr[i] = a4[i];
selectionArr[i] = a4[i];
mergeArr[i] = a4[i];
}
cout<<"\n\n Ordered set of data \n";
bubbleSort(bubbleArr, n);
selectionSort(selectionArr, n);
insertionSort(insertionArr, n);
MergeSort(mergeArr, 0, n-1);
cout<<"Merge Sort needs to
compares and swaps "<< counterMerge<<" times
\n"<<endl;
// Display the sorted data.
cout<<"\nBubble sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<bubbleArr[i];
cout<<"\nSelection sorting of data";
for (i = 0; i < n; i++)
cout<<" "<<selectionArr[i];
cout<<"\nInsertion sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<insertionArr[i];
cout<<"\nMerge sorting of data ";
for (i = 0; i < n; i++)
cout<<" "<<mergeArr[i];
return 0;
}
Screenshots -
For C++ Write a main program that inputs a list of 20 numbers from a text file and then calls 4 ...
Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....
Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.
Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...
Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...
The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file. The InsertionSort.h file performs an include of Swap.h,...
**C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...
LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...
Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...