查找用于对给定的数组进行“快速排序"排序所消耗的时间. [英] find a time consumed[taken] for sorting a given array-"quick sort"

查看:95
本文介绍了查找用于对给定的数组进行“快速排序"排序所消耗的时间.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码-但我遇到了分段错误?任何人都可以帮助我吗?
程序的目标是:找到对给定数组进行排序所花费的时间[b]


here is my code-but i am getting segmentation fault???? any body help me???
programme aim is:to find a time consumed[taken] for sorting a given array


#include<stdio.h>

#include<time.h>
const int MAX=1000;
 
int partition(int a[],int low,int high)
{
      int i,j,temp,key;
     key=a[low];
     i=low;
    j=high+1;
    while(i<=j&&key<=a[i])
    {
    i++;
    }
     while(key>a[j])
    {
    j--;
    }
     if(i<j);>
    {
    temp=a[i],a[i]=a[j],a[j]=temp;
    }
      else
    {
    temp=a[low],a[low]=a[j],a[j]=a[low];
    return j ;
    }
}
 
void quick_sort(int a[],int low,int high)
{
    int mid;
    if(low<high)>
    {
    mid=partition(a,low,high);
    quick_sort(a,low,mid-1);
    quick_sort(a,mid+1,high);
    }
}
 
void main()
    {
    int a[MAX],n,i,start,end;
    //clock_t end,start;
    double CLK_TCK;
    printf("enter the no of elements:");
    scanf("%d",&n);
    printf("\n enter the elements to sort\n");
    for(i=0;i<=n;i++)
    scanf("%d",&a[i]);
    start=clock();
    quick_sort(a,0,n-1);
    end=clock();
    printf("n the sorted elemetns are:\n\n");
    for(i=0;i<=n;i++)
    printf("%d\t",a[i]);
    printf("\n\n time consumed the array is: %5.2f sec: ",(end-start)/(double)CLK_TCK);
    //getch();
} /<pre>


output: [which i tried,myfile name-quick_sort.c]
.........................................
subbu@subbu:~/Desktop$ gcc quick_sort 
subbu@subbu:~/Desktop$ ./a.out 
enter the no of elements:3

 enter the elements to sort
2
3
3
2
Segmentation fault
subbu@subbu:~/Desktop$

推荐答案

gcc quick_sort subbu @ subbu:〜/Desktop
gcc quick_sort subbu@subbu:~/Desktop


./a.out 输入元素编号:3 输入要排序的元素 2个 3 3 2个 分段故障 subbu @ subbu:〜/Desktop
./a.out enter the no of elements:3 enter the elements to sort 2 3 3 2 Segmentation fault subbu@subbu:~/Desktop



这篇关于查找用于对给定的数组进行“快速排序"排序所消耗的时间.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆