c - 当我编译并运行时,Dev C++ 中的程序崩溃

标签 c

当我在 Dev C++ 中运行这个程序时,它会一直执行,直到“在数组中输入您想要的元素数量”,并且当我给出 5 个数字作为输入时,它会崩溃。 谁能帮我吗? 这是使用c实现快速排序

#include<stdio.h>
#include<stdlib.h>

void swap(int *a,int *b)//function to swap array
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;

}

void quicksort(int arr[],int start,int end)//function for performig quicksort
{
    if(start>=end)
    {
        return;
    }
    else
    {
        int pindex= partition(arr,start,end);
        quicksort(arr,start,pindex-1);
        quicksort(arr,pindex+1,end);
    }
}

int partition(int arr[],int start,int end)//function to partition the array
{
    int i;
    int pivot=arr[end];//always making the last element in the array as pivot
    int pindex=arr[start];
    for(i=0;i<end;i++)
    {
        if(arr[i]<=pivot)
        {
            swap(&arr[i],&arr[pindex]);
        }
        swap(&arr[pindex],&arr[end]);
    }
}


void display(int arr[],int n)//function to display the sorted array
{
    int i=0;
    for(i=0;i<n;i++)
    {
        printf("%d",arr[i]);
    }
}

void main() // main function initializing here 
{
    int n,i,arr[20];
    printf("enter the no of elements u want in an array");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
        printf("\n");
    }
    quicksort(arr,0,n);
    display(arr,n);
    getch();

}

This is my complete code of  quick sort program.i used different functions for different displaying output,swapping numbers partitioning etc.

最佳答案

发布的代码甚至还没有接近编译。

强烈建议在启用所有警告的情况下进行编译

(对于 gcc,至少使用:-Wall -Wextra -pedantic 来启用警告

修复警告。

然后重新发布代码

仅供引用:

  • 使用:int main( void )int main( int argc, char *argv[] )
  • 为除 main() 之外的每个函数放置一个原型(prototype), 位于文件开头,#include 语句之后
  • conio.h 头文件不可移植(因此不应使用) 建议使用stdlib.h
  • 任何不是 void 返回类型的函数都必须有一个 返回值;语句
  • 函数:getch()不可移植(因此不应使用) 建议使用getchar()

  • 一般来说,对于代码布局,使用:

'#include' statements
blank line
'#define' statements
blank line
'static' file scope variables
blank line
prototypes for all functions except main()
blank line
'int main()' function
blank line
each sub function, separated by a blank line

关于c - 当我编译并运行时,Dev C++ 中的程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288143/

相关文章:

c - 根据复选框更改输入框中的文本 (GTK+)

c - 如何减轻转置数组访问顺序对性能的影响?

c - 在 C 中使用带有 RSA 的 SHA1 对数据数组进行签名

c - 指针,计数器变得困惑

c - 使不选择所需的目标

c - Bison 中的未知类型 yypcontext_t

c++ - 仅对 STL 容器使用 C++ 是一种不好的做法吗?

c - 段错误问题(C)

c - 不确定这段代码中发生了什么

c - 客户端 select() 的替代方法