-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selection_sort.c: Add Selection Sort in C
This adds Selection Sort algorithm written in C Closes #145
- Loading branch information
Showing
2 changed files
with
37 additions
and
2 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
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,35 @@ | ||
#include <stdio.h> // for printf function | ||
|
||
void selection_sort(int[], int); | ||
void print_array(int[], int); | ||
|
||
int main() { | ||
int arr[] = {4, 3, 42, 82, 5, 2, 33}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
selection_sort(arr, n); | ||
printf("Sorted array is: "); | ||
print_array(arr, n); | ||
printf("\n"); | ||
return 0; | ||
} | ||
|
||
/* Function to print array */ | ||
void print_array(int arr[], int n) { | ||
for (int i = 0; i < n; ++i) { | ||
printf("%d ", arr[i]); | ||
} | ||
} | ||
|
||
/* Selection Sort algorithm */ | ||
void selection_sort(int arr[], int n) { | ||
for (int i = 0; i < n - 1; ++i) { | ||
int small = i; | ||
for (int j = i + 1; j < n; ++j) { | ||
if (arr[j] < arr[small]) | ||
small = j; | ||
} | ||
int temp = arr[i]; | ||
arr[i] = arr[small]; | ||
arr[small] = temp; | ||
} | ||
} |
60d1164
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment on 60d1164.
There are 21 results for the section all.cpp. They have been shortened and will not be shown inline because they are more than 10.
Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.