首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

鸡尾酒排序Linux上c 实现

2012-08-29 
鸡尾酒排序Linux下c 实现很久很久以前,曾经写了个blog:冒泡排序 Linux下c 实现 .,这次再show个冒泡排序的

鸡尾酒排序Linux下c 实现

      很久很久以前,曾经写了个blog:冒泡排序 Linux下c 实现 .  ,这次再show个冒泡排序的变种:鸡尾酒排序。 鸡尾酒排序在排序时,从两个方向在序列中排序。先找到最大的数字放到最后一位,然后找到最小的数字,放到第一位;然后再找到第二大的数字放到倒数第二位,再找到第二小的数字放到第二位。以此类推,直到完成排序。详细实现,请参阅下面的关键代码:

    1、排序头文件:cocktailSort.h

    

#ifndef COCKTAILSORT_H#define COCKTAILSORT_H#include<stdbool.h>extern void cocktailSort(int * pArr, int length);#endif


      2、排序源文件:cocktailSort.c

#include "cocktailSort.h"void cocktailSort(int * pArr, int length){        int bottom = 0;        int top = length-1;        bool swapped = true;        int tmp,i;        while(swapped)        {                swapped = false;                for(i=bottom; i<top; i++)                {                        if(*(pArr+i)>*(pArr+i+1))                        {                                swapped =true;                                tmp = *(pArr+i);                                *(pArr+i)=*(pArr+i+1);                                *(pArr+i+1)=tmp;                        }                }                top--;                for(i=top; i>bottom; i--)                {                        if(*(pArr+i)<*(pArr+i-1))                        {                                swapped = true;                                tmp = *(pArr+i);                                *(pArr+i)=*(pArr+i-1);                                *(pArr+i-1)=tmp;                        }                }                bottom++;        }}

        3、main函数头文件:main.h

#ifndef MAIN_H#define MAIN_H#include<stdio.h>#include "cocktailSort.h"extern void outputArr(const int *pArr, int lenght);extern int main(void);#endif


         3、main函数源文件:main.c

#include "main.h"int main(void){        printf("input array length:\n");        int length;        scanf("%d", &length);        if(length<0)        {                printf("array length must be larger 0\n");                return 1;        }        printf("array lenght is %d \n", length);        int arr[length];// ={1, 3, 6,5, 4};        int i=0;        for(i=0;i<length;i++)        {                printf("input arr[%d] value\n",i);                scanf("%d", &arr[i]);        }        printf("orig array:");        outputArr(arr, length);        cocktailSort(arr,length);        printf("cocktail sort array:");        outputArr(arr, length);        return 0;}void outputArr(const int *pArr, int length){        int i;        for(i=0;i<length;i++)        {                printf(" %d",*(pArr+i));        }        printf("\n");}


             4、Makefile文件:Makefile

all: mainmain: main.o cocktailSort.o         gcc -o main main.o cocktailSort.omain.o: main.c cocktailSort.c cocktailSort.h         gcc -c main.ccocktailSort.o: cocktailSort.c cocktailSort.h        gcc -c cocktailSort.cclean:        @echo "start cleaning"        -rm main *.o        @echo "clean completed" .PHONY: clean


            5、编译

make


            如果不出意外,可以看到可执行文件main。然后执行可执行文件,如下所示:

[root@localhost sort]$ ./main input array length:5array lenght is 5 input arr[0] value34input arr[1] value67input arr[2] value89input arr[3] value4input arr[4] value3orig array: 34 67 89 4 3cocktail sort array: 3 4 34 67 89

          

热点排行