# Confused as to how I could implement a sorting algorithm in Processing

**URL:** <https://discourse.processing.org/t/confused-as-to-how-i-could-implement-a-sorting-algorithm-in-processing/9708>\
**Category:** Coding Questions\
**Created:** [March 28, 2019, 3:36am UTC](https://discourse.processing.org/t/confused-as-to-how-i-could-implement-a-sorting-algorithm-in-processing/9708 "2019-03-28T03:36:45Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 28, 2019, 11:10am UTC](https://discourse.processing.org/t/confused-as-to-how-i-could-implement-a-sorting-algorithm-in-processing/9708/3 "2019-03-28T11:10:30Z")

</div>

> [@Xevion](#):
>
> This worked fine, but only for _bubble sort._ And it seemed to be limited by the fact that if I wanted a different algorithm, I would need checks for what ‘stage’ of the algorithm I’m at, and if I wanted a different array, if I wanted to be able to ‘rewind’ or ‘pause’ I would encounter problems, making the whole process super difficult to maintain.

> [@Xevion](#):
>
> I kind of wish I was just able to do a draw tick while inside of a loop, being able to just call display(), sleep 0.01 seconds and then the loop continues, but unfortunately, no such thing.

This has been discussed before so you might read this [discussion](https://discourse.processing.org/t/bubblesort-visualization-want-to-slow-it-down/6742)

Most sort algorithms have two key activities

1. Compare two elements in the collection to sort
2. Swap two elements in the collection if in the wrong order.

One possible solution is to remember the elements being compared / swapped in another data structure e.g. a list and then iterate forwards or backwards displaying the current state of the collection.

This is the solution I have used in the sketch shown in the linked discussion.

---

_[View the full topic](https://discourse.processing.org/t/confused-as-to-how-i-could-implement-a-sorting-algorithm-in-processing/9708)._
