C examples for Data Structure:Sort
Shell Sort on numbers read from a file
#include <stdio.h> #include <stdlib.h> #define MaxSize 100//from w w w . ja v a 2 s . co m void hsort(int[], int, int); int main() { int num[MaxSize + 1]; int n = 0, number; FILE * in = fopen("shell.in", "r"); int incr[] = {3, 8, 3, 1}; //first 3 is the number of increments while (fscanf(in, "%d", &number) == 1) { if (n < MaxSize) num[++n] = number; else { printf("\nArray too small\n"); exit(1); } } for (int i = 1; i <= incr[0]; i++) hsort(num, n, incr[i]); for (int h = 1; h <= n; h++) printf("%d ", num[h]); fclose(in); } void hsort(int A[], int n, int h) { for (int k = h + 1; k <= n; k++) { int j = k - h; //j will index elements k - h, k - 2h, k - 3h, etc int key = A[k]; while (j > 0 && key < A[j]) { A[j + h] = A[j]; j = j - h; } A[j + h] = key; } }