One-dimensional arrays, array sorting, character arrays

Source: Internet
Author: User
Tags array sort

1. One-dimensional arrays

Arrays are constructed data types

Distinguish between array elements and arrays (int a[10] a[10])

The subscript must be guaranteed not to cross the border

An array is a whole and cannot be directly involved in an operation, only a single element can be processed.

2. Array sort (bubble sort)

int a[7] = {3, 1, 7, 23, 12, 87, 2};

The outer loop controls the number of trips to compare, and n elements need to compare n-1 times

for (int i = 0; i < 6; i++) {

Inner loop control in each trip to compare the number of times, each come in one less comparison

for (int j = 0; J < 6-i; J + +) {

If the previous element is larger than the latter, the swap position

if (A[j] < A[j + 1]) {

int temp = A[j];

A[J] = a[j + 1];

A[j + 1] = temp;

}

}

}

Print a sorted array

for (int i = 0; i < 7; i++) {

printf ("%d", a[i]);

}

Select sort

int a[5] = {7, 4, 6, 2, 1};

int k = 0;

for (int i = 0; i < 4; i++) {

K = i;//assumes that the a[i] element is the minimum value

for (int j = i + 1; j < 5; J + +) {

if (A[k] > A[j]) {

K = J;

}

}

if (i! = k) {

int temp = A[i];

A[i] = a[k];

A[K] = temp;

}

}

for (int i = 0; i < 5; i++) {

printf ("%-3d", A[i]);

}

3, character array

Char c[] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e '};//length of 6

C language string ends with a

Char a[6] = "iPhone";//Length of 7 ' + '

unsigned long result = strlen (a);//The calculation is not required to meet the end of the

unsigned long s = sizeof (a);//sizeof () Memory size

printf ("%s", a);

The space occupied by the string is at least 1 larger than the string length, because the string ends with '/'. The string handler provided by the system determines whether the string ends according to '% '.

printf ("%ld", result);

printf ("%ld", s);

Strlen () Calculate string? length

strcpy () string copy shell

strcat () string concatenation

strcmp () string? comparison

One-dimensional arrays, array sorting, character arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.