Here's my implementation of a SelectionSort algorithm in C and Python.
It's fairly verbose and easy to read, but could probably do with a cleanup at some point.
The C version was written using GCC 9.3.0, and the Python version was written using Python 3.8.10.
Created
July 6, 2021 20:57
-
-
Save JamesPhillipsUK/21724fcacaf542ab16dff512e1b9ff15 to your computer and use it in GitHub Desktop.
SelectionSort in C and Python
This file contains hidden or 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
| /** | |
| * Selection Sort Algorithm. | |
| * | |
| * @author Jesse Phillips | |
| **/ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int data[] = {9, 7, 8, 5, 1, 2}; | |
| void selectionSort() | |
| { | |
| int length = sizeof(data) / sizeof(data[0]); | |
| int minimumElement; | |
| for (int count = 0; count < length - 1; count++) | |
| { | |
| minimumElement = count; | |
| for (int cnt = count + 1; cnt < length; cnt++) | |
| { | |
| if (data[cnt] < data[minimumElement]) | |
| minimumElement = cnt; | |
| } | |
| if (count != minimumElement) | |
| { | |
| data[count] = data[count] + data[minimumElement]; | |
| data[minimumElement] = data[count] - data[minimumElement]; | |
| data[count] = data[count] - data[minimumElement]; | |
| } | |
| } | |
| } | |
| int main(void) | |
| { | |
| for (int count = 0; count < sizeof(data)/sizeof(data[0]); count++) | |
| printf("%d, ", data[count]); | |
| putchar('\n'); | |
| selectionSort(); | |
| for (int count = 0; count < sizeof(data)/sizeof(data[0]); count++) | |
| printf("%d, ", data[count]); | |
| return EXIT_SUCCESS; | |
| } |
This file contains hidden or 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
| """ | |
| selectionSort.py | |
| selectionSort Algorithm | |
| Authors | |
| ------- | |
| Jesse Phillips | |
| """ | |
| data = [9, 7, 8, 5, 1, 2] | |
| def selectionSort(): | |
| for count in range (len(data)): | |
| minimumElement = count | |
| for cnt in range (count + 1, len(data)): | |
| if data[cnt] < data[minimumElement]: | |
| minimumElement = cnt | |
| if count != minimumElement: | |
| data[count] = data[count] + data[minimumElement] | |
| data[minimumElement] = data[count] - data[minimumElement] | |
| data[count] = data[count] - data[minimumElement] | |
| def main(): | |
| for count in range(len(data)): | |
| print("%d, " %data[count], end = '') | |
| print("") | |
| selectionSort() | |
| for count in range(len(data)): | |
| print("%d, " %data[count], end = '') | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment