Skip to content
Snippets Groups Projects
Commit 1a483060 authored by dario.genga's avatar dario.genga
Browse files

Add sort with insertion

The program now perform a descending sort using the insertion algorithm.
parent 4dc6ea86
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,12 @@ int main() {
// Permute smallest value with the last value
permute_lowest_value_with_last_value(array, array_size);
printf("Array after swapping the smallest value with the last one\n");
printf("Array after swapping the smallest value with the last one :\n");
print_array(array, array_size);
// Descending sort the array by using the insertion algorithm
sort_by_insertion_desc(array, array_size);
printf("Array after insertion desc sort :\n");
print_array(array, array_size);
// Swap the highest value of the array with the last element of the array
......
......@@ -84,6 +84,19 @@ void permute_lowest_value_with_last_value(int* array, size_t array_size) {
swap(&array[lowest_index], &array[last_index]);
}
void sort_by_insertion_desc(int* array, size_t array_size) {
size_t i = 1;
while (i < array_size) {
size_t k = i;
while (k > 0 && array[k - 1] < array[k]) {
swap(&array[k], &array[k - 1]);
k -= 1;
}
i++;
}
}
int find_index_highest_value_in_array(int* array, size_t array_size) {
int highest_value;
size_t index_highest_value = 0;
......
......@@ -21,6 +21,8 @@ void shuffle_array(int* array, size_t array_size);
void perform_cyclic_permutation(int* array, size_t array_size, size_t cycle_number);
void sort_by_insertion_desc(int* array, size_t array_size);
void print_array(int* array, size_t array_size);
void swap(int *x, int *y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment