Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Selection sort cpp algo in serach&sort #173

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions sort_search_problems/Selection_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

void
swapi (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
// cout << *a << " " << *b << endl;
}


vector < int >
selectionSort (vector < int >arr, int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element
swapi (&arr[min_idx], &arr[i]);
}
return arr;
}



int
main ()
{
// cout << "Hello World\n";
std::vector < int >vec;
vec = {202, 4, -5, 66, -2, 0, 96, 5, 78, 12, 48, 88};

vec = selectionSort (vec, vec.size());
for (int x:vec)
{
cout << x << " ";
}
return 0;
}