[問題] quicksort的問題

看板C_and_CPP作者 (the day)時間15年前 (2010/05/02 02:58), 編輯推噓2(205)
留言7則, 3人參與, 最新討論串1/1
以下是程式碼 #include<stdio.h> #include<stdlib.h> #define k 8 #define j 2 //#define Max 16 int values[]={11,2,9,4,7,6,7,8,8,7,2,13,10,1,4,9}; int compare(int *a,int *b) { if(*a<*b) return(-1); if(*a==*b) return(0); if(*a>*b) return(1); } int main () { int a; int i; int n; int s; int number[s]; int new[k][j]={ {11,2}, {9,4}, {7,6}, {7,8}, {8,7}, {2,13}, {10,1}, {4,9} }; printf("input\n"); for (n=0; n<16; n++) { printf("%d\n ",values[n]); } printf("\n\n\n"); qsort(values, 16, sizeof(int), compare); printf("output\n"); for (n=0; n<16; n++) { printf("%d\n ",values[n]); } printf("------------------------------------------------\n"); printf("Before\n"); for(s=0;s<8;s++) { for(i=0;i<k;i++) { number[s]=new[i][0]; printf("%d\n",number[s]); } break; } qsort( number, 8, sizeof(int), compare); printf("after\n"); for(s=0;s<8;s++) { printf("%d\n",number[s]); } system("pause"); return 0; } values[]的排序可以排序的出來 但是同樣的使用在number[]排出來是奇怪的數字了 請告訴我哪裡出現問題了 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.204.155.208

05/02 03:12, , 1F
number只有初始化number[0]
05/02 03:12, 1F

05/02 03:14, , 2F
對不起 我不太懂你的意思
05/02 03:14, 2F

05/02 03:29, , 3F
在給 number[] 值的迴圈中, 只跑了 s=0, 就break
05/02 03:29, 3F

05/02 03:30, , 4F
還有看不太懂為什麼對於每個 number[s] 要給值 8次
05/02 03:30, 4F

05/02 03:30, , 5F
k 次
05/02 03:30, 5F

05/02 03:43, , 6F
因為我覺得他好像會跑八次 所以就加break了
05/02 03:43, 6F

05/02 03:43, , 7F
不好意思 我是新手 所以可能寫的很糟
05/02 03:43, 7F
文章代碼(AID): #1Bt7diQy (C_and_CPP)