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

边栏推荐
- Osgearth topographic shading map drawing
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Unity learning notes
- MySQL containerization (1) docker installation MySQL
- Transmit pictures with Base64 encoding
- Deeply understand the underlying data structure of MySQL index
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Markdown learning
- Osgearth target selection
猜你喜欢

Notes on understanding applets 2022/7/3

Monotonic stack -84 The largest rectangle in the histogram

Parameters of convolutional neural network

Mortgage Calculator

UE4 source code reading_ Bone model and animation system_ Animation compression

Unity Editor Extension - drag and drop

Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)

Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation

Graphics_ Learnopongl learning notes

Collection interface
随机推荐
Unity learning notes
Really explain the five data structures of redis
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Query XML documents with XPath
【Rust 笔记】07-结构体
Vscode, idea, VIM development tool shortcut keys
JS ternary operator - learning notes (with cases)
[rust note] 10 operator overloading
[rust notes] 06 package and module
UE4 source code reading_ Bone model and animation system_ Animation process
TP5 order multi condition sort
Unity editor expansion - the framework and context of unity imgui
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
[concurrent programming] atomic operation CAS
Installation of PHP FPM software +openresty cache construction
Downward compatibility and upward compatibility
First Servlet
Development material set
Concurrent programming (III) detailed explanation of synchronized keyword
使用dlv分析golang进程cpu占用高问题