当前位置:网站首页>Binary tree sorting (C language, char type)
Binary tree sorting (C language, char type)
2022-07-03 08:50:00 【Cap07】
#define _CRT_SECURE_NO_WARNINGS // Binary tree sorting (char type )
#include<stdio.h> // Array index from 1 Start
#include <string.h>
void move_char(char a[]) { // Will array a Move the whole backward by one bit , Give Way a[0] invalid
for (int i = strlen(a); i > 0; i--) {
a[i] = a[i - 1];
}
}
void exch_char(char a[], int i, int j) { // In exchange for a[i] and a[j] Value
char temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void swim_char(char 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_char(a, k / 2, k);
k = k / 2;
}
}
void sink_char(char 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_char(a, k, j);
k = j;
}
}
void BiTree_sort_char(char a[]) { // Binary tree sorting ,a_length Is an array a Remove a[0] The length after
int a_length = strlen(a) - 1;
for (int k = a_length / 2; k >= 1; k--) { // Make the binary tree orderly ( Descending from top to bottom )
sink_char(a, k, a_length);
}
while (a_length > 1) { // Sort ascending from top to bottom
exch_char(a, 1, a_length--);
sink_char(a, 1, a_length);
}
}
int main() {
char a[100] = {};
printf(" Please enter the string to sort :\n");
scanf("%s", a);
move_char(a); //a[0] no need
BiTree_sort_char(a);
printf("\n The result after ascending is :\n");
for (int i = 1; i <= strlen(a); i++) {
printf("%c ", a[i]);
}
printf("\n");
return 0;
}
test result :
边栏推荐
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
- Annotations simplify configuration and loading at startup
- Advanced OSG collision detection
- MySQL index types B-tree and hash
- Convert video to GIF
- LinkedList set
- Unity Editor Extension - drag and drop
- 数据库原理期末复习
- 【Rust 笔记】09-特型与泛型
- Deep parsing (picture and text) JVM garbage collector (II)
猜你喜欢
Gif remove blank frame frame number adjustment
Really explain the five data structures of redis
UE4 source code reading_ Mobile synchronization
二进制转十进制,十进制转二进制
Unity Editor Extension - drag and drop
[concurrent programming] thread foundation and sharing between threads
Markdown learning
First Servlet
[rust notes] 02 ownership
Unity editor expansion - scrolling list
随机推荐
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Query XML documents with XPath
[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
[rust note] 10 operator overloading
Log4j2 vulnerability recurrence and analysis
Location of package cache downloaded by unity packagemanager
Unity editor expansion - controls, layouts
UE4 source code reading_ Bone model and animation system_ Animation process
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
cres
MySQL 8
redis集群系列四
UE4 source code reading_ Bone model and animation system_ Animation compression
How to deal with the core task delay caused by insufficient data warehouse resources
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Constraintlayout's constraintset dynamically modifies constraints
Drawing maze EasyX library with recursive backtracking method
Cesium for unreal quick start - simple scenario configuration
VIM learning notes from introduction to silk skating