Skip to content

Commit

Permalink
Selection Sort (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhendra-20 authored and MadhavBahl committed Feb 7, 2019
1 parent 6e71f8c commit c03b668
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions day32/CPP/day32_solved.cpp
@@ -0,0 +1,47 @@
/*
Author: Shubhendra Singh
Github: shubhendra-20
*/

#include <iostream>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;

for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<" ";
}
int main()
{
int n;
cin>>n;
int a[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}
selectionSort(a, n);
printArray(a, n);
return 0;
}

0 comments on commit c03b668

Please sign in to comment.