Week Seven: Array Operations
C language learning notes
This series of notes is to learn the complex disc Mu class teacher Weng Kai Zhejiang University, "Introduction to Programming Language -C" Note-taking and some of their own conclusion. (Part of the screenshot of the article from the course video screenshot)
Courses link: https: //www.icourse163.org/learn/ZJU-199001 tid = 1206771253 # / learn / content?
7.1 Array Operations
7.1.1 Array Operations
Array is widely used in the search. The search is now a very important application.
- Integrated array initialization:
We obtained the array a [0] = 2, the other 12 digits automatic zero-fill.
- So, if we want an array initialized to all 0 , we can actually go do not cycle, direct
Cnt [ number ] = {0} ; will all initialized to 0 of
- Positioning the integrated initialization
EG2: Int a[10]={ [0]=2, [2]=3,6,};
It is to give a [ 0 ] assigned a 2 ; a a [ 2 ] of assignment 3 ; to a [ 3 ] of assignment 6, the remaining complement to 0.
- With n given positioned in the initialized data
- No data is positioned behind the contact position in front of
- Value of zero padding other locations
- May not be given array size, let the compiler count
- Particularly suitable for sparse array initialization data
- How to get the size of the array?
Sizeof how many bytes the resulting array occupies.
Results after% lu for displaying sizeof
Sizeof (a) / sizeof (a [0]) is an array element occupies
Benefits: Once the initial array data to modify, without modifying the code traversed.
2. array assignment
Not int b [] = a
Array variable itself can not be assigned; put all the elements of an array to another array, you must traverse.
This is the only way to put an array assigned to another array, no other way.
3. The array passed to the function
- An array as a parameter, is given in [] is not always the size of the array, write only A [] ; (as shown in the screenshot below)
- Inside the function can not be utilized sizeof number of elements in the array to calculate the
- We often need to calculate the size of the array is good in the main function with the other parameters, it passed into the function. (As in FIG. Length)
7.1.2 Array Example: How to tell if a number is not a prime number and establish a prime table? Three ways.
method one:
Method two: only need to test more than x small prime numbers, it can not divisible; established using the prime number table array
Method three: prime number table configured
Fake code:
Two small questions about whether the subscript is out of bounds:
7.2 Search
7.2.1 Linear Search
Very important! ! !
Basic methods: traversing (that is called linear search)
7.2.2 Examples of search
A data structure: a hash table hash table
构造两个数组,我们在第一个数组中用一个值10得到下标是2,再用这个下标去第二个数组中得到我们想要的结果也就是:dime
割裂的数组有什么问题?
这种程序的结构对于Cache 不友好。
我们希望把面额和名字放在一起。
7.2.3二分搜索
线性搜索最大的问题是它的效率问题。
二分搜索的前提:排好序
二分搜索最大的好处是:效率。 次数: log2N
1000个东西 log2(1000)=10
7.3排序初步
7.3.1选择排序
如果数据是无序的怎么办?
用选择排序的算法来对它进行排序。
第一步:找出最大
第二步:交换 swap a[maxid]和 a[len-1]
选择排序是用来将无序的数组变为有序的数组。