标签:c
//从小到大: //下标法: #include <stdio.h> int main() { int i, j, t; int arr[10] = { 1, 3, 2, 9, 6, 7, 0, 4, 5, 8 }; int len = sizeof(arr) / sizeof(arr[0]); for (i = 0; i < len-1; i++) { for (j = 0; j < len-i-1; j++) { if (arr[j] > arr[j+1]) { t = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = t; } } } printf("排序后数组为:\n"); for (i = 0; i < len; i++) { printf("%d ", arr[i]); } return 0; } //第二种方法:指针法 #include <stdio.h> int maopao(int *arr,int len) { int i, j, t; for (i = 0; i < len - 1; i++) { for (j = 0; j < len-i-1; j++) { if (*(arr+j )> *(arr+j+1)) { t = *(arr + j); *(arr + j) = *(arr + j + 1); *(arr + j + 1) = t; } } } printf("排序后数组为:\n"); for (i = 0; i < len; i++) { printf("%d ", *(arr+i)); } printf("\n"); } int main() { int arr[10] = { 1, 4, 8, 0, 5, 7, 2, 9, 3, 6 }; int len = sizeof(arr) / sizeof(arr[0]); maopao(arr, len); return 0; }
标签:c
原文地址:http://10706198.blog.51cto.com/10696198/1706402