Skip to content

Commit

Permalink
selection_sort.c: Add Selection Sort in C
Browse files Browse the repository at this point in the history
This adds Selection Sort algorithm written in C

Closes #145
  • Loading branch information
123vivekr committed Oct 5, 2018
1 parent f298c76 commit 60d1164
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ This repository contains examples of various algorithms written on different pro
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | | |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |

| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | | | [:octocat:](shell_sort/Python) |
| [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | | | |

## Implemented Data Structures

Expand Down
35 changes: 35 additions & 0 deletions selection_sort/C/selection_sort.c
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;
}
}

1 comment on commit 60d1164

@sangamcse
Copy link
Member

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.

Message File Line
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 1
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 7
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 8
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 9
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 10
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 11
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 12
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 13
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 18
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 19
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 20
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 25
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 26
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 27
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 28
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 29
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 30
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 31
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 32
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 33
Line contains following spacing inconsistencies: - Spaces used instead of tabs. selection_sort/C/selection_sort.c 34

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.