# Insertion sort in an array

**URL:** <https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434>\
**Category:** Learning\
**Created:** [August 11, 2018, 4:31pm UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434 "2018-08-11T16:31:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![superlativ27](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@superlativ27](https://discuss.ocaml.org/u/superlativ27)\
**Post date:** [August 11, 2018, 4:31pm UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434/1 "2018-08-11T16:31:42Z")

</div>

I have created a help function (insertinorder) inorder for the function insertionsort to go through all of the elements, but I can’t still get the functions working correct.

Can someone assist me in what I am doing wrong?

```
     let rec insertinorder(array,end) =
        let temp = array.(end) in
        if array.(end-1) > temp
        then array.(end) = array.(end-1)
        array.(end-1) = temp
        if end > 1
        then insertinorder(array,end-1)
        else ();;

    let rec insertionsort (array, current) =
    if current > array.length-1
    then ()
    else insertinorder(array, current)
    insertionsort(array, current +1);;
```

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [August 11, 2018, 5:13pm UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434/2 "2018-08-11T17:13:06Z")

</div>

As a zeroth step, you should try to make your code compile (`end` is a keyword).

---

<div class="post-metadata">

**Author:** ![superlativ27](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@superlativ27](https://discuss.ocaml.org/u/superlativ27)\
**Post date:** [August 11, 2018, 5:14pm UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434/3 "2018-08-11T17:14:51Z")

</div>

> [@octachron](#):
>
> zeroth step,

What is zeroth step?

How would it be compiled?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [August 11, 2018, 5:26pm UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434/4 "2018-08-11T17:26:45Z")

</div>

> [@superlativ27](#):
>
> What is zeroth step?

It is the minimal step that you should have taken _before_ asking your question.

> [@superlativ27](#):
>
> How would it be compiled?

How are you even working? Are you writing code on paper in pseudo-code? Anyway, write your code in the REPL or compile it with `ocamlc` or `ocamlopt` and get rid of your parser and type errors, then update your question.

Hint: you should also use `-strict-sequence`.

---

<div class="post-metadata">

**Author:** ![superlativ27](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@superlativ27](https://discuss.ocaml.org/u/superlativ27)\
**Post date:** [August 12, 2018, 8:30am UTC](https://discuss.ocaml.org/t/insertion-sort-in-an-array/2434/5 "2018-08-12T08:30:04Z")

</div>

Nevermind, I solved it.
