//
// main.cpp
// Selection Sort
//
// Created by Himanshu on 29/08/21.
//
#include <iostream>
using namespace std;
void printArray (int arr[], int n) {
for (int i=0; i<n; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
void selectionSort(int arr[], int n) {
cout<<"Initial array:"<<endl;
printArray(arr, n);
cout<<"Selection sort"<<endl;
for (int i=0; i<n-1; i++) {
int indexMin = i;
for (int j=i+1; j<n; j++) {
if (arr[j] < arr[indexMin]) {
indexMin = j;
}
}
if (indexMin != i) {
swap(arr[i], arr[indexMin]);
}
cout<<"Array after "<<(i+1)<<" iteration: "<<endl;
printArray(arr, n);
}
}
int main() {
int arr[] = {21, 15, 30, 13 , 2};
int n = 5;
selectionSort(arr, n);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBTZWxlY3Rpb24gU29ydAovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAyOS8wOC8yMS4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgcHJpbnRBcnJheSAoaW50IGFycltdLCBpbnQgbikgewogICAgZm9yIChpbnQgaT0wOyBpPG47IGkrKykgewogICAgICAgIGNvdXQ8PGFycltpXTw8IiAiOwogICAgfQogICAgY291dDw8ZW5kbDsKfQoKdm9pZCBzZWxlY3Rpb25Tb3J0KGludCBhcnJbXSwgaW50IG4pIHsKICAgIGNvdXQ8PCJJbml0aWFsIGFycmF5OiI8PGVuZGw7CiAgICBwcmludEFycmF5KGFyciwgbik7CiAgICAKICAgIGNvdXQ8PCJTZWxlY3Rpb24gc29ydCI8PGVuZGw7CiAgICAKICAgIGZvciAoaW50IGk9MDsgaTxuLTE7IGkrKykgewogICAgICAgIGludCBpbmRleE1pbiA9IGk7CiAgICAgICAgZm9yIChpbnQgaj1pKzE7IGo8bjsgaisrKSB7CiAgICAgICAgICAgIGlmIChhcnJbal0gPCBhcnJbaW5kZXhNaW5dKSB7CiAgICAgICAgICAgICAgICBpbmRleE1pbiA9IGo7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaWYgKGluZGV4TWluICE9IGkpIHsKICAgICAgICAgICAgc3dhcChhcnJbaV0sIGFycltpbmRleE1pbl0pOwogICAgICAgIH0KICAgICAgICBjb3V0PDwiQXJyYXkgYWZ0ZXIgIjw8KGkrMSk8PCIgaXRlcmF0aW9uOiAiPDxlbmRsOwogICAgICAgIHByaW50QXJyYXkoYXJyLCBuKTsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpbnQgYXJyW10gPSB7MjEsIDE1LCAzMCwgMTMgLCAyfTsKICAgIGludCBuID0gNTsKICAgIHNlbGVjdGlvblNvcnQoYXJyLCBuKTsKICAgIHJldHVybiAwOwp9Cg==