📅  最后修改于: 2020-10-15 03:13:32             🧑  作者: Mango
在选择排序中,每次通过都会选择数组中未排序元素中的最小值,并将其插入数组中的适当位置。
首先,找到数组的最小元素并将其放在第一个位置。然后,找到数组的第二个最小元素并将其放在第二个位置。这个过程一直持续到我们得到排序数组为止。
使用选择排序算法的n-1遍对具有n个元素的数组进行排序。
因此,通过遵循上述过程,对元素A [0],A [1],A [2],…,A [n-1]进行排序。
考虑以下包含6个元素的数组。通过使用选择排序对数组的元素进行排序。
A = {10,2,3,90,43,56}。
Pass | Pos | A[0] | A[1] | A[2] | A[3] | A[4] | A[5] |
---|---|---|---|---|---|---|---|
1 | 1 | 2 | 10 | 3 | 90 | 43 | 56 |
2 | 2 | 2 | 3 | 10 | 90 | 43 | 56 |
3 | 2 | 2 | 3 | 10 | 90 | 43 | 56 |
4 | 4 | 2 | 3 | 10 | 43 | 90 | 56 |
5 | 5 | 2 | 3 | 10 | 43 | 56 | 90 |
排序的A = {2,3,10,43,56,90}
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time | Ω(n) | θ(n2) | o(n2) |
Space | o(1) |
选择排序(ARR,N)
最小(ARR,K,N,POS)
#include
int smallest(int[],int,int);
void main ()
{
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
printf("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
int smallest(int a[], int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
#include
using namespace std;
int smallest(int[],int,int);
int main ()
{
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
cout<<"\n printing sorted elements...\n";
for(i=0;i<10;i++)
{
cout<
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
public class SelectionSort {
public static void main(String[] args) {
int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,j,k,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
System.out.println("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
public static int smallest(int a[], int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
using System;
public class Program
{
public void Main() {
int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int i,pos,temp;
for(i=0;i<10;i++)
{
pos = smallest(a,10,i);
temp = a[i];
a[i]=a[pos];
a[pos] = temp;
}
Console.WriteLine("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
{
Console.WriteLine(a[i]);
}
}
public static int smallest(int[] a, int n, int i)
{
int small,pos,j;
small = a[i];
pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
def smallest(a,i):
small = a[i]
pos=i
for j in range(i+1,10):
if a[j] < small:
small = a[j]
pos = j
return pos
a=[10, 9, 7, 101, 23, 44, 12, 78, 34, 23]
for i in range(0,10):
pos = smallest(a,i)
temp = a[i]
a[i]=a[pos]
a[pos]=temp
print("printing sorted elements...")
for i in a:
print(i)
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
fn main ()
{
let mut a: [i32;10] = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
for i in 0..10
{
let mut small = a[i];
let mut pos = i;
for j in (i+1)..10
{
if a[j]
输出:
printing sorted elements...
7
9
10
12
23
23
34
44
78
101
Selection Sort
输出:
printing sorted elements ...
7
9
10
12
23
23
34
44
78
101
Selection sort
输出:
printing sorted elements ...
7
9
10
12
23
23
34
44
78
101