📜  minimumm sort 尝试过的python(1)

📅  最后修改于: 2023-12-03 15:17:39.653000             🧑  作者: Mango

Minimum Sort in Python

Minimum Sort, also known as Selection Sort, is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning.

Algorithm
  1. Set the first position as the minimum value
  2. Traverse the array from the second position to the end
  3. If the current value is less than the minimum value, set the current value as the new minimum value
  4. Swap the minimum value with the value in the first position
  5. Repeat steps 1-4 for the remaining unsorted part of the array
Python Implementation
def minimum_sort(arr):
    n = len(arr)
    for i in range(n-1):
        min_index = i
        for j in range(i+1, n):
            if arr[j] < arr[min_index]:
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]
    return arr
Example
arr = [5, 2, 8, 3, 1]
print(minimum_sort(arr))

Output:

[1, 2, 3, 5, 8]
Performance
  • Time complexity: O(n^2)
  • Space complexity: O(1)

Minimum Sort is a simple sorting algorithm that can be used for small arrays, but its time complexity makes it inefficient for large arrays or performance-critical applications. Other sorting algorithms, such as Quick Sort or Merge Sort, have better time complexity and are preferred in most cases.