|
|
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
|
กระทู้ #1401 [C] (จาก IP: 58.9.164.243)
ช่วยหน่อยคับ เรื่อง โปรแกรมQuicksort
รันไม่ผ่านน่ะคับ ช่วยแก่ทีคับว่าผิดตรงไหน ปวดหัวไปหมดแล้ว #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 12 void showlist(int arr[],int st,int end) { int i; for( i=st;i<=end;i++) printf("%d ",arr[i]); } void doquicksort(int arr[],int low,int up) { int piv,temp,left,right; pivot=false; left=low; right=up; piv=low; // take first element as pivot if(low>=up) return; printf("Sublist ("); showlist(arr,low,up); printf("\b)"); while(!pivot) { // Compare from right to left while( arr[piv]<=arr[right] && piv!=right ) right=right-1; if( piv==right ) bool pivot=true; if( arr[piv] > arr[right] ) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } // Compare from left to right while( arr[piv]>=arr[left] && left!=piv ) left=left+1; if(piv==left) pivot=true; if( arr[piv] < arr[left] ) { temp=arr[piv]; arr[piv]=arr[left]; arr[left]=temp; piv=left; } } printf("\nPivot is %d\n",arr[piv]); printf("Sorted sublist : ("); showlist(arr,low,up); printf("\b)\n\n"); doquicksort(arr,low,piv-1); doquicksort(arr,piv+1,up); } void main() { int arr[MAX]={48,29,8,59,72,88,42 ,65, 95, 19, 82, 68}; int n,i; n=12; printf("Before sort : "); showlist(arr,0,n-1); printf("\n"); doquicksort(arr,0,n-1); printf("After sorted : "); showlist(arr,0,n-1); printf("\n"); }
|
จากคุณ
:
โย / dlo_xlb@hotmail.com [2007-09-19 00:06:55]
|
|
ความคิดเห็น #26027 (จาก IP: 203.146.147.190)
ลองไล่โค้ดเขียนเป็น flowchart แล้วก็นั่งพิจจารณาว่า ผิดตรงไหน |
จากคุณ
:
อย่าแพ้ ป.4 [2007-09-19 10:20:16]
|
|
ความคิดเห็น #26029 (จาก IP: 58.64.120.53)
void doquicksort(int arr[],int low,int up); void showlist(int arr[],int st,int end); //--------------------------------------------------------------------------- int main(int argc, char* argv[]) { int arr[MAX]={48,29,8,59,72,88,42 ,65, 95, 19, 82, 68}; int n,i;
n=12; printf("Before sort : "); showlist(arr,0,n-1); printf("\n"); doquicksort(arr,0,n-1); printf("After sorted : "); showlist(arr,0,n-1); printf("\n"); return 0; } //--------------------------------------------------------------------------- void showlist(int arr[],int st,int end) { int i; for( i=st;i<=end;i++) printf("%d ",arr[i]); } //--------------------------------------------------------------------------- void doquicksort(int arr[],int low,int up) { int piv,temp,left,right;
bool pivot=false; left=low; right=up; piv=low; // take first element as pivot if(low>=up) return; printf("Sublist ("); showlist(arr,low,up); printf("\b)"); while(!pivot) { // Compare from right to left while( arr[piv]<=arr[right] && piv!=right ) right=right-1; if( piv==right ) pivot=true; if( arr[piv] > arr[right] ) { temp=arr[piv]; arr[piv]=arr[right]; arr[right]=temp; piv=right; } // Compare from left to right while( arr[piv]>=arr[left] && left!=piv ) left=left+1; if(piv==left) pivot=true; if( arr[piv] < arr[left] ) { temp=arr[piv]; arr[piv]=arr[left]; arr[left]=temp; piv=left; } } printf("\nPivot is %d\n",arr[piv]); printf("Sorted sublist : ("); showlist(arr,low,up); printf("\b)\n\n"); doquicksort(arr,low,piv-1); doquicksort(arr,piv+1,up); } //---------------------------------------------------------------------------
|
จากคุณ
:
bird / bird_aw@yahoo.com [2007-09-19 10:47:23]
|
|
|
- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -
|
|
|
|
|