Question
Hello, i need help for this question. , i already have a Code, but it doesnt work. Can somebody find the mistake, why it doesnt
Hello, i need help for this question. , i already have a Code, but it doesnt work. Can somebody find the mistake, why it doesnt work?
With the sorting method 'Direct Swap (Bubble Sort)' you find for an ascending sorting of a container with n elements within an outer loop, multiple passes of an inner loop through the Container instead. In each iteration of the inner loop, adjacent pairs of values are compared. If a pair has the wrong order (the smaller value is further back), the two values become swapped inside the container. If a couple already has the correct order, nothing is done. There this way a small value in each pass of the inner loop only one position forward in the worst case, (n-1) passes through the outer loop for a complete sorting required. Write a function template bubbleSort that is a container of type T (T, for example array
This is my Code
//#include
#include
using namespace std;
template
void print(T& container, int size) { //print container
for (int i = 0; i < size; i++)
cout << container[i] << " ";
cout << endl;
}
template
bool lessEqualSorted(T& container, int size) { //check order
for (int i = 0; i < size - 1; i++)
if (container[i] > container[i + 1])
return false;
return true;
}
template
void directSwap(T& container, int size) { //bubble sort
for (int i = 0; i < size; i++)
for (int j = 0; j < size - 1; j++)
if (container[j] >= container[j + 1])
swap(container[j], container[j + 1]);
}
template
void directSelection(T& container, int size) { //selection sort
for (int i = 0; i < size; i++) {
int index = i;
for (int j = i + 1; j < size; j++) {
if (container[index] > container[j])
index = j;
}
swap(container[i], container[index]);
}
}
std::array
int main() {
//testing functions for (int i = 0; i < 100; i++)
int a [i] = rand() % 100; //random numbers
cout << "Array operations ";
print(a, 100);
cout << "Sorted? :" << lessEqualSorted(a, 100) << " "; //lessequalsorted function
cout << "After Bubble Sort: ";
directSwap(a, 100); //bubble sort
print(a, 100);
cout << "Sorted? :" << lessEqualSorted(a, 10) << " "; //lessequalsorted function
cout << "--------------------------------- ";
for (int i = 0; i < 100; i++)
a[i] = rand() % 100;
print(a, 100);
cout << "Sorted? :" << lessEqualSorted(a, 100) << " "; //lessequalsorted function
cout << "After Selection Sort: ";
directSelection(a, 100); //selection sort
print(a, 100);
cout << "Sorted? :" << lessEqualSorted(a, 100) << endl; //lessequalsorted function
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ";
vector
cout << "String operations ";
print(v, 5);
cout << "Sorted? :" << lessEqualSorted(v, 5) << " "; //lessequalsorted function
cout << "After Bubble Sort: ";
directSwap(v, 5); //bubble sort
print(v, 5);
cout << "Sorted? :" << lessEqualSorted(v, 5) << " "; //lessequalsorted function
cout << "--------------------------------- ";
v = { "ABC","ABR","AED","NS","ABCD" };
print(v, 5);
cout << "Sorted? :" << lessEqualSorted(v, 5) << " "; //lessequalsorted function
cout << "After Selection Sort: ";
directSelection(v, 5); //selection sort
print(v, 5);
cout << "Sorted? :" << lessEqualSorted(v, 5) << " "; //lessequalsorted function
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started