C高级第四讲

1、思维导图

2、写一个shell函数,获取用户的uid和gid并使用变量接收

bash 复制代码
#!/bin/bash
function get_id()
{
	uid=`id -u ubuntu`
	gid=`id -g ubuntu`
}
get_id
echo "uid:$uid"
echo "gid:$gid"

运行结果

3、排序

冒泡排序

cpp 复制代码
/* 
   ---------------------------------
    @author:YoungZorn
    created on 2023/8/7 19:01.
   ---------------------------------
*/
#include<iostream>

using namespace std;

void BubbleSort(int *arr,int n){
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j]>arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

void output(int *arr,int n){
    for (int i = 0; i < n; i++) {
        cout<<arr[i]<<" ";
    }
}

int main(){
    int arr[] = {56,78,12,23,43,90,51,78};
    int len = sizeof(arr)/ sizeof(int);
    BubbleSort(arr,len);
    output(arr,len);
    return 0;
}

选择排序

cpp 复制代码
/* 
   ---------------------------------
    @author:YoungZorn
    created on 2023/8/7 19:06.
   ---------------------------------
*/
#include<iostream>

using namespace std;

void SelectSort(int *arr,int n){
    for (int i = 0; i < n - 1; i++) {
        for (int j = i+1; j < n; j++) {
            if (arr[i] > arr[j]){
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

void output(int *arr,int n){
    for (int i = 0; i < n; i++) {
        cout<<arr[i]<<" ";
    }
}

int main(){
    int arr[] = {12,32,42,54,68,23,85,90,80};
    int len = sizeof(arr)/sizeof(int);
    SelectSort(arr,len);
    output(arr,len);
    return 0;
}

快速排序

cpp 复制代码
/* 
   ---------------------------------
    @author:YoungZorn
    created on 2023/8/7 19:09.
   ---------------------------------
*/
#include<iostream>

using namespace std;

int OneSort(int *arr,int low,int high){  //获取基准值
    int key = arr[low];
    while (low < high){
        while (low < high && key <= arr[high]){
            high--;
        }
        arr[low] = arr[high];
        while (low < high && key >= arr[low]){
            low++;
        }
        arr[high] = arr[low];
    }
    arr[low] = key;
    return low;
}

void QuickSort(int *arr,int low,int high){
    if(low >= high){
        return;
    }
    int mid = OneSort(arr,low,high);
    QuickSort(arr,0,mid-1);
    QuickSort(arr,mid+1,high);
}

void output(int *arr,int n){
    for (int i = 0; i < n; i++) {
        cout<<arr[i]<<" ";
    }
}

int main(){
    int arr[] = {34,12,32,87,52,74,68,30};
    int len = sizeof(arr)/ sizeof(int);
    QuickSort(arr,0,len-1);
    output(arr,len);
    return 0;
}
相关推荐
pzx_0011 分钟前
【LeetCode】14. 最长公共前缀
算法·leetcode·职场和发展
self_myth2 分钟前
算法与数据结构实战技巧:从复杂度分析到数学优化
算法
songx_9936 分钟前
leetcode10(跳跃游戏 II)
数据结构·算法·leetcode
Yuki’41 分钟前
网络编程---UDP
c语言·网络·网络协议·udp
.YM.Z1 小时前
C语言——文件操作
c语言·文件操作
先做个垃圾出来………2 小时前
差分数组(Difference Array)
java·数据结构·算法
hansang_IR2 小时前
【题解】洛谷 P4286 [SHOI2008] 安全的航线 [递归分治]
c++·数学·算法·dfs·题解·向量·点积
乐迪信息2 小时前
乐迪信息:AI摄像机在智慧煤矿人员安全与行为识别中的技术应用
大数据·人工智能·算法·安全·视觉检测
GanGuaGua2 小时前
Linux系统:线程的互斥和安全
linux·运维·服务器·c语言·c++·安全
多恩Stone2 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc