This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021″
Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!
Creation and initialization of a two-dimensional array
Creation of a two-dimensional array
Int arr[3][4]; char arr[3][5]; double arr[2][4];Copy the code
Initialization of a two-dimensional array
Int arr[3][4]= {1,2,3,4}; Int arr [3] [4] = {{1, 2}, {4, 5}}; Int arr [] [4] = {{2, 3}, {4, 5}};Copy the code
The use of two-dimensional arrays
Two-dimensional arrays are also used with subscripts. Look at the code:
int main() { int arr[3][4] = { 0 }; int i = 0; for (i = 0; i < 3; i++) { int j = 0; for (j = 0; j < 4; j++) { arr[i][j] = i * 4 + j; } } for (i = 0; i < 3; i++) { int j = 0; for (j = 0; j < 4; j++) { printf("%d ", arr[i][j]); }}Copy the code
Storage of two-dimensional arrays in memory
Like a one-dimensional array, here we try to print each element of a two-dimensional array.
#include <stdio.h>
int main()
{
int arr[3][4];
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
printf("&arr[%d][%d] = %p\n", i, j, &arr[i][j]);
}
}
return 0;
}
Copy the code
According to the results, we can analyze that the two-dimensional array is also continuously stored in memory.
Bubble sort
Reason: Each bubble sort of 10 digits can bring one element to its proper position. After 9 times of sorting, the remaining one must be in its proper position
Each trip can compare one element less than 10 elements, the first trip compares 9 pairs of numbers, the second trip compares 8 pairs…….
Void Swap(int* pa, int* pb) {int TMP = pa; *pa = *pb; *pb = tmp; } void BubbleSort(int* arr, int sz) { int i = 0, j = 0; For (I = 0; i < sz-1; I++) {// each sort can compare one less element for (j = 0; j < sz - 1 - i; J++) {/ / arranged in ascending order Namely before a than a big if (arr [j] > arr [j + 1]) {Swap (& arr [j], & arr [j + 1)); }}}} int main () {int arr [10] = {9,5,6,3,6,7,9,0,3, 2}; int sz = sizeof(arr) / sizeof(arr[0]); BubbleSort(arr, sz); int i = 0; for (i = 0; i < sz; i++) { printf("%d ", arr[i]); } return 0; }Copy the code
Optimized version
Optimized version of bubble sort:
When the first bubble sort does not swap, it is already in order
Void Swap(int* pa, int* pb) {int TMP = pa; *pa = *pb; *pb = tmp; } void BubbleSort(int* arr, int sz) { int i = 0, j = 0; For (I = 0; i < sz-1; i++) { int flag = 1; For (j = 0; for (j = 0; j < sz - 1 - i; J++) {/ / arranged in ascending order Namely before a than a big if (arr [j] > arr [j + 1]) {Swap (& arr [j], & arr [j + 1)); flag = 0; If (flag == 1) {break; if(flag == 1) {break; }}} int main () {int arr [10] = {9,5,6,3,6,7,9,0,3, 2}; int sz = sizeof(arr) / sizeof(arr[0]); BubbleSort(arr, sz); int i = 0; for (i = 0; i < sz; i++) { printf("%d ", arr[i]); } return 0; }Copy the code
If the bubble sort is to be swapped, it indicates disorder, set flag = 0,
In the next bubble sort, set flag=1 again to continue to judge whether the exchange is needed. If the exchange is not needed this time, it indicates that the exchange is in order, and break jumps out
That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!