Simple selection
Unstable
Worst Time: O (N)
Average time: O (N)
Best time: O (N)
Space: O (1)
#include <stdio.h>#define Swap(x,y,t) ((t)=(x),(x)=(y),(y)=(t))#define MaxSize 100typedef struct list {int Size,MaxList;int Elements[MaxSize];}List;List CreateList(int n,int max) {List ls ;int i ;ls.Size=n;ls.MaxList=max;for(i=0;i<n;i++) {scanf("%d",&ls.Elements[i]);}return ls;}void SelectSort(List *list) {int small,i,j,temp;for(i=0;i<list->Size-1;i++) {small=i;for(j=i+1;j<list->Size;j++) {if(list->Elements[j]<list->Elements[small])small=j;}if(small!=i)Swap(list->Elements[i],list->Elements[small],temp);}}void PrintList(List list) {int i ; for(i=0;i<list.Size;i++) {printf("%d ",list.Elements[i]);}}void main() {int n ,maxList;List list;while(scanf("%d%d",&n,&maxList)!=EOF) {list=CreateList(n,maxList);SelectSort(&list);PrintList(list);}}
Heap sorting
Unstable
Worst Time: O (nlogn) [The heap is a complete binary tree. The execution time of the downward adjustment function adjustdown cannot exceed O (logn). Therefore, the worst time for heap creation is O (logn ), the worst time of the operation is O (n). ajustdown is called every time a record is output, so the execution time O (nlogn).]
Average time: O (nlogn)
Best time: O (nlogn)
Space: O (1) [only one record variable temp is required for exchanging two records, and no additional record space is required]
# Include <stdio. h> # define maxsize 100 # define swap (X, Y, t) (t) = (x), (x) = (y), (y) = (t) typedef int t; typedef struct minheap {int size, maxheap; t elements [maxsize] ;}minheap; void adjustdown (T heap [], int R, int N) {int child = 2 * r; t temp = heap [R]; while (Child <= N) {If (Child <n & heap [child]> heap [Child + 1]) Child ++; If (temp <= heap [child]) break; heap [Child/2] = heap [child]; child * = 2;} heap [Child/2] = temp;} void heapsort (minheap * HP) {int I; int temp; for (I = hp-> size/2; I> 0; I --) {// create a heap adjustdown (HP-> elements, I, HP-> size) ;}for (I = hp-> size; I> 1; I --) {// select the sorting printf ("% d ", HP-> elements [1]); swap (HP-> elements [1], HP-> elements [I], temp); adjustdown (HP-> elements, 1, i-1);} printf ("% d \ n", HP-> elements [1]);} // 2th method/* void reaheap (T * heap, int N) {int parent = 0, end = N, child = 1, right, temp; while (Child <End) {right = Child + 1; if (right <End) & (heap [right]
[Sorting within eight data structures] Select sorting (Simple selection, heap sorting)