C language array, array

Source: Internet
Author: User

C language array, array


1. array introduction:

An array is a set of elements of the same data type arranged in a certain order. It is to name a variable of the same type with a name, and then identify the set of their variables with a number, this name is called the array name and the number is called the subscript. Each variable that makes up an array is called the component of an array, also known as an element of an array, or a subscript variable. Arrays are a form of organizing several variables of the same type in order to facilitate processing in programming. The collections of these sort-by-order data elements are called arrays.

2. Example requirements:
For the most basic data structure of arrays, list the operations supported by this data structure, and analyze the time complexity of each operation in the big O mode.

3. Implementation Process:

# Include <QCoreApplication>
// Basic operations on the array, sorting, taking the maximum value, fuzzy query of a value

// [1] array Sorting Algorithm
// 1.1, bubble sort O (n2)
The best result is the order of the array: O (2N ),

The average is a normal random out of order: O (2N ),

The worst is that the data is completely out of order O (n2 ).

Void BubbleSort (int A [], int N)

{

Int tmp;
For (I = 0; I <N-1; ++ I)
{
For (j = 1; j <N-I; ++ j)
{
If (A [j]> A [j-1]) // sort the values from large to small, and swap the smaller values to the back.
{
Tmp = A [j-1];
A [j-1] = A [j];
A [j] = tmp;
}
}
}
}




// 1.2 fast sorting. Its average time complexity is O (Nlog2N)
The best thing is that the time complexity in the case of array order is: O (nlogn ),
The average time complexity is O (nlogn ). ,
The worst is that the data is completely out of order O (n ^ 2 ).
Int Partition (int * arr, int low, int high)
{
Int lower = arr [low];
While (low {
While (low Arr [low] = arr [high];
While (low Arr [high] = arr [low];
}
Arr [low] = slow;
Return low;
}


Void Qsort (int * arr, int low, int high)
{
If (low {
Int mid = Partition (arr, low, high );
Qsort (arr, low, mid-1 );
Qsort (arr, mid + 1, high );
}
}


// [2] maximum value Algorithm for Array
In the best case, the maximum value is the first, and the time complexity is: O (n ^ 2)
Mean is, the maximum value is in the middle, time complexity: O (n ).
Worst case: the maximum value is at the end. Time Complexity: O (2 ^ n)
Int max (int array [], int n );
Void main ()
{
Int num [N], count, I, val;
? Scanf ("% d", & count );
For (I = 0; I <count; I ++)
{
Scanf ("% d", & num [I]);
}
Val = max (num, count );
Printf ("% dn", val );
}

Int max (int array [], int n)
{
If (n <= 1)
Return (array [0]); // It is a number, and the maximum value is your own
Int t = max (array + 1, n-1); // calculate the maximum number of N-1 numbers
If (t> array [0]) // t is greater than the first, returns the maximum t
Return (t );
Else
Return (array [0]); // t small, returns array [0];
}

// [3] whether an array query contains a value
In the best case, it is the first, with the time complexity of O (1 );
On average, the value is in the middle, and the time complexity is 0 (N/2 );
In the worst case, the value ends or does not exist. The time complexity is O (N );
Int main ()
{
Int n, m, I, a [20];
Int find;
Scanf ("% d", & n );
For (I = 0; I <n; I ++ ){
Scanf ("% d", & a [I]);
}
Scanf ("% d", & m );
Find =-1;
For (I = 0; I <n; I ++ ){
If (a [I] = m ){
Find = I;
Break;
}
}

If (find <0 ){
Printf ("Non ");
}
Else {
Printf ("% dn", find );
}
Return 0;
}


// [4] array input and output. I didn't think of any Algorithm for sequential traversal at the moment?

Bytes ----------------------------------------------------------------------------------------------------------------
<All Rights Reserved. This document can be reprinted, but the source address must be indicated by link. Otherwise, we will be held legally responsible.>
Original blog address: http://blog.itpub.net/26230597/viewspace-1386607/
Original Author: Huang Shan (mchdba)
Bytes ----------------------------------------------------------------------------------------------------------------

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.