Skip to content

Instantly share code, notes, and snippets.

@Colk-tech
Created April 10, 2021 15:08
Show Gist options
  • Save Colk-tech/a5ac83cb92445788f5a2c9e0faf84857 to your computer and use it in GitHub Desktop.
Save Colk-tech/a5ac83cb92445788f5a2c9e0faf84857 to your computer and use it in GitHub Desktop.
intの配列を昇順にソートするプログラム (A program that sorts array of int in ASCENDING order)
int is_sorted(int target_array[], int array_length) {
for (int i = 0; i < array_length - 1; i++) {
if (target_array[i] > target_array[i+1]) {
return 0;
}
}
return 1;
}
int sort_int_array(int target_array[], int *sorted_array_ptr, int array_length) {
int temp;
memcpy(sorted_array_ptr, target_array, sizeof(int) * NUMBERS_LENGTH);
while ( !is_sorted(sorted_array_ptr, array_length) ) {
for (int i = 0; i < array_length - 1; i++) {
if (sorted_array_ptr[i] < sorted_array_ptr[i+1]) {
temp = sorted_array_ptr[i];
sorted_array_ptr[i] = sorted_array_ptr[i+1];
sorted_array_ptr[i+1] = temp;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment