Compared with the content of the previous day, today's content is relatively simple. The VT control code is mainly used in three sorting algorithms: Select sorting, Bubble sorting, and fast sorting. Binary Search. A Brief Introduction to traversing Binary Trees is also provided to POSIX. System Call function: Use of Open Close read write lseek fstat.
According to Liu, the VT control code was prepared for small projects in the future. For example, snake and Tetris. Currently, you only need to know the basic operations. It mainly controls the optical mark. All VT100 controllers are headers of/033 (that is, esc ascii code) and are output using output character statements. The specific format can be either of the following numeric forms:/033 [<number> M. for example, "/33 [40 m" indicates that the output of subsequent characters is output in black background/33 [0 m indicates that the previous settings are canceled. The foreground (Character Color) and background color must be output at the same time. The other is the control character form. That is, the last character is not m, but a control character. /033 [k clear content from the cursor to the end of the line/033 [the NC cursor shifts n rows right.
If you select sorting or Bubble sorting, the two sorting types are not mentioned. Focus on quick sorting. First, the concepts of time complexity and space complexity are popularized. The time complexity is measured when the algorithm is executed.TimeSpace complexity is a measure of the size of storage space temporarily occupied by an algorithm during operation.
Quick sorting uses recursion to divide the content to be sorted by an axis into two parts. Put one side bigger than this axis and the other side smaller. Then place the axis on both sides. The Code is as follows:
1 #include<stdio.h> 2 3 void quick(int *,int ,int ); 4 int main() 5 { 6 int arr[8]={3,4,5,2,6,2,9,5}; 7 8 quick(arr,0,7); 9 10 int i = 0;11 for(i=0;i<8;i++) 12 printf("%d\n",arr[i]);13 14 }15 void quick(int *arr,int low,int high){16 int i,j,pivot;17 if(low >high) 18 return ;19 pivot = arr[low];20 i = low;21 j = high;22 while(i < j){23 while(i<j && arr[j]>pivot)24 j--;25 if(i < j)26 arr[i++] =arr[j];27 while(i<j && arr[i]<=pivot)28 i++;29 if(i < j)30 arr[j--] =arr[i];31 }32 33 arr[i] = pivot;34 quick(arr,low,i-1);35 quick(arr,i+1,high);36 37 }
The binary search and traversal of Binary Trees are simple code. I will not record it. Mainly POSIX standard. About the functions called by the system. This is the core of our next class.
POSIX indicates the portable operating system interface (POSIX), which defines the interface standard that the operating system should provide for applications.
Because I learned how to operate C files the other day. The file operations in C are the encapsulation of the read, write, open, and close lseek functions. So there is basically no difference in use. When calling system functions, the key is to read the content in the Data Manual. In particular, function functions, parameters, and return values. The following code uses several functions:
1 #include<fcntl.h> 2 #include<stdio.h> 3 #include<sys/stat.h> 4 5 int main() 6 { 7 int fp = open("hello",O_RDWR | O_CREAT,0664); 8 if(fp <0){ 9 perror("open");10 return 1;11 }12 char *buf = "hello bunfly";13 int ha = write(fp,buf,12);14 if(ha < 0){15 perror("write");16 return 1;17 }18 lseek(fp,0,SEEK_SET);19 20 unsigned char data[1024] = {0};21 int ret = read(fp,data,1024);22 if(ret < 0){23 perror("read");24 return 1;25 }26 printf("data is %s\n",data);27 struct stat st;28 ret = fstat(fp,&st);29 if(ret < 0){30 perror("fstat");31 return 1;32 }33 printf("len is %d\n",st.st_size);34 close(fp);35 }
19th days: VT control code and Data Structure