Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!