1.插入排序:
bool Insert(List L, ElementType X) {
if (L == NULL)return false;
if (L->Last == MAXSIZE - 1)return false;
for (int i = 0; i <= L->Last; i++)
if (L->Data[i] == X)return false;
int i = 0;
for(;i<L->Last+1;i++)
if (X > L->Data[i]) {
for (int j = L->Last; j >= i; j--)
L->Data[j + 1] = L->Data[j];
break;
}
else if (X == L->Data[i])return false;
L->Data[i] = X;
L->Last++;
return true;
}
2.选择排序(快排)
int partition(SqList &L,int low,int hight){
int pivotkey = L.r[low].key;
while(low<high){
while(low<high&&L.r[high].key>=pivotkey) --high;
swap(L.r[low],L.r[high]);
while(low<high&&L.r[low].key<=pivotkey) ++low;
swap(L.r[low],L.r[high]);
}
return low;
}
void Qsort(SqList &L,int low ,int high){
if(low<high){
pivotlc = partition(L,low,high);
Qsort(L,low,pivotlc-1);
Qsort(L,pivolc+1,high);
}
}
3.选择排序(堆排)
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 10
typedef struct MyStruct
{
int arr[10]={-1,8,7,1,5,3,9,2,4,6};
int size = MAXSIZE;
}heap;
heap h1;
void adjust(int last) {
for (int i = last; i >= 1; i--) {
int j = i;
int temp = h1.arr[i];
for (; j >= 2 && h1.arr[j/2] < h1.arr[j]; j /= 2)
h1.arr[j] = h1.arr[j/2];
h1.arr[j] = temp;
for (int i = 0; i <MAXSIZE; i++)
printf("%d,", h1.arr[i]);
printf("\n");
}
}
int main() {
adjust(MAXSIZE-1);
printf("break\n");
for (int i = MAXSIZE - 1; i >= 1; i--) {
int t = h1.arr[i];
h1.arr[i] = h1.arr[1];
h1.arr[1] = t;
adjust(i-1);
printf("num %d adj finished\n", MAXSIZE - i);
}
for (int i = 0; i <MAXSIZE; i++)
printf("%d,", h1.arr[i]);
return 0;
}
4.归并排序
#include <stdio.h>
#define ElementType int
#define MAXN 100
void merge_pass( ElementType list[], ElementType sorted[], int N, int length );
void output( ElementType list[], int N )
{
int i;
for (i=0; i<N; i++) printf("%d ", list[i]);
printf("\n");
}
void merge_sort( ElementType list[], int N )
{
ElementType extra[MAXN]; /* the extra space required */
int length = 1; /* current length of sublist being merged */
while( length < N ) {
merge_pass( list, extra, N, length ); /* merge list into extra */
output( extra, N );
length *= 2;
merge_pass( extra, list, N, length ); /* merge extra back to list */
output( list, N );
length *= 2;
}
}
int main()
{
int N, i;
ElementType A[MAXN];
scanf("%d", &N);
for (i=0; i<N; i++) scanf("%d", &A[i]);
merge_sort(A, N);
output(A, N);
return 0;
}
void merge_pass(ElementType list[], ElementType sorted[], int N, int length) {
int temp=0;
int pos = 0;
while (pos < N) {
int left = pos;
int right = pos + length;
while (left < pos + length&&right < pos + 2 * length&&left < N&&right < N) {
if (list[left] < list[right])
sorted[temp++] = list[left++];
else
sorted[temp++] = list[right++];
}
while (left < pos + length&&left < N)
sorted[temp++] = list[left++];
while (right < pos + 2 * length&&right < N)
sorted[temp++] = list[right++];
pos = pos + length * 2;
}}
5.
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//低位优先
//测试数据 278 109 63 930 589 184 505 269 8 83
int main() {
int n = 0;
int max = 0;
cout << "输入排序元素的个数:" << endl;
cin >> n;
vector<int>input(n);
cout << "输入n个元素:" << endl;
for (int i=0; i < n; i++) {
cin >> input[i];
if (input[i] > max)
max = input[i];
}
string s = to_string(max);
int flag = 1;
for (int i = 0; i < s.length(); i++) {
vector<vector<int>>bucket(10);
for (int j = 0; j < n; j++) {
int tmp = input[j];
tmp = (tmp / flag) % 10;
bucket[tmp].push_back(input[j]);
}
int t = 0;
for (int j = 0; j < 10; j++) {
for (int k = 0; k < bucket[j].size(); k++)
input[t++] = bucket[j][k];
}
flag *= 10;
}
for (int i=0; i < n; i++)
cout << input[i] << " ";
return 0;
}