bubbleSort.cpp
#includeusing namespace std; void bubbleSort(int * arr, int len) { for (int i=0; i< len-1; i++) { for (int j=0; j if (*(arr+j) > *(arr+j+1)) { int temp = *(arr+j); *(arr+j) = *(arr+j+1); *(arr+j+1) = temp; } } } } void bubbleSort02(int * arr, int len) { for (int i=0; i< len-1; i++) { for (int j=0; j if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } int main() { int arr[10] = {4,3,6,9,1,2,10,8,7,5}; int len = sizeof(arr)/sizeof(arr[0]); for (int i=0; i cout << arr[i] << " "; } cout << endl; bubbleSort02(arr, len); for (int i=0; i cout << arr[i] << " "; } cout << endl; system("pause"); return 0; }
执行结果: