Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:06:04+00:00 2026-05-26T12:06:04+00:00

I need to implement quicksort in SML for a homework assignment, and I’m lost.

  • 0

I need to implement quicksort in SML for a homework assignment, and I’m lost. I was previously unfamiliar with how quicksort was implemented, so I read up on that, but every implementation I read about was in an imperative one. These don’t look too difficult, but I had no idea how to implement quicksort functionally.

Wikipedia happens to have quicksort code in Standard ML (which is the language required for my assignment), but I don’t understand how it’s working.

Wikipedia code:

val filt = List.filter
fun quicksort << xs = let
fun qs [] = []
 | qs [x] = [x]
 | qs (p::xs) = let
     val lessThanP = (fn x => << (x, p))
     in
       qs (filt lessThanP xs) @ p :: (qs (filt (not o lessThanP) xs))
     end
in
  qs xs
end

In particular, I don’t understand this line: qs (filt lessThanP xs) @ p :: (qs (filt (not o lessThanP) xs)). filt will return a list of everything in xs less than p*, which is concatenated with p, which is cons-ed onto everything >= p.*

*assuming the << (x, p) function returns true when x < p. Of course it doesn’t have to be that.

Actually, typing this out is helping me understand what’s going on a bit. Anyways, I’m trying to compare that SML function to wiki’s quicksort pseudocode, which follows.

function quicksort(array, ‘left’, ‘right’)

  // If the list has 2 or more items
  if 'left' < 'right'

      // See "Choice of pivot" section below for possible choices
      choose any 'pivotIndex' such that 'left' ≤ 'pivotIndex' ≤ 'right'

      // Get lists of bigger and smaller items and final position of pivot
      'pivotNewIndex' := partition(array, 'left', 'right', 'pivotIndex')

      // Recursively sort elements smaller than the pivot
      quicksort(array, 'left', 'pivotNewIndex' - 1)

      // Recursively sort elements at least as big as the pivot
      quicksort(array, 'pivotNewIndex' + 1, 'right')

Where partition is defined as

// left is the index of the leftmost element of the array
// right is the index of the rightmost element of the array (inclusive)
//   number of elements in subarray = right-left+1
function partition(array, 'left', 'right', 'pivotIndex')
  'pivotValue' := array['pivotIndex']
  swap array['pivotIndex'] and array['right']  // Move pivot to end
  'storeIndex' := 'left'
  for 'i' from 'left' to 'right' - 1  // left ≤ i < right
      if array['i'] < 'pivotValue'
          swap array['i'] and array['storeIndex']
          'storeIndex' := 'storeIndex' + 1
  swap array['storeIndex'] and array['right']  // Move pivot to its final place
  return 'storeIndex'

So, where exactly is the partitioning happening? Or am I thinking about SMLs quicksort wrongly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:06:04+00:00Added an answer on May 26, 2026 at 12:06 pm

    So, where exactly is the partitioning happening? Or am I thinking about SMLs quicksort wrongly?

    A purely functional implementation of quicksort works by structural recursion on the input list (IMO, this is worth mentioning). Moreover, as you see, the two calls to “filt” allow you to partition the input list into two sublists (say A and B), which then can be treated individually. What is important here is that:

    • all elements of A are less than or equal to the pivot element (“p” in the code)
    • all elements of B are greater than the pivot element

    An imperative implementation works in-place, by swapping elements in the same array. In the pseudocode you’ve provided, the post-invariant of the “partition” function is that you have two subarrays, one starting at ‘left’ of input array (and ending at ‘pivotIndex’), and another starting right after ‘pivotIndex’ and ending at ‘right’. What is important here is that the two subarrays can be seen as representations of the sublists A and B.

    I think that by now, you have the idea where the partitioning step is happening (or conversely, how the imperative and functional are related).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement some crypto protocol on C# and want to say that
I need to implement a Remember me button in a login form that uses
Need to implement functionality similar to what dotdotdot jQuery plugin does but cannot use
I need to implement a read only property on my type. Moreover the value
I'm learning threads yet, but don't know much things. I see that I need
I need to implement two rank queries [ rank(k) and select(r) ]. But before
I need to implement a Forgot Password page, but my passwords are salted &
I need to implement validation for a form but by two submit buttons? Can
I need to implement a shell script that kills a process. The problem is
I need to implement multilevel expandable list view, something like ExpandableListView but with more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.