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;
}
相关推荐
Mr.朱鹏2 分钟前
JVM-GC垃圾回收案例
java·jvm·spring boot·算法·spring·spring cloud·java-ee
WJSKad123527 分钟前
【DepthPro】实战教程:单目深度估计算法详解与应用
算法
wzqllwy29 分钟前
8 大经典排序算法(Java 实现):原理 + Demo + 核心分析
java·算法·排序算法
We་ct31 分钟前
LeetCode 77. 组合:DFS回溯+剪枝,高效求解组合问题
开发语言·前端·算法·leetcode·typescript·深度优先·剪枝
重生之我是Java开发战士34 分钟前
【递归、搜索与回溯】二叉树中的深度优先搜索:布尔二叉树,求根节点到叶节点数字之和,二叉树剪枝,验证二叉搜索树,第K小的元素,二叉树的所有路径
算法·深度优先·剪枝
篮l球场34 分钟前
矩阵置零
算法
lihihi36 分钟前
P1650 [ICPC 2004 Shanghai R] 田忌赛马(同洛谷2587)
开发语言·算法·r语言
朱一头zcy37 分钟前
[牛客]BC38 变种水仙花
算法
努力学算法的蒟蒻37 分钟前
day105(3.6)——leetcode面试经典150
算法·leetcode·面试
猫猫的小茶馆39 分钟前
【Linux 驱动开发】Linux 内核启动过程详解
linux·c语言·arm开发·驱动开发·stm32·单片机·mcu