Using a selection sort to sort an array in python. How can I optimize?

℡╲_俬逩灬. 提交于 2019-12-24 18:43:11

问题


Working on this challenge on HackerRank and got this code to pass 10 out of 15 test cases. It is failing due to timeout error which is HackerRank's way of telling you that the algorithm is not optimized. How can I optimize this code to run on larger input data?

The goal is to figure out the minimum number of swaps necessary to sort an unsorted array.

Update: Each element in the array is distinct.

def minimum_swaps(arr):
"""Returns the minimum number of swaps to re-oder array in ascending order."""

    swaps = 0
    for val in range(len(arr) - 1, 0, -1):

        # Index of max value
        max_pos = 0
        for index in range(1, val + 1):

            if arr[index] > arr[max_pos]:
                max_pos = index

        # Skip if value is already in sorted position
        if max_pos == val:
            continue

        arr[val], arr[max_pos] = arr[max_pos], arr[val]
        swaps += 1

    return swaps

回答1:


Look at the code. It has 2 nested loops:

  • The outer loop iterates over the positions val.
  • The inner loop finds the index of the value that should be at the index val, i.e., max_pos.

It takes a lot of time just to find the index. Instead, I will compute the index of each value and store it in a dict.

index_of = {value: index for index, value in enumerate(arr)}

(note that because all values in arr are distinct, there should be no duplicated keys)

And also prepare a sorted version of the array: that way it's easier to find the maximum value instead of having to loop over the array.

sorted_arr = sorted(arr)

Then do the rest similar to the original code: for each index visited, use sorted_arr to get the max, use index_of to get its current index, if it's out-of-place then swap. Remember to update the index_of dict while swapping too.

The algorithm takes O(n) operations (including dict indexing/modifying), plus sorting cost of n elements (which is about O(n log n)).


Note: If the array arr only contains integers in a small range, it may be faster to make index_of an array instead of a dict.




回答2:


The short answer is: implement merge sort. The bubble sort algorithm you are using has a O(n^2) running time, while merge sort has a O(log_2(n)) running time.



来源:https://stackoverflow.com/questions/52232880/using-a-selection-sort-to-sort-an-array-in-python-how-can-i-optimize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!