******HELPPPP******* The bubble sort function moves the smallest array element t
ID: 3752275 • Letter: #
Question
******HELPPPP*******
The bubble sort function moves the smallest array element to the first position in the unsorted part of the array and increases the first position in the unsorted part of the array. Eventually the array is sorted. The first position of the unsorted part of the array is 0 when the sort begins.
Rewrite the function so that the largest element moves to the first position in the unsorted part of the array and decreases the first position in the unsorted part of the array. The first position of the unsorted part of the array is size - 1 when the sort begins. "size" is the number of elements in the array.
#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
#include <ctime>
#include <iostream>
#include <iterator>
#include <random>
#include <string>
using namespace std;
template <typename Type>
void bubbleSort(Type x[], int N)
{
int i, j;
for (i = 0; i < N - 1; i++)
{
// the previous i elements are placed
for (j = 0; j < N - i - 1; j++)
{
// swap the two elements
if (x[j] < x[j+1])
{
Type temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
}
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);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.