admin 管理员组

文章数量: 887021


2023年12月22日发(作者:web百度百科)

void shellSort(int arr[], int n){ int i, j, elem; int k = n/2; while(k>=1) { for(i = k; i < n; i ++) { elem = arr[i]; for(j = i; j >= k; j-=k) { if(elem < arr[j-k]) { arr[j] = arr[j-k]; } else { break; } } arr[j] = elem; } k = k/2; }}void printArray(int arr[], int n){ int i; for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("n"); return;}void main(){ int arr[10] = {2,5,6,4,3,7,9,8,1,0}; printArray(arr, 10); shellSort(arr, 10); printArray(arr, 10); return;}5.归并排序

printf("n"); return;}void swap(int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; return;}//arr[j] < arr[l], arr[j+1,..i)>arr[l]static int partition(int arr[], int l, int r){ int i, j; i = l + 1; j = l; while(i <= r) { if(arr[i] > arr[l]) { i++; } else { swap(&arr[j + 1], &arr[i]); i++; j++; } } swap(&arr[l], &arr[j]); return j;}static void _quickSort(int arr[], int l, int r){ int key; if(l >= r) { return; } key = partition(arr, l, r); _quickSort(arr, l, key - 1); _quickSort(arr, key + 1, r);}void quickSort(int arr[], int n){ _quickSort(arr, 0, n - 1); return;}void main(){ int arr[10] = {1,5,9,8,7,6,3,4,0,2}; printArray(arr, 10); quickSort(arr, 10); printArray(arr, 10);}

} printf("n"); return;}void swap(int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; return;}static void _quickSort3(int arr [ ],int l,int r){ int i, lt, gt; if(l >= r) { return; } i = l + 1; lt = l; gt = r ; while(i <= gt) { if(arr[i] < arr[l]) { swap(&arr[lt + 1], &arr[i]); lt ++; i++; } else if(arr[i] > arr[l]) { swap(&arr[i], &arr[gt]); gt--; } else { i++; } } swap(&arr[l], &arr[gt]); _quickSort3(arr, l, lt); _quickSort3(arr, gt + 1, r); return;}void quickSort(int arr[], int n){ _quickSort3(arr, 0, n - 1); return;}void main(){ int arr[10] = {1,5,9,8,7,6,3,4,0,2}; printArray(arr, 10); quickSort(arr, 10); printArray(arr, 10);}

int j = 0 ; while(2*k + 1 < n) { j = 2 *k + 1; if (j + 1 < n && arr[j] < arr[j+1]) { j ++;

} if(arr[k] < arr[j]) { swap(&arr[k], &arr[j]); k = j; } else { break; } } return; } void heapSort(int arr[], int n) { int i = 0; for(i = (n - 1 -1)/2; i >=0; i--) { shiftDown(arr, n, i); } for(i = n - 1; i > 0; i--) { swap(&arr[0], &arr[i]); shiftDown(arr, i, 0); } return; } void printArray(int arr[], int n){ int i; for(i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("n"); return;} void main(){ int arr[10] = {1,5,9,8,7,6,3,4,0,2}; printArray(arr, 10); heapSort(arr, 10); printArray(arr, 10);}总结待续……..


本文标签: 百度 排序 总结