#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void BubbleSort(int arr[], int nLength)
{
int nFlag;
int i;
int j;
if(arr == NULL || nLength <= 0)return;
for(i=0;i<nLength;i++){
nFlag = 0;
for(j=0;j<nLength-i-1;j++){
if(arr[j] > arr[j+1]){
arr[j] = arr[j]^arr[j+1];
arr[j+1] = arr[j]^arr[j+1];
arr[j] = arr[j]^arr[j+1];
nFlag = j;
}
}
if(nFlag == 0)return;
i = nLength-nFlag-2;
}
}
void SelectSort(int arr[], int nLenght)
{
int nMin;
int i;
int j;
if(arr == NULL || nLenght<=0)return;
for(i=0;i<nLenght;i++)
{
nMin = i;
for(j=i+1;j<nLenght;j++)
{
if(arr[nMin] > arr[j]){
nMin = j;
}
}
if(nMin != i){
arr[nMin] = arr[nMin]^arr[i];
arr[i] = arr[nMin]^arr[i];
arr[nMin] = arr[nMin]^arr[i];
}
}
}
void InsertSort(int arr[], int nLength)
{
int i;
int j;
int nTemp;
if(arr==NULL || nLength<=0)return;
for(i=1; i<nLength; i++)
{
if(arr[i] < arr[i-1]){
j = i;
nTemp = arr[j];
while(j>=1 && nTemp < arr[j-1]){
arr[j] = arr[j-1];
j--;
}
arr[j] = nTemp;
}
}
}
void CountSort(int arr[], int nLength)
{
int i;
int nMax;
int nMin;
int *pTemp;
int *pArr;
int nLen;
int nMark;
if(arr==NULL || nLength<=0)return;
nMax = arr[0];
nMin = arr[0];
for(i=1; i<nLength; i++){
if(nMax < arr[i])nMax = arr[i];
if(nMin > arr[i])nMin = arr[i];
}
nLen = nMax - nMin + 1;
pTemp = (int *)malloc(sizeof(int)*nLen);
pArr = (int *)malloc(sizeof(int)*nLength);
memset(pTemp, 0, sizeof(int)*nLen);
for(i=0; i< nLength; i++)
{
pTemp[arr[i]-nMin]++;
}
for(i=0; i<nLen-1; i++){
pTemp[i+1] += pTemp[i];
}
nLen = nLength-1;
while(nLen >= 0){
nMark = --pTemp[arr[nLen]-nMin];//{1,2,4,7,6,5,0,9,8,3}
pArr[nMark] = arr[nLen];
nLen--;
}
for(i=0; i<nLength; i++){
arr[i] = pArr[i];
}
}
int FindStandardTwoWhile(int arr[], int nLeft, int nRight)
{
int nMark;
if(arr == NULL || nLeft<0 || nRight<0 || nLeft>nRight)return;
nMark = arr[nLeft];
while(nLeft < nRight){
while(nRight>nLeft && arr[nRight]>=nMark)nRight--;
arr[nLeft] = arr[nRight];
while(nLeft<nRight && arr[nLeft]<=nMark)nLeft++;
arr[nRight] = arr[nLeft];
}
arr[nLeft] = nMark;
return nLeft;
}
int FindStandardSplit(int arr[], int nLeft, int nRight)
{
int nMark;
int nMin;
if(arr == NULL || nLeft<0 || nRight<0 || nLeft>=nRight)return -1;
nMark = arr[nRight];
nMin = nLeft-1;
while(nLeft < nRight){
if(arr[nLeft] <= nMark){
if(nLeft != nMin + 1){
arr[nLeft] = arr[nMin + 1]^arr[nLeft];
arr[nMin + 1] = arr[nMin + 1]^arr[nLeft];
arr[nLeft] = arr[nMin + 1]^arr[nLeft];
}
nMin++;
}
nLeft++;
}
if(nLeft != nMin + 1){
arr[nLeft] = arr[nMin + 1]^arr[nLeft];
arr[nMin + 1] = arr[nMin + 1]^arr[nLeft];
arr[nLeft] = arr[nMin + 1]^arr[nLeft];
}
nMin++;
return nMin;
}
void ShellSort(int arr[], int nLength)
{
int nMark;
int i;
int j;
int nTemp;
int k;
if(arr==NULL || nLength<=0)return;
for(nMark = nLength/2; nMark >= 1; nMark/=2){
for(i=0; i<nMark; i++){
for(j=i+nMark; j<nLength; j+=nMark){
if(arr[j] < arr[j-nMark]){
k = j-nMark;
nTemp = arr[j];
while(k>=i && nTemp<arr[k]){
arr[k+nMark] = arr[k];
k -= nMark;
}
arr[k + nMark] = nTemp;
}
}
}
}
}
void QuickSort(int arr[], int nLeft, int nRight)
{
int nStandard;
if(arr==NULL || nLeft<0 || nRight<0 || nRight<=nLeft)return;
nStandard = FindStandardSplit(arr, nLeft, nRight);
QuickSort(arr, nLeft, nStandard-1);
QuickSort(arr, nStandard+1, nRight);
}
int main()
{
int arr1[]={1,2,4,7,6,5,0,9,8,3,3,4,3,2,1,4,5,6,12,213,324,435,134,123};
int arr2[]={1,2,4,7,6,5,0,9,8,3,3,4,3,2,1,4,5,6};
int arr3[]={1,2,4,7,6,5,0,9,8,3,3,4,3,2,1,4,5,6};
int arr4[]={1,2,4,7,6,5,0,9,8,3,3,4,3,2,1,4,5,6};
int arr5[]={1,2,4,7,6,5,0,9,8,3,3,4,3,2,1,4,5,6};
BubbleSort(arr1,sizeof(arr1)/sizeof(int));
SelectSort(arr2,sizeof(arr2)/sizeof(int));
CountSort(arr3,sizeof(arr3)/sizeof(int));
QuickSort(arr4,0,sizeof(arr4)/sizeof(int)-1);
ShellSort(arr5,sizeof(arr5)/sizeof(int));
return 0;
}
排序算法集合
最新推荐文章于 2024-09-03 20:45:39 发布