思维导图:
写一个函数,获取用户的uid和gid并使用变量接收:
#!/bin/bash
function get_uid
{
my_uid=`id -u`
my_gid=`id -g`
}
get_uid
echo "当前用户的UID:$my_uid"
echo "当前用户的GID:$my_gid"
整理冒泡排序、选择排序和快速排序的代码
快速排序:
#include<myhead.h>
//一次排序函数
int one_sort(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 quick_sort(int arr[], int low, int high){
if(low < high){
int mid = one_sort(arr, low, high);
//中间值右侧递归排序
quick_sort(arr, low, mid-1);
//中间值左侧递归排序
quick_sort(arr, mid+1, high);
}
}
int main(int argc, const char *argv[])
{
int arr[]={1,4,5,7,3,12,99,45,2,77};
int len = sizeof(arr)/sizeof(arr[0]);
quick_sort(arr, 0, len-1);
for(int i=0; i < len; i++){
printf("%d\t", arr[i]);
}
putchar(10);
return 0;
}
冒泡排序:
#include<myhead.h>
void bub_sort(int arr[], int len){
int temp = 0;
int count = 0;
for( int i = 0; i < len/2; i++){
for( int j = i+1; j < len-i; j++){
if( arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main(int argc, const char *argv[])
{
int arr[] = {12, 26, 24, 8, 16, 1, 99};
int len = sizeof(arr)/sizeof(arr[0]);
bub_sort(arr, len);
for(int i=0; i < len; i++){
printf("%d\t", arr[i]);
}
putchar(10);
return 0;
简单选择排序:
#include<myhead.h>
void select_sort(int arr[], int len){
int temp = 0;
for( int i = 0; i < len-1; i++){
int min=i;
for( int j = i+1; j < len; j++){
if(arr[min] > arr[j]){
min = j;
}
}
if(min != i){
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
int main(int argc, const char *argv[])
{
int arr[] = { 12, 15, 99, 26, 28, 8, 24};
int len = sizeof(arr)/sizeof(arr[0]);
select_sort(arr, len);
for(int i=0; i < len; i++){
printf("%d\t", arr[i]);
}
putchar(10);
return 0;
}