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

15 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
  4. Fantastic!! you are doing good job! I impressed. Many bodies are follow to you and try to some new.. After read your comments I feel; Its very interesting and every guys sahre with you own works. Great!!
    vé máy bay đi trương gia giới

    vé máy bay từ tphcm đi đài loan

    vé máy bay tphcm đi taipei

    vé máy bay giá rẻ đi cao hùng

    ReplyDelete
  5. The arrangements and administrations that custom ERP programming advancement is intended to give, is typically not in box along these lines, it requires a ton of time and funds to introduce the plan coming about to high forthright charge. media pitch

    ReplyDelete
  6. I have been surfing online for more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. Personally, sagame
    ufa

    ufabet

    ReplyDelete
  7. Watch movies online sa-movie.com, watch new movies, series Netflix HD 4K, watch free movies on your mobile phone, Tablet, watch movies on the web. ดูหนังออนไลน์


    SEE4K Watch movies, watch movies, free series, load without interruption, sharp images in HD FullHD 4k, all matters, all tastes, see anywhere, anytime, on mobile phones, tablets, computers. ดูหนังใหม่


    GangManga read manga, read manga, read manga online for free, fast loading, clear images in HD quality, all titles, anywhere, anytime, on mobile, tablet, computer. อ่านการ์ตูน


    Watch live football live24th, watch football online, link to watch live football, watch football for free. ผลบอลสดa

    ReplyDelete