Skip to content

Commit

Permalink
Create selection_sort.c
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit010pro authored Oct 12, 2022
1 parent 4e71889 commit 5280d48
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sorting Programs/selection_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>

// SELECTION SORT

void selection_sort(int arr[], int n) {
int min_idx, temp;
for (int i = 0; i < n - 1; i++)
{
min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
if (min_idx != i) {
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
}


// PRINT ARRAY

void print_arr(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
}

int main()
{
int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10;

bubble_sort(arr, n);

print_arr(arr, n);

return 0;
}

0 comments on commit 5280d48

Please sign in to comment.