Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / BubbleSort.cpp
blob: 217bf32ec0b91092955751cc5fa2ad069406f1c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;


void swap(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}

void bubbleSort(int* arr, unsigned size)
{
	for (unsigned j = 0; j < size - 1; j++)
	{
		bool isSwapped = false;

		for (unsigned i = 0; i < size - 1 - j; i++)
		{
			if (arr[i] > arr[i + 1])
			{
				swap(arr[i], arr[i + 1]);
				isSwapped = true;
			}
		}

		if (!isSwapped)
			return;
	}
}

int main()
{
	int arr[] = { 1,2,2,1, 1 };
	selectionSort(arr, 5);

	for (int i = 0; i < 5; i++)
		cout << arr[i] << " ";
}