diff options
1 files changed, 34 insertions, 0 deletions
| -rw-r--r-- | Sem. 11/SelectionSort.cpp | 34 |
diff --git a/Sem. 11/SelectionSort.cpp b/Sem. 11/SelectionSort.cpp new file mode 100644 index 0000000..87cc6eb --- /dev/null +++ b/Sem. 11/SelectionSort.cpp @@ -0,0 +1,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] << " "; +} |
