0

Given a randomly ordered array of n elements partition the elements into two subsets such that elements less than x are in one subset and elements greater than x are in separate subset. For instance, [28, 26, 25, 11, 16, 12, 24, 29, 6, 10] and x = 17 lead to the array [10, 6, 12, 11, 16, 25, 24, 29, 26, 28].

I thought of an algorithm as follows:

  • Establish the array a[0,...,n] and the partitioning value x.

  • Move the partitions towards each other until the wrongly placed elements are encountered. Allow for special cases such as x being outside the range of array values.

  • While the two partitions have not crossed over exchange the wrongly partitioned pair and extend both partitions inward by one element

  • extend left partition while elements less than or equal to x

  • extend the right partition while elements are greater than x

  • Return the partitioning index p in the partitioned array.

kopecs
  • 1,545
  • 10
  • 20
  • Do you have an `array` or a `list`? – kopecs Mar 26 '20 at 22:20
  • I believe you forgot to ask a question. Also, that looks like the partitioning step of Quicksort. – molbdnilo Mar 27 '20 at 06:49
  • Yes, the "question" part of "Q&A" is kind of missing here. This is an assignment. You may either use or get inspired by the built-in [`List.partition`](https://smlfamily.github.io/Basis/list.html#SIG:LIST.partition:VAL) function, depending on the restrictions of your assignment (how much of the internals of the function you're supposed to make yourself). – sshine Mar 27 '20 at 16:01

0 Answers0