Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 11 / SelectionSort.cpp
blob: 87cc6ebfa7ac0e3ac95a63ad3f340500e8ca38b2 (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
#include <iostream>
using namespace std;

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

void selectionSort(int* arr, unsigned size)
{
	for (unsigned i = 0; i < size - 1; i++)
	{
		//findMin
		int minElementIndex = i;
		for (unsigned j = i + 1; j < size; j++)
		{
			if (arr[j] < arr[minElementIndex])
				minElementIndex = j;
		}
		if (minElementIndex != i)
			swap(arr[i], arr[minElementIndex]);
	}
}

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

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