Skip to content

Commit

Permalink
add: selectionsort (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiracle authored Oct 13, 2022
1 parent a5bf9db commit 7217cc5
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions C/selectionsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,30 @@

void selectionSort(int arr[], int n)
{
// your code goes here
// print the sorted array
int index = 0;
int min;
for (int i = 0; i < n; i++)
{
min = arr[i];
for (int j = i; j < n; j++)
{
if (arr[j] < min)
{
index = j;
min = arr[index];
}
}
if (arr[i] != arr[index])
{
arr[i] += arr[index];
arr[index] = arr[i] - arr[index];
arr[i] -= arr[index];
}
}
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}

int main()
Expand Down

0 comments on commit 7217cc5

Please sign in to comment.