Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c + + please Do a line - by - line BigO analysis of these 3 algorithm sets. It is not necessary to determine the

c++ please Do a line-by-line BigO analysis of these 3 algorithm sets. It is not necessary to determine the purpose of the algorithms or even the names of the algorithms. Based on your BigO analysis of these algorithms, determine which algorithm is most efficient and why. You may determine the algorithms have the same BigO and therefore are equally efficient but the algorithms do vary in efficiency that may not be apparent in a BigO analysis.
If you find a "logic" or "runtime" error in the code, the "error" isn't intentional so assign it an appropriate BigO value for the code in question.
----------------------------
Algorithm Set1
void algorithm1(int a[], int i){
int t, s =
while (s i){
t = a[s];
a[s]= a[i];
a[i]= t;
s++;
i--;
}
}
int algorithm2(int a[], int n){
int m, i;
for (m =0, i =0; i n; ++i)
if (a[i]> a[m])
m = i;
return m;
}
void algorithm3(int a[], int n){
for (int c = n; c >1; --c){
int m = algorithm2(a, c);
if (m != c -1){
algorithm1(a, m);
algorithm1(a, c -1);
}
}
}
Algorithm Set2
void algorithmA(int a[], int l, int h)
{
if (l >= h)
return;
if (a[l]> a[h])
swap(a[l], a[h]);
if (h - l +1>2){
int t =(h - l +1)/3;
algorithmA(a, l, h - t);
algorithmA(a, l + t, h);
algorithmA(a, l, h - t);
}
}
Algorithm Set3
const int P2= pow(2,5);
void algorithmX(int a[], int l, int r)
{
for (int i = l +1; i = r; i++){
int t = a[i];
int j = i -1;
while (j >= l && a[j]> t){
a[j +1]= a[j];
j--;
}
a[j +1]= t;
}
}
void algorithmY(int a[], int l, int m, int r)
{
int l1= m - l +1, l2= r - m;
int left[l1], right[l2];
for (int i =0; i l1; i++)
left[i]= a[l + i];
for (int i =0; i l2; i++)
right[i]= a[m +1+ i];
int i =0;
int j =0;
int k = l;
while (i l1 && j l2){
if (left[i]= right[j]){
a[k]= left[i];
i++;
}
else {
a[k]= right[j];
j++;
}
k++;
}
while (i l1){
a[k]= left[i];
k++;
i++;
}
while (j l2){
a[k]= right[j];
k++;
j++;
}
}
void algorithmZ(int a[], int n)
{
for (int i =0; i n; i += P2)
algorithmX(a, i, min((i + P2-1),(n -1)));
for (int s = P2; s n; s =2* s){
for (int l =0; l n; l +=2* s){
int m = l + s -1;
int r = min((l +2* s -1),(n -1));
if (m r)
algorithmY(a, l, m, r);
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions