Created
November 2, 2023 14:00
-
-
Save cristiancrazy/78b45b84631ff032d05f4e6df3dee795 to your computer and use it in GitHub Desktop.
Basic array sorting functions, including Selection Sort and Bubble Sort
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
/* ================================== * | |
* Author: CristianCrazyIT | |
* Date: 02-11-2023 (dmy) | |
* Desc: Simple Sorting Functions | |
* ================================== */ | |
/* Selection Sort on int array[N-Dimension] */ | |
void selectionSort(int array_in[], int N){ | |
int min, tmp; | |
for(int i = 0; i < N; i++){ | |
min = i; | |
for(int j = i+1; j < N; j++){ | |
if(array_in[j] < array_in[min]) min = j; | |
} | |
if(array_in[i] != array_in[min]){ | |
tmp = array_in[i]; | |
array_in[i] = array_in[min]; | |
array_in[min] = tmp; | |
} | |
} | |
} | |
/* Bubble Sort on int array[N-Dimension] */ | |
void bubble_sort(int array_in[], int N){ | |
int tmp; | |
for(int i = 0; i < N-1; i++){ | |
for(int j = 0; j < N-1; j++){ | |
if(array_in[j+1] < array_in[j]){ | |
tmp = array_in[j]; | |
array_in[j] = array_in[j+1]; | |
array_in[j+1] = tmp; | |
} | |
} | |
} | |
} | |
/* Bubble Sort Variant - on int array[N-Dimension] */ | |
void bubble_sort_v(int array_in[], int N){ | |
bool changes; int tmp; | |
do{ | |
changes = false; | |
for(int i = 0; i < N-1; i++) | |
if(array_in[i+1] < array_in[i]){ | |
tmp = array_in[i]; | |
array_in[i] = array_in[i+1]; | |
array_in[i+1] = tmp; | |
changes = true; | |
} | |
}while(changes == true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment