lib in c that contains as many generalist sort functions as I can
As quicksort already exists in c for arrays I didn't make it
in this version I focused on just adding the most basic sorts to arrays,they are Insertion Sort, Bubble Sort, Heap Sort and Merge Sort
to use each of these functions we have to pass at least 4 parameters.
-
Here we need 4 parameters, the array you want to sort, the array size, the size of the array type and a int function that have 2 const void parameters, (this function will give you how your array will be ordered).
-
Sample:
#include <stdio.h>
#include "sort.h"
//Create the compar
int compar(const void* a, const void* b)
{
return *(int*) a - *(int*) b;
}
int main(void)
{
//Create the array
int arr[] = {2,6,14,25,13,25,46,85};
//get the num of objects
int arr_size = sizeof(arr)/sizeof(int);
//sort the array and take the number of steps that was necessary for that
long steps = isort(arr,arr_size ,sizeof(int), compar);
//print the number of steps
printf("%d\n", steps);
//print sorted array
for(int i = 0; i < arr_size; i++)
{
printf("%d ", arr[i]);
}
}
-
Here we need 5 parameters, the array, the size of the array type, a int function that have 2 const void parameters, (this function will give you how your array will be ordered), 0 and the array size - 1 (also works with the array size but it takes more steps to complete)
-
Sample:
#include <stdio.h>
#include "sort.h"
//Create the compar
int compar(const void* a, const void* b)
{
return *(int*) a - *(int*) b;
}
int main(void)
{
//Create the array
int arr[] = {2,6,14,25,13,25,46,85};
//get the num of objects
int arr_size = sizeof(arr)/sizeof(int);
//sort the array and take the number of steps that was necessary for that
long steps = msort(arr ,sizeof(int), compar,0,arr_size - 1);
//print the number of steps
printf("%d\n", steps);
//print sorted array
for(int i = 0; i < arr_size; i++)
{
printf("%d ", arr[i]);
}
}
- implements basic sorts for array
- implements basic sorts for lists
- implements basic sorts for trees
- implements basic sorts for files
- implements more sorts
MIT © Arthur Scanoni