CodeFights - Simple Sort - Solution

Problem
To understand how efficient the built-in Python sorting function is, you decided to implement your own simple sorting algorithm and compare its speed to the speed of the Python sorting. Write a function that, given an array of integers arr, sorts its elements in ascending order.
Hint: with Python it's possible to swap several elements in a single line. To solve the task, use this knowledge to fill in both of the blanks (...).
Example
For arr = [2, 4, 1, 5], the output should be
simpleSort(arr) = [1, 2, 4, 5].
Input/Output
  • [execution time limit] 4 seconds (py)
  • [input] array.integer arr
    Guaranteed constraints:
    1 ≤ arr.length ≤ 100,
    -105 ≤ arr[i] ≤ 105.
  • [output] array.integer
    The given array with elements sorted in ascending order.
Solution

We just have to swap the elements which are out of order. The line in bold does just that. Its called tuple unpacking.


def simpleSort(arr):

    n = len(arr)

    for i in range(n):
        j = 0
        stop = n - i
        while j < stop - 1:
            if arr[j] > arr[j + 1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
            j += 1
    return arr

5 comments:

  1. InstantPcApps
    It's a hub for free mac, pc, android and ios software. You can get any kind of game, software and mobile application free of cost. instantpcapps.com

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. Array types are often implemented by array data structures, but sometimes by other means, such as hash tables, linked lists, or search trees.

    internship in chennai for mechanical
    internship in chennai for cse students
    internship in chennai for eee
    internship in chennai for ece students
    internship in chennai for bcom students
    internship in chennai for mechanical engineering students
    python internship in chennai
    internship in chennai for it students
    companies offering internship in chennai
    internship in chennai for it

    ReplyDelete
  3. I agree with a lot of the points you made in this article. If you are looking for the Array Interview Questions, then visit TutorialCup. I love your content, they are very nice and very useful to us and this text is worth everyone’s attention.

    ReplyDelete