Skip to content
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
44 changes: 44 additions & 0 deletions selection-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
using namespace std;
void selectionsort(int [], int);
void swap(int*, int*);
void getArray(int [], int);
int main() //Driver code
{
int arr[] = {-2, 6, 28, 13, 16, 11};
int n = 6;
selectionsort(arr, n);
cout<<"Sorted Array:\n";
getArray(arr, n);
return 0;
}
void swap(int *x, int *y) //Function to swap the elements
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void selectionsort(int arr1[], int sz) //Selection sort function
{
int i, j, small;
for (i = 0; i<sz-1; i++) //outer loop
{
small = i;
for (j = i+1; j<sz; j++) //inner loop
{
if(arr1[j]<arr1[small]) //compares value at j and small
{
small = j; //if value at j is smaller, index is stored in small
}
}
swap(arr1[i], arr1[small]); //swapping the smallest element
}
}
void getArray(int A[], int size) //Function to display the array
{
for (int i = 0; i<size; i++)
{
cout<<A[i]<<' ';
}
}