[問題] sort int and char*

看板C_and_CPP (C/C++)作者 (始終不渝)時間11年前 (2015/04/29 14:00), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
[code] #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 7 int main() { int i=0,j=0,k=0; int b[7]={7,6,5,4,3,2,1}; char a[N][7]={"g","f","e","d","c","b","a"}; int temp_b=0; char *temp_a=NULL; printf("before sorted\n"); printf("a[] b[]\n"); printf("-----\n"); for(j=0;j<7;j++) { printf("%s %d\n",a[j],b[j]); } //sort for(i=0;i<7;i++) { if(a[i]>a[i+1]) { strcpy(temp_a,a[i+1]); strcpy(a[i+1],a[i]); strcpy(a[i],temp_a); } } for(k=0;k<7;k++) { if(b[k]>b[k+1]) { /* b[i+1]=temp_b; temp_b=b[i+1]; b[i+1]=b[i]; */ temp_b=b[k+1]; b[k+1]=b[k]; b[k]=temp_b; } } printf("----------------\n"); printf("after sorted\n"); printf("a[] b[]\n"); printf("-----\n"); for(i=0;i<7;i++) printf("%s %d\n",a[i],b[i]); system("PAUSE"); return 0; } [Output] before sorted a[] b[] ----- g 7 f 6 e 5 d 4 c 3 b 2 a 1 ---------------- after sorted a[] b[] ----- g 6 f 5 e 4 d 3 c 2 b 1 a 7 [Question] why no sorted -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.72.142.198 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1430287226.A.2F5.html

04/29 14:07, , 1F
sorting演算法是錯的 沒那麼單純
04/29 14:07, 1F

04/29 14:20, , 2F
Because it's not sort
04/29 14:20, 2F
文章代碼(AID): #1LG7DwBr (C_and_CPP)
文章代碼(AID): #1LG7DwBr (C_and_CPP)