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

边栏推荐
- Explain sizeof, strlen, pointer, array and other combination questions in detail
- Unity Editor Extension - Outline
- Monotonic stack -84 The largest rectangle in the histogram
- Markdown learning
- Alibaba canal actual combat
- On the difference and connection between find and select in TP5 framework
- producer consumer problem
- [rust notes] 11 practical features
- TP5 multi condition sorting
- Chocolate installation
猜你喜欢

Monotonic stack -42 Connect rainwater

请求参数的发送和接收

Unity interactive water ripple post-treatment

MySQL three logs

Es8 async and await learning notes

XPath实现XML文档的查询

Advanced OSG collision detection

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

VIM learning notes from introduction to silk skating

OpenGL learning notes
随机推荐
Intersectionpicker in osgearth
Parameters of convolutional neural network
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
[concurrent programming] collaboration between threads
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Sending and receiving of request parameters
[concurrent programming] explicit lock and AQS
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Allocation exception Servlet
数据库原理期末复习
Unity Editor Extension - event handling
cres
UE4 source code reading_ Bone model and animation system_ Animation process
Baidu editor ueeditor changes style
Unity Editor Extension - drag and drop
DOM 渲染系统(render mount patch)响应式系统
Monotonic stack -42 Connect rainwater
Deep parsing (picture and text) JVM garbage collector (II)
【Rust 笔记】11-实用特型