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;
}
相关推荐
小柯博客3 小时前
从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(十二)
c语言·stm32·单片机·嵌入式硬件·php·嵌入式
C++ 老炮儿的技术栈4 小时前
UDP 与 TCP 的区别是什么?
开发语言·c++·windows·算法·visual studio
殇者知忧4 小时前
【论文笔记】若干矿井粉尘检测算法概述
深度学习·神经网络·算法·随机森林·机器学习·支持向量机·计算机视觉
mochensage6 小时前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
chengooooooo6 小时前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
GUIQU.6 小时前
【每日一题 | 2025年6.2 ~ 6.8】第16届蓝桥杯部分偏简单题
算法·蓝桥杯·每日一题
weixin_527550407 小时前
初级程序员入门指南
javascript·python·算法
乄夜7 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
嘉陵妹妹9 小时前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon9 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展