当前位置:网站首页>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 :

边栏推荐
- JS ternary operator - learning notes (with cases)
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Graphics_ Learnopongl learning notes
- 单调栈-42. 接雨水
- Osganimation library parsing
- MySQL three logs
- [concurrent programming] working mechanism and type of thread pool
- Creation and content of mapnode -- osgearth rendering engine series (2)
- UE4 source code reading_ Bone model and animation system_ Animation process
- Unity editor expansion - controls, layouts
猜你喜欢

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

Markdown learning

22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制

Analysis of Alibaba canal principle

Servlet的生命周期

UE4 source code reading_ Bone model and animation system_ Animation compression
![[concurrent programming] explicit lock and AQS](/img/5f/a80751a68726f53d11133810f454a3.jpg)
[concurrent programming] explicit lock and AQS

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

Mortgage Calculator
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads
随机推荐
【Rust 笔记】09-特型与泛型
[concurrent programming] collaboration between threads
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Convert video to GIF
UE4 source code reading_ Bone model and animation system_ Animation compression
ES6 promise learning notes
【Rust 笔记】12-闭包
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Unity Editor Extension - Outline
[RPC] RPC remote procedure call
二进制转十进制,十进制转二进制
Es8 async and await learning notes
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
注解简化配置与启动时加载
Constraintlayout's constraintset dynamically modifies constraints
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
Markdown learning
producer consumer problem
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Creation and content of mapnode -- osgearth rendering engine series (2)