标签:back quic nes chapter inf border type art 3.3
//****************************Sort.h********************************************
#ifndef SORT_H
#define SORT_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//冒泡排序
void BubbleSort(int data[],int len);
//简单选择排序
void SelectSort(int data[],int len);
//直接插入排序
void InsertSort(int data[],int len);
//希尔排序
void ShellSort(int data[],int len);
//堆排序
void HeapSort(int data[],int len);
//归并排序
void MergeSort(int data[],int len);
//快速排序
void QuickSort(int data[],int len);
#endif //SORT_H
//****************************Sort.c********************************************
#include "Sort.h"
void swap(int a[],int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
//冒泡排序
void BubbleSort(int data[],int len)
{
int i,j;
bool flag;
for(i=0;i < len-1;i++)
{
flag = false;
for(j = len-1;j >i;j--)
{
if(data[j] < data[j-1])
{
swap(data,j,j-1);
}
}
if(!flag) //标志位未被置位,序列已经有序
{
break;
}
}
}
//简单选择排序
void SelectSort(int data[],int len)
{
int i,j,min;
for(i=0;i < len;i++)
{
min = i;
for(j=i+1;j < len;j++)
{
if(data[j] < data[min])
{
min = j;
}
}
if(min != i)
{
swap(data,i,min);
}
}
}
//直接插入排序
void InsertSort(int data[],int len)
{
int i,j;
for(i=1;i < len;i++)
{
if(data[i] < data[i-1])
{
int tmp = data[i];
for(j= i-1;j >= 0 && data[j] > tmp;j--)
{
data[j+1] = data[j];
}
data[j+1] = tmp;
}
}
}
//希尔排序
void ShellSort(int data[],int len)
{
int increment = len;
int i,j;
do
{
increment = increment/3+1;
for(i=increment+1;i < len;i++)
{
if(data[i] < data[i-increment])
{
int tmp = data[i];
for(j= i-increment;j >= 0 && data[j] > tmp ;j-=increment)
{
data[j+increment] = data[j];
}
data[j+increment] = tmp;
}
}
}
while(increment>1);
}
void HeapAdjust(int data[],int s,int m)
{
int i,tmp;
tmp = data[s];
for(i=2*s;i <= m;i*=2)
{
if(i<m && data[i] < data[i+1])
{
++i;
}
if(tmp >= data[i])
{
break;
}
data[s] = data[i];
s = i;
}
data[s] = tmp;
}
//堆排序
void HeapSort(int data[],int len)
{
int i;
for(i=len/2;i >= 0;i--)
{
HeapAdjust(data,i,len-1);
}
for(i=len-1;i > 0;i--)
{
swap(data,0,i);
HeapAdjust(data,0,i-1);
}
}
void Merge(int data[],int tmp[],int s,int m,int t)
{
int j,k,l;
for(j=m+1,k=s;j<=t && s<=m;k++)
{
if(data[s] < data[j])
{
tmp[k] = data[s++];
}
else
{
tmp[k] = data[j++];
}
}
if(s <= m)
{
for(l=0;l <= m-s;l++)
{
tmp[k+l] = data[s+l];
}
}
if(j <= t)
{
for(l=0;l <= t-j;l++)
{
tmp[k+l] = data[j+l];
}
}
}
void MSort(int data[],int tmp[],int s,int t)
{
int tmp1[1000];
int m;
if(s == t)
{
tmp[s] = data[s];
}
else
{
m = (s+t)/2;
MSort(data,tmp1,s,m);
MSort(data,tmp1,m+1,t);
Merge(tmp1,tmp,s,m,t);
}
}
//归并排序
void MergeSort(int data[],int len)
{
MSort(data,data,0,len-1);
}
int Partition(int data[],int low,int high)
{
int key = data[low];
while(low < high)
{
while(low<high && data[high] > key)
{
high--;
}
data[low] = data[high];
while(low <high && data[low] < key)
{
low++;
}
data[high] = data[low];
}
data[low] = key;
return low;
}
void QSort(int data[],int low,int high)
{
int pivot;
if(low <high)
{
pivot = Partition(data,low,high);
QSort(data,low,pivot-1);
QSort(data,pivot+1,high);
}
}
//快速排序
void QuickSort(int data[],int len)
{
QSort(data,0,len-1);
}
//****************************SortTest.c********************************************
#include "Sort.h"
void travel(int a[],int n)
{
for(int i=0;i < n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int a[] = {2,6,8,5,4,10,1,3,7,9};
travel(a,10);
//冒泡排序
//BubbleSort(a,10);
//简单选择排序
//SelectSort(a,10);
//直接插入排序
//InsertSort(a,10);
//希尔排序
//ShellSort(a,10);
//堆排序
//HeapSort(a,10);
//归并排序
//MergeSort(a,10);
//快速排序
QuickSort(a,10);
travel(a,10);
}
x
//****************************Sort.h********************************************
//冒泡排序
void BubbleSort(int data[],int len);
//简单选择排序
void SelectSort(int data[],int len);
//直接插入排序
void InsertSort(int data[],int len);
//希尔排序
void ShellSort(int data[],int len);
//堆排序
void HeapSort(int data[],int len);
//归并排序
void MergeSort(int data[],int len);
//快速排序
void QuickSort(int data[],int len);
//SORT_H
//****************************Sort.c********************************************
void swap(int a[],int i,int j)
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
//冒泡排序
void BubbleSort(int data[],int len)
{
int i,j;
bool flag;
for(i=0;i < len-1;i++)
{
flag = false;
for(j = len-1;j >i;j--)
{
if(data[j] < data[j-1])
{
swap(data,j,j-1);
}
}
if(!flag) //标志位未被置位,序列已经有序
{
break;
}
}
}
//简单选择排序
void SelectSort(int data[],int len)
{
int i,j,min;
for(i=0;i < len;i++)
{
min = i;
for(j=i+1;j < len;j++)
{
if(data[j] < data[min])
{
min = j;
}
}
if(min != i)
{
swap(data,i,min);
}
}
}
//直接插入排序
void InsertSort(int data[],int len)
{
int i,j;
for(i=1;i < len;i++)
{
if(data[i] < data[i-1])
{
int tmp = data[i];
for(j= i-1;j >= 0 && data[j] > tmp;j--)
{
data[j+1] = data[j];
}
data[j+1] = tmp;
}
}
}
//希尔排序
void ShellSort(int data[],int len)
{
int increment = len;
int i,j;
do
{
increment = increment/3+1;
for(i=increment+1;i < len;i++)
{
if(data[i] < data[i-increment])
{
int tmp = data[i];
for(j= i-increment;j >= 0 && data[j] > tmp ;j-=increment)
{
data[j+increment] = data[j];
}
data[j+increment] = tmp;
}
}
}
while(increment>1);
}
void HeapAdjust(int data[],int s,int m)
{
int i,tmp;
tmp = data[s];
for(i=2*s;i <= m;i*=2)
{
if(i<m && data[i] < data[i+1])
{
++i;
}
if(tmp >= data[i])
{
break;
}
data[s] = data[i];
s = i;
}
data[s] = tmp;
}
//堆排序
void HeapSort(int data[],int len)
{
int i;
for(i=len/2;i >= 0;i--)
{
HeapAdjust(data,i,len-1);
}
for(i=len-1;i > 0;i--)
{
swap(data,0,i);
HeapAdjust(data,0,i-1);
}
}
void Merge(int data[],int tmp[],int s,int m,int t)
{
int j,k,l;
for(j=m+1,k=s;j<=t && s<=m;k++)
{
if(data[s] < data[j])
{
tmp[k] = data[s++];
}
else
{
tmp[k] = data[j++];
}
}
if(s <= m)
{
for(l=0;l <= m-s;l++)
{
tmp[k+l] = data[s+l];
}
}
if(j <= t)
{
for(l=0;l <= t-j;l++)
{
tmp[k+l] = data[j+l];
}
}
}
void MSort(int data[],int tmp[],int s,int t)
{
int tmp1[1000];
int m;
if(s == t)
{
tmp[s] = data[s];
}
else
{
m = (s+t)/2;
MSort(data,tmp1,s,m);
MSort(data,tmp1,m+1,t);
Merge(tmp1,tmp,s,m,t);
}
}
//归并排序
void MergeSort(int data[],int len)
{
MSort(data,data,0,len-1);
}
int Partition(int data[],int low,int high)
{
int key = data[low];
while(low < high)
{
while(low<high && data[high] > key)
{
high--;
}
data[low] = data[high];
while(low <high && data[low] < key)
{
low++;
}
data[high] = data[low];
}
data[low] = key;
return low;
}
void QSort(int data[],int low,int high)
{
int pivot;
if(low <high)
{
pivot = Partition(data,low,high);
QSort(data,low,pivot-1);
QSort(data,pivot+1,high);
}
}
//快速排序
void QuickSort(int data[],int len)
{
QSort(data,0,len-1);
}
//****************************SortTest.c********************************************
void travel(int a[],int n)
{
for(int i=0;i < n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
int a[] = {2,6,8,5,4,10,1,3,7,9};
travel(a,10);
//冒泡排序
//BubbleSort(a,10);
//简单选择排序
//SelectSort(a,10);
//直接插入排序
//InsertSort(a,10);
//希尔排序
//ShellSort(a,10);
//堆排序
//HeapSort(a,10);
//归并排序
//MergeSort(a,10);
//快速排序
QuickSort(a,10);
travel(a,10);
}
标签:back quic nes chapter inf border type art 3.3
原文地址:https://www.cnblogs.com/LyndonMario/p/9326372.html