整理文件夹,去年数据结构写的,希尔和桶排没有找到,备份一下, 有时间添加详细算法流程
1.冒泡排序(Bubble Sort):
#include <stdio.h>
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Swap(int *a,int *b) {
int t = *a;
*a = *b;
*b = t;
return;
}
void Bubble_Sort(int a[],int s) {
for(int i = 0; i < s; ++i) {
int flag = 0;
for(int j = 0; j < s-i-1; ++j) {
if(a[j] > a[j+1]) {
Swap(&a[j],&a[j+1]);
flag = 1;
}
}
if(!flag) {
break;
}
}
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Bubble_Sort(a,s);
Pri(a,s);
return 0;
}
2.堆排序(Head Sort):
#include <stdio.h>
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Swap(int *a,int *b) {
int t = *a;
*a = *b;
*b = t;
return;
}
void AdjustHead(int a[],int i,int s) {
int fa = i;
int ch = fa*2+1;
while(ch < s) {
if(a[ch] < a[ch + 1] && ch + 1 < s) {
++ch;
}
if(a[fa] < a[ch]) {
Swap(&a[fa],&a[ch]);
fa = ch;
}
ch = ch * 2 + 1;
}
return;
}
void Head_Sort(int a[],int s) {
for(int i = s/2-1; i >= 0; --i) {
AdjustHead(a,i,s);
}
for(int i = s-1; i > 0; --i) {
Swap(&a[0],&a[i]);
AdjustHead(a,0,i);
}
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Head_Sort(a,s);
Pri(a,s);
return 0;
}
3.插入排序(Insertion Sort):
#include <stdio.h>
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Insert_Sort(int a[],int s) {
for(int i = 1; i < s; ++i) {
int t = a[i];
int j = i - 1;
while(j >= 0 && a[j] > t) {
a[j+1] = a[j];
--j;
}
a[j+1] = t;
Pri(a,s);
}
Pri(a,s);
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Insert_Sort(a,s);
Pri(a,s);
return 0;
}
4.归并排序(Merge Sort):
https://www.cnblogs.com/chengxiao/p/6194356.html
#include <stdio.h>
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Swap(int *a,int *b) {
int t = *a;
*a = *b;
*b = t;
return;
}
void merge(int a[],int l,int r,int mid) {
int t[r-l+1];
for(int k = l; k <= r; ++k) {
t[k-l] = a[k];
}
int i = l,j = mid+1;
for(int k = l; k <= r; ++k) {
if(i > mid) {
a[k] = t[j-l];
++j;
}
else if(j > r) {
a[k] = t[i-l];
++i;
}
else if(t[i-l] > t[j-l]) {
a[k] = t[j-l];
++j;
}
else if(t[i-l] <= t[j-l]) {
a[k] = t[i-l];
++i;
}
}
return;
}
void Merge_Sort(int a[],int l,int r) {
if(l < r) {
int mid = (l+r)/2;
Merge_Sort(a,l,mid);
Merge_Sort(a,mid+1,r);
merge(a,l,r,mid);
}
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Merge_Sort(a,0,s-1);
Pri(a,s);
return 0;
}
5.选择排序(Select-Sort):
#include <stdio.h>
const int INF = 0x3f3f3f3f;
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Swap(int *a,int *b) {
int t = *a;
*a = *b;
*b = t;
return;
}
void Select_Sort(int a[],int s) {
int mi = 0;
for(int i = 0; i < s - 1; ++i) {
for(int j = i; j < s; ++j) {
mi = a[j] < a[mi] ? j : mi;
}
Swap(&a[i],&a[mi]);
}
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Select_Sort(a,s);
Pri(a,s);
return 0;
}
6.快速排序(Quick Sort):
数组:
#include <stdio.h>
void Pri(int a[],int s) {
for(int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
return;
}
void Quick_Sort(int a[],int l,int r) {
if(l < r) {
int i = l,j = r;
int t = a[r];
while(i < j) {
while(a[i] <= t && i < j)
++i;
if(i < j)
a[j--] = a[i];
while(a[j] >= t && i < j)
--j;
if(i < j)
a[i++] = a[j];
}
a[i] = t;
Quick_Sort(a,l,i-1);
Quick_Sort(a,i+1,r);
}
return;
}
int main() {
int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};
int s = sizeof(a) / sizeof(int);
Pri(a,s);
Quick_Sort(a,0,s-1);
Pri(a,s);
return 0;
}
链表:
typedef struct PCB {
int tag;
int priority;
int cputime;
struct PCB* next;
}PCB;
void _swap(PCB* x, PCB* y) {
PCB t;
t.cputime = x->cputime;
t.priority = x->priority;
t.tag = x->tag;
x->cputime = y->cputime;
x->priority = y->priority;
x->tag = y->tag;
y->cputime = t.cputime;
y->priority = t.priority;
y->tag = t.tag;
}
PCB* GetPartion(PCB* head, PCB* tail)
{
int pri = head->priority;
PCB* u = head;
PCB* v = u->next;
while(v != tail)
{
if(v->priority > pri)
{
u = u->next;
_swap(u, v);
}
v = v->next;
}
_swap(u, head);
return u;
}
void QuickSort(PCB* head, PCB* tail)
{
if(head != tail)
{
PCB* partion = GetPartion(head, tail);
QuickSort(head,partion);
QuickSort(partion->next,tail);
}
}