//
// main.cpp
// Sorting Algorithms
//
// Created by Himanshu on 21/09/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 BubbleSort (int arr[], int n) {
for (int i=0; i<n; i++) {
for (int j=0; j<n-i-1; j++) {
if (arr[j+1] < arr[j]) {
swap (arr[j], arr[j+1]);
}
}
printf("Array after %d iteration: \n", i+1);
printArray(arr, n);
}
}
void InsertionSort (int arr[], int n) {
for (int i=1; i<n; i++) {
int j = i;
while (j>0 && arr[j-1] > arr[j]) {
swap (arr[j-1], arr[j]);
j--;
}
printf("Array after %d iteration: \n", i);
printArray(arr, n);
}
}
void SelectionSort (int arr[], int n) {
for (int i=0; i<n; i++) {
int indexMin = i;
for (int j = i+1; j<n; j++) {
if (arr[j] < arr[indexMin]) {
indexMin = j;
}
}
if (indexMin != i) {
swap (arr[indexMin], arr[i]);
}
printf("Array after %d iteration: \n", i+1);
printArray(arr, n);
}
}
int main () {
int arr1[5] = {5, 3, 2, 4, 1};
int arr2[5] = {5, 3, 2, 4, 1};
int arr3[5] = {5, 3, 2, 4, 1};
int n = 5;
cout<<"Bubble Sort:"<<endl;
BubbleSort (arr1, n);
cout<<endl<<"Insertion Sort:"<<endl;
InsertionSort (arr2, n);
cout<<endl<<"Selection Sort:"<<endl;
SelectionSort (arr3, n);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBTb3J0aW5nIEFsZ29yaXRobXMKLy8KLy8gIENyZWF0ZWQgYnkgSGltYW5zaHUgb24gMjEvMDkvMjEuCi8vCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHByaW50QXJyYXkgKGludCBhcnJbXSwgaW50IG4pIHsKICAgIGZvciAoaW50IGk9MDsgaTxuOyBpKyspIHsKICAgICAgICBjb3V0PDxhcnJbaV08PCIgIjsKICAgIH0KICAgIGNvdXQ8PGVuZGw7Cn0KCgp2b2lkIEJ1YmJsZVNvcnQgKGludCBhcnJbXSwgaW50IG4pIHsKICAgIGZvciAoaW50IGk9MDsgaTxuOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqPTA7IGo8bi1pLTE7IGorKykgewogICAgICAgICAgICBpZiAoYXJyW2orMV0gPCBhcnJbal0pIHsKICAgICAgICAgICAgICAgIHN3YXAgKGFycltqXSwgYXJyW2orMV0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHByaW50ZigiQXJyYXkgYWZ0ZXIgJWQgaXRlcmF0aW9uOiBcbiIsIGkrMSk7CiAgICAgICAgcHJpbnRBcnJheShhcnIsIG4pOwogICAgfQp9Cgp2b2lkIEluc2VydGlvblNvcnQgKGludCBhcnJbXSwgaW50IG4pIHsKICAgIGZvciAoaW50IGk9MTsgaTxuOyBpKyspIHsKICAgICAgICBpbnQgaiA9IGk7CiAgICAgICAgd2hpbGUgKGo+MCAmJiBhcnJbai0xXSA+IGFycltqXSkgewogICAgICAgICAgICBzd2FwIChhcnJbai0xXSwgYXJyW2pdKTsKICAgICAgICAgICAgai0tOwogICAgICAgIH0KICAgICAgICBwcmludGYoIkFycmF5IGFmdGVyICVkIGl0ZXJhdGlvbjogXG4iLCBpKTsKICAgICAgICBwcmludEFycmF5KGFyciwgbik7CiAgfQp9Cgp2b2lkIFNlbGVjdGlvblNvcnQgKGludCBhcnJbXSwgaW50IG4pIHsKICBmb3IgKGludCBpPTA7IGk8bjsgaSsrKSB7CiAgICAgIGludCBpbmRleE1pbiA9IGk7CiAgICAgIGZvciAoaW50IGogPSBpKzE7IGo8bjsgaisrKSB7CiAgICAgICAgIGlmIChhcnJbal0gPCBhcnJbaW5kZXhNaW5dKSB7CiAgICAgICAgICAgIGluZGV4TWluID0gajsKICAgICAgICAgfQogICAgICB9CiAgICAgIGlmIChpbmRleE1pbiAhPSBpKSB7CiAgICAgICAgIHN3YXAgKGFycltpbmRleE1pbl0sIGFycltpXSk7CiAgICAgIH0KICAgICAgcHJpbnRmKCJBcnJheSBhZnRlciAlZCBpdGVyYXRpb246IFxuIiwgaSsxKTsKICAgICAgcHJpbnRBcnJheShhcnIsIG4pOwogIH0KfQoKaW50IG1haW4gKCkgewogICAgaW50IGFycjFbNV0gPSB7NSwgMywgMiwgNCwgMX07CiAgICBpbnQgYXJyMls1XSA9IHs1LCAzLCAyLCA0LCAxfTsKICAgIGludCBhcnIzWzVdID0gezUsIDMsIDIsIDQsIDF9OwogICAgaW50IG4gPSA1OwogICAgCiAgICBjb3V0PDwiQnViYmxlIFNvcnQ6Ijw8ZW5kbDsKICAgIEJ1YmJsZVNvcnQgKGFycjEsIG4pOwogICAgCiAgICBjb3V0PDxlbmRsPDwiSW5zZXJ0aW9uIFNvcnQ6Ijw8ZW5kbDsKICAgIEluc2VydGlvblNvcnQgKGFycjIsIG4pOwogICAgCiAgICBjb3V0PDxlbmRsPDwiU2VsZWN0aW9uIFNvcnQ6Ijw8ZW5kbDsKICAgIFNlbGVjdGlvblNvcnQgKGFycjMsIG4pOwogICAgCiAgICByZXR1cm4gMDsKfQo=