Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 or vector ) as parameters and the algorithm described applied to the container to make it ascending (i.e. more precisely: 'not descending': the one on one Value following value is greater than or equal to the predecessor). The sorting method 'Direct Selection (Selection Sort)' searches an unsorted container in the inner one of two nested loops, first after the smallest element. After that, the smallest element exchanged with the first element of the container. This process is used for the unsorted subcontainers, beginning with the second, then the third, fourth, etc. element of the container. Any such Passing through the container means that another element comes to the place where it belongs. As soon as the unsorted subcontainer only contains one element, the container is sorted. In the worst In this case, too (n-1) passes through the outer loop are required. Write a function template selectionSort that takes a container of type T as a parameter and apply this algorithm to the container so as not to sort it in descending order. Also write a function template lessEqualSorted, which uses a container of type T as a parameter takes over and returns true if the container is sorted in ascending order (i.e. 'not descending'), false if this is not the case. Test your sort functions by creating an array filled with random integers and then one filled with at least five words in an unsorted order Sort vector and check the sorting results with lessEqualSorted.

This is my Code

//#include

#include #include using namespace std;

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::arraya;

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 ";

vectorv = { "ABC","ABR","AED","NS","ABCD" };

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

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

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

Students also viewed these Databases questions