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

边栏推荐
- Log4j2 vulnerability recurrence and analysis
- Unity Editor Extension - event handling
- [concurrent programming] working mechanism and type of thread pool
- [concurrent programming] thread foundation and sharing between threads
- LinkedList set
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- 【Rust 笔记】11-实用特型
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Unity multi open script
- 注解简化配置与启动时加载
猜你喜欢

Parameters of convolutional neural network

单调栈-503. 下一个更大元素 II

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

100 GIS practical application cases (78) - Multi compliance database design and data warehousing

too many open files解决方案

MySQL three logs

Installation of PHP FPM software +openresty cache construction

VIM learning notes from introduction to silk skating

UE4 source code reading_ Bone model and animation system_ Animation compression

22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
随机推荐
Binary to decimal, decimal to binary
[rust notes] 07 structure
Unity editor expansion - draw lines
分配异常的servlet
Constraintlayout's constraintset dynamically modifies constraints
Es8 async and await learning notes
[concurrent programming] Table hopping and blocking queue
Unity Editor Extension - drag and drop
Alibaba canaladmin deployment and canal cluster Ha Construction
[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
Log4j2 vulnerability recurrence and analysis
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Unity editor expansion - scrolling list
796 · unlock
了解小程序的笔记 2022/7/3
Monotonic stack -84 The largest rectangle in the histogram
Cesium for unreal quick start - simple scenario configuration
JS ternary operator - learning notes (with cases)
Query XML documents with XPath
【Rust 笔记】13-迭代器(上)