Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e71f8c
commit c03b668
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |