冒泡排序代碼:指針
//冒泡排序 void arr(){ int arr[] = {3,2,4,1}; int count = sizeof(arr)/sizeof(arr[0]); for (int i=0; i<count-1; i++) { for (int j=0; j<count-i-1; j++) { if (arr[j]<arr[j+1]) { int temp; temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for (int x = 0; x<count; x++) { printf("count=%i,value=%i\n",count,arr[x]); } }
冒泡的指針排序:code
void pointArr(){ int arr[] = {5,4,6,2,7,1,3}; int count = sizeof(arr)/sizeof(arr[0]); int *pointer; pointer = arr; for (int i = 0; i<count-1; i++) { for (int j = 0; j<count-i-1; j++) { if (*(pointer+j)<*(pointer+j+1)) { int tem; tem = *(pointer+j); *(pointer+j) = *(pointer+j+1); *(pointer+j+1) = tem; } } } for (int x = 0; x<count; x++) { printf("count=%i,value=%i\n",count,arr[x]); } }
選擇排序代碼:排序
void selectArr(){ int arr[] = {4,3,5,2,10,50,20,45}; int count = sizeof(arr)/sizeof(arr[0]); for (int i = 0; i<count-1; i++) { for (int j = i+1; j<count; j++) { if (arr[i]>arr[j]) { int temp; temp = arr[i]; arr[i] = arr[j]; arr[j]=temp; } } } for (int x = 0; x<count; x++) { printf("count=%i,value=%i\n",count,arr[x]); } }