*******C++ HELP******* 5-3 The 2 versions of bubble sort try to sort the array w
ID: 3757034 • Letter: #
Question
*******C++ HELP******* 5-3
The 2 versions of bubble sort try to sort the array when the arrays are already sorted. Modify the two bubble sorts to stop as soon as they sort their arrays.
#include <ctime>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
using namespace std;
template <typename Type>
void bubbleSort(Type elts[], int size);
template <typename Type>
void fill(Type elts[], int size);
int main()
{
const int size = 12;
int a[size];
fill(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
cout << endl;
bubbleSort(a, size);
cout << endl; copy(a, a + size, ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
template <typename Type>
void bubbleSort(Type elts[], int size)
{
for (int sorted_part = size - 1; sorted_part > 0; --sorted_part)
for (int unsorted_part = 0; unsorted_part < sorted_part;
++unsorted_part)
if (elts[unsorted_part] > elts[unsorted_part
+ 1])
{
Type t = elts[unsorted_part];
elts[unsorted_part] =
elts[unsorted_part + 1];
elts[unsorted_part + 1] = t;
}
}
int seed = 0;
default_random_engine e(seed);
uniform_int_distribution<int> u(10, 99);
template <typename Type>
void fill(Type elts[], int size)
{
for (int i = 0; i < size; ++i)
elts[i] = u(e);
}
================= SECOND PROBLEM=================
ZOOM
#include <ctime>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
using namespace std;
template <typename Type>
void bubbleSort(Type elts[], int size);
template <typename Type>
void fill(Type elts[], int size);
int main()
{
const int size = 12;
int a[size];
fill(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
bubbleSort(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
return 0;
}
template <typename Type>
void bubbleSort(Type elts[], int size)
{
for(int sorted_part = 0; sorted_part < size - 1; ++sorted_part)
for(int unsorted_part = size - 1; unsorted_part >
sorted_part; --unsorted_part)
if (elts[unsorted_part] < elts[unsorted_part
- 1])
{
Type t = elts[unsorted_part];
elts[unsorted_part] =
elts[unsorted_part - 1];
elts[unsorted_part - 1] = t;
}
}
int seed = 0;
default_random_engine e(seed);
uniform_int_distribution<int> u(10, 99);
template <typename Type>
void fill(Type elts[], int size)
{
for (int i = 0; i < size; ++i)
elts[i] = u(e);
}
Explanation / Answer
//changes made are highlighted
#include <ctime>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
using namespace std;
template <typename Type>
void bubbleSort(Type elts[], int size);
template <typename Type>
void fill(Type elts[], int size);
int main()
{
const int size = 12;
int a[size];
fill(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
cout << endl;
bubbleSort(a, size);
cout << endl; copy(a, a + size, ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
template <typename Type>
void bubbleSort(Type elts[], int size)
{
bool comparision; //declaration of comparision flag
for (int sorted_part = size - 1; sorted_part > 0; --sorted_part)
{
comparision = true; //assigns true for before entering into inner loop
for (int unsorted_part = 0; unsorted_part < sorted_part;++unsorted_part)
{
if (elts[unsorted_part] > elts[unsorted_part+ 1])
{
Type t = elts[unsorted_part];
elts[unsorted_part] =elts[unsorted_part + 1];
elts[unsorted_part + 1] = t;
comparision = false; //if execution enters this block then changes to false
}
}
if(comparision) //if comparision is true then breaks the outer loop
break;
}
}
int seed = 0;
default_random_engine e(seed);
uniform_int_distribution<int> u(10, 99);
template <typename Type>
void fill(Type elts[], int size)
{
for (int i = 0; i < size; ++i)
elts[i] = u(e);
}
//SECOND PROBLEM
#include <ctime>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
using namespace std;
template <typename Type>
void bubbleSort(Type elts[], int size);
template <typename Type>
void fill(Type elts[], int size);
int main()
{
const int size = 12;
int a[size];
fill(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
bubbleSort(a, size);
copy(a, a + size, ostream_iterator<int>(cout, " ")); cout << endl;
return 0;
}
template <typename Type>
void bubbleSort(Type elts[], int size)
{
bool comparision = true; //declared comparision flag which is initialized to true
for(int sorted_part = 0; sorted_part < size - 1&&comparision; ++sorted_part) //included comparision in the condition
{
comparision = false; //assings false before entering inner loop
for(int unsorted_part = size - 1; unsorted_part >sorted_part; --unsorted_part)
if (elts[unsorted_part] < elts[unsorted_part- 1])
{
Type t = elts[unsorted_part];
elts[unsorted_part] =
elts[unsorted_part - 1];
elts[unsorted_part - 1] = t;
comparision = true; //if exectuion enters this block comparision is changed to true.
}
}
}
int seed = 0;
default_random_engine e(seed);
uniform_int_distribution<int> u(10, 99);
template <typename Type>
void fill(Type elts[], int size)
{
for (int i = 0; i < size; ++i)
elts[i] = u(e);
}
//I have implemented the same logic of breaking the for loop in two ways for the two problems but both of them will stop further sorting once the array gets sorted.
//any query, post in the comment section.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.