RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
shell_sort.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
3  * 2020 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser General
6  * Public License v2.1. See the file LICENSE in the top level directory for more
7  * details.
8  */
9 
23 #include <stdio.h>
24 #include "utils.h"
25 #include "vector.h"
26 
27 /* Shell Sort Program */
28 void int_shell_sort(int *array, int length)
29 {
30  int n = length;
31  int i, j, gap, temp;
32 
33  for (gap = n / 2; gap > 0; gap /= 2) {
34  for (i = gap; i < n; i++) {
35  temp = array[i];
36  for (j = i; j >= gap; j -= gap) {
37  if (temp < array[j - gap]) {
38  array[j] = array[j - gap];
39  }
40  else {
41  break;
42  }
43  }
44  array[j] = temp;
45  }
46  }
47 }
48 
49 void shell_sort(vector_t *arr, uint8_t length)
50 {
51  int32_t j, k;
52  vector_t temp;
53 
54  for (int32_t i = length / 2; i > 0; i = i / 2) {
55  for (j = i; j < length; j++) {
56  temp = arr[j];
57  for (k = j - 1; k >= 0 && arr[k] > temp; k--) {
58  arr[k + 1] = arr[k];
59  }
60  arr[k + 1] = temp;
61  }
62  }
63 }
vector_t
#define vector_t
Define the data type of the vector elements.
Definition: vector.h:33
shell_sort
void shell_sort(vector_t *arr, uint8_t length)
Sort a data set of type utils_t by using the Shell sort algorithm.
Definition: shell_sort.c:49
utils.h
Utilities for linear algebra.
int_shell_sort
void int_shell_sort(int *array, int length)
Sort a data set of integers by using the Shell sort algorithm.
Definition: shell_sort.c:28
vector.h
Vector computations.