Implemented merge sort
This commit is contained in:
parent
edca11bb96
commit
c78cc298c5
2 changed files with 66 additions and 5 deletions
63
merge_sort.c
63
merge_sort.c
|
|
@ -3,7 +3,7 @@
|
||||||
*-----------------------------------------------------------------------------
|
*-----------------------------------------------------------------------------
|
||||||
* Exercise Number: S06
|
* Exercise Number: S06
|
||||||
* Title: Insertion sort implementation
|
* Title: Insertion sort implementation
|
||||||
* Author: */<your name>;/*
|
* Author: Marc Tismonar
|
||||||
*-----------------------------------------------------------------------------
|
*-----------------------------------------------------------------------------
|
||||||
* Description:
|
* Description:
|
||||||
* Implements the merge sort strategy
|
* Implements the merge sort strategy
|
||||||
|
|
@ -11,3 +11,64 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "merge_sort.h"
|
#include "merge_sort.h"
|
||||||
|
|
||||||
|
static void merge_sort_list_merge(IntList list, IntList left, IntList right, criterion_fn criterion) {
|
||||||
|
while (list_get_size(left) > 0 && list_get_size(right) > 0) {
|
||||||
|
if (criterion(list_get_at(left, 0), list_get_at(right, 0))) {
|
||||||
|
list_transfer_head(left, list);
|
||||||
|
} else {
|
||||||
|
list_transfer_head(right, list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list_append(list, left);
|
||||||
|
list_append(list, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
static IntList merge_sort_list_divide_merge(IntList list, IntList result, criterion_fn criterion) {
|
||||||
|
if (list_get_size(list) <= 1) {
|
||||||
|
list_append(result, list);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
IntList left = list_obtain(), right = list_obtain();
|
||||||
|
IntList left_result = list_obtain(), right_result = list_obtain();
|
||||||
|
|
||||||
|
list_split(list, left, right, list_get_size(list) / 2);
|
||||||
|
merge_sort_list_divide_merge(left, left_result, criterion);
|
||||||
|
merge_sort_list_divide_merge(right, right_result, criterion);
|
||||||
|
merge_sort_list_merge(result, left_result, right_result, criterion);
|
||||||
|
|
||||||
|
list_release(&left);
|
||||||
|
list_release(&right);
|
||||||
|
list_release(&left_result);
|
||||||
|
list_release(&right_result);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the given list according to the merge sort strategy.
|
||||||
|
*
|
||||||
|
* @param list The list to be sorted.
|
||||||
|
* @param criterion The pointer to the function that implements the sorting criterion.
|
||||||
|
* That function accepts two integer parameters and returns a boolean value.
|
||||||
|
*/
|
||||||
|
void merge_sort_list(IntList list, criterion_fn criterion) {
|
||||||
|
IntList result = list_obtain();
|
||||||
|
result = merge_sort_list_divide_merge(list, result, criterion);
|
||||||
|
list_clear(list);
|
||||||
|
list_append(list, result);
|
||||||
|
list_release(&result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the given array according to the merge sort strategy.
|
||||||
|
*
|
||||||
|
* @param array The array to be sorted.
|
||||||
|
* @param length The length of the array.
|
||||||
|
* @param criterion The pointer to the function that implements the sorting criterion.
|
||||||
|
* That function accepts two integer parameters and returns a boolean value.
|
||||||
|
*/
|
||||||
|
void merge_sort_array(int array[], int length, criterion_fn criterion) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
* ---------------------------------------------------------
|
* ---------------------------------------------------------
|
||||||
* Exercise Number: S06
|
* Exercise Number: S06
|
||||||
* Title: Insertion sort implementation
|
* Title: Insertion sort implementation
|
||||||
* Author: */<your name>;/*
|
* Author: Marc Tismonar
|
||||||
* ----------------------------------------------------------
|
* ----------------------------------------------------------
|
||||||
* Description:
|
* Description:
|
||||||
* Implements the merge sort strategy
|
* Implements the merge sort strategy
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
* @param criterion The pointer to the function that implements the sorting criterion.
|
* @param criterion The pointer to the function that implements the sorting criterion.
|
||||||
* That function accepts two integer parameters and returns a boolean value.
|
* That function accepts two integer parameters and returns a boolean value.
|
||||||
*/
|
*/
|
||||||
void merge_sort_list(<params>);
|
void merge_sort_list(IntList list, criterion_fn criterion);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sorts the given array according to the merge sort strategy.
|
* Sorts the given array according to the merge sort strategy.
|
||||||
|
|
@ -34,6 +34,6 @@ void merge_sort_list(<params>);
|
||||||
* @param criterion The pointer to the function that implements the sorting criterion.
|
* @param criterion The pointer to the function that implements the sorting criterion.
|
||||||
* That function accepts two integer parameters and returns a boolean value.
|
* That function accepts two integer parameters and returns a boolean value.
|
||||||
*/
|
*/
|
||||||
void merge_sort_array(<params>);
|
void merge_sort_array(int array[], int length, criterion_fn criterion);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue