当前位置:网站首页>Binary tree sorting (C language, int type)
Binary tree sorting (C language, int type)
2022-07-03 08:50:00 【Cap07】
#define _CRT_SECURE_NO_WARNINGS // Binary tree sorting (int type )
#include<stdio.h> // Array index from 1 Start
#include <string.h>
void move_int(int a[], int a_length) { // Will array a Move the whole backward by one bit , Give Way a[0] invalid
for (int i = a_length; i > 0; i--) {
a[i] = a[i - 1];
}
}
void exch_int(int a[], int i, int j) { // In exchange for a[i] and a[j] Value
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void swim_int(int a[], int k) { // Floating up k The element of location To the right place ( The tree is descending from top to bottom )
while (k > 1 && a[k / 2] < a[k]) {
exch_int(a, k / 2, k);
k = k / 2;
}
}
void sink_int(int a[], int k, int a_length) { // Sinking k Location elements , Give priority to exchanging positions with the largest left and right children ( The tree is descending from top to bottom )
while (2 * k <= a_length) { //a_length Is an array a Remove a[0] The length after
int j = 2 * k;
if (j < a_length && a[j] < a[j + 1]) j++;
if (!(a[k] < a[j])) break;
exch_int(a, k, j);
k = j;
}
}
void BiTree_sort_int(int a[], int a_length) { // Binary tree sorting ,a_length Is an array a The effective length of minus 1( I.e. removal a[0] The length after )
for (int k = a_length / 2; k >= 1; k--) { // Make the binary tree orderly ( Descending from top to bottom )
sink_int(a, k, a_length);
}
while (a_length > 1) { // Sort ascending from top to bottom
exch_int(a, 1, a_length--);
sink_int(a, 1, a_length);
}
}
int main() {
int a[100] = {};
printf(" Please enter the total number of numbers :\n");
int N;
scanf("%d", &N);
printf("\n Please enter %d Number :\n", N);
for (int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
move_int(a, N); //a[0] Give up not to use !!
BiTree_sort_int(a, N);
printf("\n The result after ascending is :\n");
for (int i = 1; i <= N; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}test result :

边栏推荐
- Annotations simplify configuration and loading at startup
- First Servlet
- 注解简化配置与启动时加载
- 第一个Servlet
- JS ternary operator - learning notes (with cases)
- [rust note] 10 operator overloading
- UE4 source code reading_ Bone model and animation system_ Animation node
- Visual Studio (VS) shortcut keys
- Advanced OSG collision detection
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
猜你喜欢

Markdown learning

MySQL 8

Explain sizeof, strlen, pointer, array and other combination questions in detail

Monotonic stack -42 Connect rainwater

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Advanced OSG collision detection

Installation of PHP FPM software +openresty cache construction

Redux - learning notes

Simple demo of solving BP neural network by gradient descent method
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
随机推荐
Parameters of convolutional neural network
【Rust 笔记】13-迭代器(上)
JS ternary operator - learning notes (with cases)
DOM 渲染系统(render mount patch)响应式系统
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
[rust notes] 06 package and module
LinkedList set
Markdown learning
Baidu editor ueeditor changes style
Unity Editor Extension - Outline
ES6 promise learning notes
[rust notes] 12 closure
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
【Rust笔记】05-错误处理
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
On the difference and connection between find and select in TP5 framework
Alibaba canal actual combat
单调栈-84. 柱状图中最大的矩形
UE4 source code reading_ Bone model and animation system_ Animation compression