(1)简单选择排序:
算法思想:
1.每一趟排序,找到待排序序列中关键字最小的数据元素
2.将其与待排序序列中第一个数据元素交换位置
3.并将其从下一趟待排序列中移出
4.重复该过程,直到某趟排序时排序序列中仅剩下两个数据元素
算法分析:
该算法必须进行n-1趟,每趟进行n-i-1次比较,总的比较次数:n(n-1)/2
稳定性:不稳定
时间复杂度:任何情况O(n^2)
/*简单选择排序*/
#include<stdio.h>
#define MAXSIZE 10
typedef struct{
int key;
}Sqnote;
typedef struct
{
Sqnote s[MAXSIZE];
int length;
}SqList;
void Swap(Sqnote *a,Sqnote *b)//换位函数
{
int temp = a->key;
a->key = b->key;
b->key = temp;
}
int FindMin(SqList L,int startIndex)//找最小值函数
{
int i, minIndex = startIndex;
for (i = startIndex + 1; i < L.length; i++)
{
if (L.s[i].key < L.s[minIndex].key)
{
minIndex = i;
}
}
return minIndex;
}
void SelectSort(SqList *L)//选择排序函数
{
int minIndex, startIndex = 0;
while (startIndex<L->length)
{
minIndex = FindMin(*L,startIndex);
Swap(&(L->s[startIndex]), &(L->s[minIndex]));
startIndex++;
}
}
int main()
{
SqList L;
int n;
scanf("%d", &n);
L.length = n;
printf("input:");
for (int i = 0; i<L.length; i++)
scanf("%d", &L.s[i].key);
SelectSort(&L);
for (int i = 0; i<L.length; i++)
printf("%d ", L.s[i]);
return 0;
}
/*
10
input:25 16 93 2 45 98 3 40 122 38
2 3 16 25 38 40 45 93 98 122
/*
(2)直接插入排序
(3)冒泡排序
(4)快速排序
(5)合并排序
(6)堆排序