KeyPoint : 找到关键的父节点
单线程找最大值 最快的方法
#include "stdlib.h" #include "stdio.h" void findMax(int *arr, int size) { int totParent = size / 2 - 1; for (;totParent >= 0;totParent--) { int parent = totParent; int child = 2 * totParent + 1; if (child < size - 1 && arr[child] < arr[child + 1]) { child++; } if (arr[parent] < arr[child]) { int tmp = arr[child]; arr[child] = arr[parent]; arr[parent] = tmp; } } } void printArr(int *arr, int length) { for (int i = 0;i < length;i++) { printf("%d ",arr[i]); } printf("\n"); } void sortArr(int *arr, int length) { for (int i = 0;i < length - 1;i++) { findMax(arr + i, length - i); printArr(arr + i, length - i); } } int main() { int arr[10] = { 0,2,87,49,34,62,53,6,44,90 }; int length = sizeof(arr)/sizeof(int); sortArr(arr, length); printArr(arr, length); system("pause"); return 0; }
时间: 2024-12-12 05:57:55