-1
quicksort 알고리즘을 실행할 때 문제가 발생했습니다. 오류가 발생하여 문제가 발생한 곳을 찾을 수 없습니다. 누군가가 실수를 지적 할 수 있다면 나는 감사 할 것이다.quicksort 구현 문제
#include <stdio.h>
#include <stdlib.h>
void main(){
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr,0,n-1);
printArray(arr,0,n-1);
}
//Quicksort Function
void quickSort(int arr[],int low,int high){
if (low < high){
int pi=partition(arr,low,high);
quickSort(arr,low,pi-1);//takes care of lower set of numbers
quickSort(arr,pi+1,high);//takes care of higher elements above pivot
}
}
//Function for partitioing, in my program i am cosidering pivot as the element at high or the last element
int partition(int arr[],int low,int high){
int i,j;
i=(low-1);
int pivot=arr[high];
for(j=low;j<=high;j++){
if(arr[j]<=pivot){
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}
//function to print array
void printArray(int arr[],int low,int high){
int i;
for(i=low;i<=high;i++){
printf("%d ",arr[i]);
}
}
//function to swap two elements of array
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
무엇 오류? 그리고 코드를 포맷하십시오. –
알고리즘의 각 반복에서 정렬 할 항목의 상태를 인쇄하고 수동으로 정렬 할 때 해당 출력과 비교해 보았습니까? 오류를 찾으려고 무엇을 했습니까? 그래서 당신을위한 디버거가 아닙니다. –
stackoverflow.com에 오신 것을 환영합니다. [도움말 페이지] (http://stackoverflow.com/help), 특히 [여기서 어떤 주제에 관해서 물어볼 수 있습니까?] (http://stackoverflow.com/help/) 섹션을 읽어보십시오. on-topic) 및 [ "어떤 유형의 질문을하지 않아야합니까?"] (http://stackoverflow.com/help/dont-ask). 또한 [둘러보기] (http://stackoverflow.com/tour)와 [좋은 질문을하는 방법에 대해 읽어보십시오.] (http://stackoverflow.com/help/how-to-ask). –