Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My bubble sort is not working, or its not sorting the numbers after each pass. C++ code is down here #include using namespace std; void

My bubble sort is not working, or its not sorting the numbers after each pass.

C++ code is down here

#include

using namespace std;

void bubblesort(int L[], int N)

{

bool flag;//if a pass occurs

int last;

int temp;

int smaller;

for(int pass = 1;(pass <= N) && (flag == false); pass++)//going trhough the numbers

{

flag = false;//no pass occurs

last = N-pass;//last index after each pass

for(int i = 1; i <= last; i++)

{

if(L[i-1] > L[i])//if the number in front index is greater than number after it swap the two numbers smaller number is in L[i]

{

smaller = i;//I want to swap this to the index i-1

}

temp = L[smaller];

L[smaller] = L[i-1];

L[i-1]= temp;

flag = true;//pass occured

}

}

}

int main()

{

int myElements[10];

int size;

cout << "How many elements? (<=10): ";

cin >> size;

for (int i = 0; i < size; i++)

{

cout << "Element:";

cin >> myElements[i];

}

// ** call bubblesort here with myElements and size

bubblesort(myElements, size);

cout << "Sorted List" << endl;

for (int i = 0; i < size; i++)

{

cout << myElements[i] << " ";

}

cout << endl;

}

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

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

Know how to use reservations systems to inventory demand.

Answered: 1 week ago