0.Main /* ============================================================================ Name : c-algorithm.c Author : QiBaoyuan Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include "compare-int.h" #include "Sort.h" int main(void) { int p1 = 15; int p2 = 34; int p3 = 22; int p4 = 74; void* arrays[] = { &p1, &p2, &p3, &p4 }; printf("result is:%d", int_equal(&p1, &p2)); InsertSort(arrays, sizeof(arrays) / sizeof(void*)); int i = 0; for (i = 0; i < sizeof(arrays) / sizeof(void*); i++) { printf("/n%d ", (int) *((int*) arrays[i])); } return EXIT_SUCCESS; } 1.compare-int.h /* * compare-int.h * * Created on: 2010-7-10 * Author: qibaoyuan */ #ifndef COMPAREINT_H_ #define COMPAREINT_H_ #ifdef __cplusplus extern "C" { #endif /** * compare the integer values pointed by two pointers to determine * if they are equal * @param loc1 pointer to the first value to compare. * @param loc2 pointer to the second value to compare * return non-zero if the two value is equal,zero if they are equal */ int int_equal(void* loc1, void* loc2); /** *compare the integer valaue pointed by two pointers * @param loc1 pointer to the first value to compare. * @param loc2 pointer to the second value to compare * return a negetive value if the first value is less than the second one; * return zero if they are equal;return positive otherwise. */ int int_compare(void* loc1, void* loc2); #ifdef __cplusplus } #endif /*__cplusplus*/ #endif /* COMPAREINT_H_ */ 2.compare-int.c /* * compare-int.c * * Created on: 2010-7-10 * Author: qibaoyuan */ #include "compare-int.h" /** * compare the integer values pointed by two pointers to determine * if they are equal * @param loc1 pointer to the first value to compare. * @param loc2 pointer to the second value to compare * return non-zero if the two value is equal,zero if they are equal */ int int_equal(void* loc1, void* loc2) { int *vloc1 = (int*) loc1; int *vloc2 = (int*) loc2; return *vloc1 == *vloc2; } /** *compare the integer valaue pointed by two pointers * @param loc1 pointer to the first value to compare. * @param loc2 pointer to the second value to compare * return a negetive value if the first value is less than the second one; * return zero if they are equal;return positive otherwise. */ int int_compare(void* loc1, void* loc2) { int *vloc1 = (int*) loc1; int *vloc2 = (int*) loc2; if (*vloc1 < *vloc2) return -1; else if (*vloc1 > *vloc2) return 1; else return 0; } 3.Sort.h /* * Sort.h * * Created on: 2010-7-10 * Author: qibaoyuan */ #ifndef SORT_H_ #define SORT_H_ #ifdef __cplusplus extern "C" { #endif /** * InsertSortValue */ typedef void* InsertSortValue; /** * InsertSort Algorithm * @param arrays arrays to sort * @n the size of the array * return null(temp) */ void InsertSort(InsertSortValue arrays[], int n); #ifdef __cplusplus } #endif /*__cplusplus*/ #endif /* SORT_H_ */ 4.Sort.c /* * Sort.c * * Created on: 2010-7-10 * Author: qibaoyuan */ #include <stdio.h> #include <stdlib.h> #include "compare-int.h" #include "Sort.h" /** * InsertSort Algorithm * @param arrays arrays to sort * @n the size of the array * return null(temp) */ void InsertSort(InsertSortValue arrays[], int n) { if (n == 1) return; int j = 0;//outer loop indicator int i = 0;//inner loop indicator InsertSortValue key; for (j = 1; j < n; j++) { key = arrays[j];//put curr value to key i = j - 1;//the most recent value while (i >= 0 && int_compare(arrays[i], key) > 0) { arrays[i + 1] = arrays[i]; i = i - 1; } arrays[i + 1] = key; } for (i = 0; i < n; i++) { printf("%d ", (int) *((int*) arrays[i])); } }