Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bubble sorting #1

Open
nactyecenko opened this issue Oct 22, 2024 · 1 comment
Open

Bubble sorting #1

nactyecenko opened this issue Oct 22, 2024 · 1 comment

Comments

@nactyecenko
Copy link
Owner

No description provided.

@nactyecenko
Copy link
Owner Author

#include
using namespace std;

void bubble_sort(int a[], int size);

int main(void)
{
int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
int i = 0;

cout << "To sorting: \n";
for (i=0; i<10; i++) cout << arr[i] <<" ";
cout << endl;

bubble_sort(arr, 10);

cout << "Later on: \n";
for (i=0; i<10; i++) cout << arr[i] << " ";
cout << endl;

return 0;

}

void bubble_sort(int a[], int size)
{
int switched = 1;
int hold = 0;
int i = 0;
int j = 0;

size -= 1;

for (i = 0; i < size && switched; i++)
{
	switched = 0;
	for(j = 0; j < size - i; j++)
	if(a[j] > a[j+1])
	{
		switched = 1;
		hold = a[j];
		a[j] = a[j + 1];
		a[j + 1] = hold;
	}
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant