In: Computer Science
Need C++ code to be able to run, keep getting a constant value error
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
int c;
void insertionSort(int* arr, int n)
{
for (int i = 1;i < n;i++)
{
int v = arr[i];
int j;
for (j = i - 1;j > -1;j--)
{
c++;
if (arr[j] > v)
{
arr[j + 1] = arr[j];
}
else
{
break;
}
}
arr[j + 1] = v;
}
}
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1, n2 = r - m;
int al[n1], ar[n2];
for (int i = l;i < m + 1;i++)
{
al[i - l] = arr[i];
}
for (int i = m + 1;i < r + 1;i++)
{
ar[i - (m + 1)] = arr[i];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
c++;
if (al[i] - 1 < ar[j])
{
arr[k] = al[i];
i++;
k++;
}
else
{
arr[k] = ar[j];
j++;
k++;
}
}
while (i < n1)
{
arr[k] = al[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = ar[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);
}
}
int main() {
int n = 1;
for (int i = 0;i < 13;i++)
{
n *= 2;
int arr[n];
c = 0;
for (int i = 0;i < n;i++)
{
arr[i] = n - i;
}
cout << "INSERTION SORT" << endl;
auto start = high_resolution_clock::now();
insertionSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast(stop - start);
cout << "Size: " << n << "\t\tComparisons: "
<< c << "\t\tRunning time(ms): " <<
duration.count() << endl;
c = 0;
for (int i = 0;i < n;i++)
{
arr[i] = n - i;
}
cout << "Merge SORT" << endl;
start = high_resolution_clock::now();
mergeSort(arr, 0, n - 1);
stop = high_resolution_clock::now();
duration = duration_cast(stop - start);
cout << "Size: " << n << "\t\tComparisons: "
<< c << "\t\tRunning time(ms): " <<
duration.count() << endl;
return 0;
}
}
//I have fixed your cpp program you had made the syntax error while using the duration_cast
//Link to code => https://repl.it/@FAYAZPASHA/TrustworthyVirtuousClasslibrary#main.cpp
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
int c;
void insertionSort(int* arr, int n)
{
for (int i = 1;i < n;i++)
{
int v =
arr[i];
int j;
for (j = i - 1;j
> -1;j--)
{
c++;
if (arr[j] >
v)
{
arr[j + 1]
= arr[j];
}
else
{
break;
}
}
arr[j + 1] = v;
}
}
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1, n2 = r - m;
int al[n1], ar[n2];
for (int i = l;i < m + 1;i++)
{
al[i - l] = arr[i];
}
for (int i = m + 1;i < r + 1;i++)
{
ar[i - (m + 1)] = arr[i];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
{
c++;
if (al[i] - 1 < ar[j])
{
arr[k] = al[i];
i++;
k++;
}
else
{
arr[k] = ar[j];
j++;
k++;
}
}
while (i < n1)
{
arr[k] = al[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = ar[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);
}
}
int main() {
int n = 1;
for (int i = 0;i < 13;i++)
{
n *= 2;
int arr[n];
c = 0;
for (int j = 0; j < n;j++)
{
arr[j] = n - j;
}
cout << "INSERTION SORT" << endl;
auto start = high_resolution_clock::now();
insertionSort(arr, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Size: " << n << "\t\tComparisons: "
<< c << "\t\tRunning time(ms): " <<
duration.count() << endl;
c = 0;
for (int k = 0;k < n;k++)
{
arr[k] = n - k;
}
cout << "Merge SORT" << endl;
start = high_resolution_clock::now();
mergeSort(arr, 0, n - 1);
stop = high_resolution_clock::now();
duration = duration_cast<milliseconds>(stop - start);
cout << "Size: " << n << "\t\tComparisons: "
<< c << "\t\tRunning time(ms): " <<
duration.count() << endl;
return 0;
}
}