赞
踩
选择排序算法有效地将元素排列成两个部分:已排序的子列表和未排序的子列表。这种方法简化了列表组织,提高了排序效率。在本文中,我们深入研究选择排序的机制,解释其对各种类型列表进行排序的过程和有效性。让我们探讨什么是选择排序以及它是如何工作的。
选择排序,也称为就地比较排序,是一种简单的排序算法。它的工作原理是重复查找最小元素并将其放置在正确的排序位置。
选择排序的工作原理是将列表分为两个子列表:
最初,已排序的子列表是空的,我们只有未排序的子列表。
在选择排序的每次迭代中,我们 –
我们重复上述步骤,直到得到一个已排序的列表,并且未排序的子列表中不再有任何元素。每次迭代后,已排序子列表的大小都会增加,而未排序子列表的大小会减少。
注意 –我们正在搜索最小的元素,因为我们想按升序对列表进行排序。如果我们想按降序对列表进行排序,我们也可以搜索最大的元素。
选择排序算法的流程图可以描述如下:
回想一下上面按身高顺序排列学生的示例。在这种情况下,我们可以按照以下步骤将学生按身高顺序排列:
现在,让我们考虑一下我们想要按升序对列表进行排序。以下是该算法将遵循的步骤:
为了更好地理解选择排序,请回想一下最初包含元素5、6、4、2 的列表。
对该列表进行排序的步骤包括 -
从上图,我们可以推断出关于选择排序算法的以下结论:
我们知道,要使用选择排序对 n 个元素的列表进行排序,我们需要执行n-1 次迭代。
以下是选择排序的算法——
- begin selectionSort(list)
- for i = 0 to sizeof(list) - 1
-
- minIndex = i;
-
- for j = i + 1 to sizeof(list)
-
- if list[j] < list[mid_index]
- minIndex = j;
- end if
-
- swap(list[minIndex], list[i])
-
- end for
-
- end for
- end selectionSort

现在让我们通过以下步骤来了解选择排序的算法-
现在我们已经了解了什么是选择排序,让我们看看如何通过代码实现选择排序算法。
- // Selection Sort in Java
- class SelectionSort
- {
- void selectionSort(int arr[])
- {
- int size = arr.length;
-
- // loop to iterate over the entire array
- for (int i = 0; i < size - 1; i++)
- {
- // set minIndex equal to the first unsorted element
- int minIndex = i;
-
- //iterate over unsorted sublist
- for (int j = i+1; j < size; j++)
- //helps in finding the minimum element
- if (arr[j] < arr[minIndex])
- minIndex = j;
-
- // swapping the minimum element with the element at minIndex to place it at its proper position
- int temp = arr[minIndex];
- arr[minIndex] = arr[i];
- arr[i] = temp;
- }
- }
- }

- # Selection sort in Python
-
- def selectionSort(arr):
-
- # loop to iterate over the array elements
- for i in range(len(arr)):
-
- # set min_index equal to the first unsorted element
- min_index = i
-
- # iterate over unsorted sublist
- for j in range(i+1, len(arr)):
-
- #helps in finding the minimum element
- if arr[min_index] > arr[j]:
- min_index = j
-
- # swapping the minimum element with the element at min_index to place it at its correct position
-
- arr[i], arr[min_index] = arr[min_index], arr[i]

- // Selection Sort in C++
- //function to swap elements
- void swap(int *xp, int *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- void selectionSort(int arr[], int size)
- {
- int i, j, minIndex;
- // loop to iterate over the entire array
- for (i = 0; i < size - 1; i++)
- {
- // set minIndex equal to the first unsorted element
- minIndex = i;
-
- //iterate over unsorted sublist
- for (j = i+1; j < size; j++)
-
- //helps in finding the minimum element
- if (arr[j] < arr[minIndex])
- minIndex = j;
-
- // swapping the minimum element with the element at minIndex to place it at its correct position
- swap(&arr[minIndex], &arr[i]);
- }
- }

- // Selection Sort in C++
- void selectionSort(int arr[], int size)
- {
- int i, j, minIndex;
- // loop to iterate over the entire array
- for (i = 0; i < size - 1; i++)
- {
- // set minIndex equal to the first unsorted element
- minIndex = i;
-
- //iterate over unsorted sublist
- for (j = i+1; j < size; j++)
-
- // helps in finding the minimum element
- if (arr[j] < arr[minIndex])
- minIndex = j;
-
- // swapping the minimum element with the element at minIndex to place it at its correct position
- // using std::swap() method for swapping
- swap(arr[minIndex], arr[i]);
- }
- }

- // Selection sort in JavaScript
- function swap(arr,xp, yp)
- {
- var temp = arr[xp];
- arr[xp] = arr[yp];
- arr[yp] = temp;
- }
- function selectionSort(arr)
- {
- var i, j, minIndex;
-
- // loop to iterate over the entire array
- for (i = 0; i < arr.length - 1; i++)
- {
- // set minIndex equal to the first unsorted element
- minIndex = i;
-
- // iterate over unsorted sublist
- for (j = i + 1; j < arr.length; j++)
- if (arr[j] < arr[minIndex])
- minIndex = j;
-
- // swapping the minimum element with the element at minIndex
- swap(arr, minIndex, i);
- }
- }

- // Selection sort in C#
- class ScalerTopics
- {
- //selection sort function
- static void SelectionSort(int[] arr)
- {
- int size = arr.Length;
- // loop to iterate over the entire array
- for (int i = 0; i < size - 1; i++)
- {
- // set minIndex equal to the first unsorted element
- int minIndex = i;
-
- //iterate over unsorted sublist
- for (int j = i + 1; j < size; j++)
- if (arr[j] < arr[minIndex])
- minIndex = j;
- // swapping the minimum element with the element at minIndex
- int temp = arr[minIndex];
- arr[minIndex] = arr[i];
- arr[i] = temp;
- }
- }
- }

为了更好地理解选择排序的时间复杂度,让我们将SelectionSort()函数分为以下部分:
设列表中元素的数量为n。现在,我们知道对于第 1 部分,循环将运行 n 次。
在第 1 部分中,交换发生的次数将等于 n,并且每次交换将花费常数时间。因此,对于第 1 部分,我们可以说时间复杂度为O(n)。
第 2 部分嵌套在第 1 部分中运行的循环内。此循环有助于搜索最小数字。在这部分下,进行n次比较,执行时间恒定。换句话说,每执行一次第 1 部分,第 2 部分就会执行 n 次。如果第 1 部分执行 n 次,则第 2 部分将执行n * n次。
由此,我们可以说选择排序算法的复杂度为O(n ^ 2)。
现在让我们讨论最佳、平均和最坏情况下的时间复杂度。
(n – 1) * (( n – 2) + (n – 3) + (n – 4) +….1 ),这将等于 n * (n – 1)/2 。而且,用 Big-Oh 表示法来说,这将是。
选择排序算法的空间复杂度为O(1)。这是因为我们使用了固定数量的变量,除了循环变量和辅助变量(包括 temp、size 和 minIndex)之外,我们不需要任何额外的内存空间。这意味着我们甚至可以在不使用任何额外变量的情况下对包含一千个元素的数组进行排序。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。