Headline

The problem of sorting a given list

Description

The problem can be described, for example, as follows:

  • Input:
    • A list l of values
  • Output:
    • A list r satisfying these properties:
      • r is a permutation is of l.
      • r is sorted.
For instance, given the input {2,4,3,1,4}, the output should be {1,2,3,4,4}.

This is an algorithmic problem; there are various sorting algorithms, e.g., Quicksort.

Illustration

Sorting problem in Haskell

The ingredients of the above problem description, i.e., the test for two lists to be permutations of each other and the test for a list to be sorted, can be described more formally as follows; we use Language:Haskell notation:

-- Test for two lists to be permutations of each other
permutation :: Eq a => [a] -> [a] -> Bool
permutation [] [] = True
permutation (x:xs) ys = remove ys []
  where
    -- Repeat removal of equal elements
    remove [] _ = False
    remove (y:ys) zs =
      if (x==y)
        then permutation xs (zs++ys)
        else remove ys (y:zs)

-- Test for a list to be sort
sorted :: Ord a => [a] -> Bool
sorted [] = True
sorted [x] = True
sorted (x1:x2:xs) = x1 <= x2 && sorted (x2:xs)

We apply the list properties to a few sample lists:

-- Illustrate list properties
main = do
  let l1 = [2,4,3,1,4]
  let l2 = [1,2,3,4,4]
  let l3 = [1,8,2,7,4]
  print $ sorted l1 -- False
  print $ sorted l2 -- True
  print $ sorted l3 -- False
  print $ permutation l1 l2 -- True
  print $ permutation l1 l3 -- False


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.