c编程快速排序,带指针的冒泡排序演示

时间:2016-05-05 22:09:10

标签: c loops sorting pointers exit

很抱歉,这段代码有点像函数的性质,但是我已经实现了一个演示函数来测试速度与冒泡排序的差异,并快速排序完美的输出({{3菜单功能正常工作我不会包含那部分

如果你们想要我可以加入它,我还没有包含演示功能

然而,当我用指针实现相同的函数时,我不断得到一个奇怪的输出(http://imgur.com/XEGMgQB),其中我将为指针返回可变数量的测试。在我下面放置相关代码,任何帮助都将非常感激。

我确实有一个主要功能和其他一些不包括在内,因为它们工作正常,我不相信它们是问题,我的直觉说问题在于Bubble_mode_p函数或quicksort_p函数的循环,程序没有使用命令

在cygwin中编译
gcc -o calc calc.c -std=c99 -lm 

以下是源代码:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include<ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#define KEY "Enter the calculator operation you want to do: "
#define enter_option() printf("%s", KEY)
//Timing functions are below**********************************************************************
#define timing_start() start = clock();     
#define timing_end(f) msec = (double)(clock()-start) * 1000.0 / CLOCKS_PER_SEC; \
    printf("Calling "#f " takes %10.2f microseconds!\n", msec);

void Quick_mode_p();
int* quicksort_p(int *left, int *right, int *pivot);
void Bubble_mode_p(int *ptr,int s);
void swap(int *a, int *b);

void Quick_mode_p() {
    srand(time(NULL));
    int t1,t2,t_tot;

    printf("\n");
    printf("***************    Report of performance comparision    ***************\n"); 
    printf("\n");       
    printf("             quicksort       bubblesort      (in microseconds)\n"); 
    int end=0;
    do {
        int start=0,top=0;
        end+=500;
        int a[end],b[end];
        int *p=&a[0],*q=&a[end],*k=&a[0];
        int *f=&b[0];
        printf("N=%d:   ",end);
        for (int j=0;j<end;j++){
            a[j]=rand()%((2*end)+1)-end;
            if(a[j]<start)
                start=a[j];
            else if (a[j]>top)
                top=a[j];
        }
        for(int i=0;i<end;i++){
            b[i]=a[i];
        }

        t1=clock();
        quicksort_p(p,q,k);   
        t2=clock();
        t_tot= (double)(t2-t1)*1000000/CLOCKS_PER_SEC;
        printf("\t%7.1d\t", t_tot);
        t1=clock();
        Bubble_mode_p( f,end);
        t2=clock();
        t_tot= (double)(t2-t1)*1000000/CLOCKS_PER_SEC;
        printf("\t%7.1d\t", t_tot);

        printf("      array in range: [%d,%d]\n",start ,top ); 
        printf("\n");       
    } while (end!=5000); 
    clean_input();

}

int *quicksort_p(int *left, int *right, int *pivot) {
    int *i, *q;
    q = right;
    i = left ;
    int p = *pivot;

    while (q > pivot) {
        while ( p < *i )
            pivot++;
        while ( *q > *i )
            q--;
        if ( q > pivot ) {
            swap(pivot,q);
        }
    }
    swap(left, q);
    return q ;
}

void Bubble_mode_p(int *ptr,int s) {
    int i,j;
    int temp;
    for (i=1;i<s;i++) {
        for (j=0;j<s-i;j++) {
            if (*(ptr+j)>*(ptr+j+1)) {
                temp=*(ptr+j);
                *(ptr+j)=*(ptr+j+1);
                *(ptr+j+1)=temp;
            }
        }

    }
}

void swap(int *a, int *b) {
    int c = *a;
    *a = *b;
    *b = c;
} 

0 个答案:

没有答案
相关问题