About the insertion and deletion of the array and sort

xiaoxiao2021-03-06  85

Insertion of arrays:

#include

#define size 10

int main ()

{

INT A [size] = {10, 12, 14, 16, 18, 20, 13, 200, 134, 59}; / * Initialization array * /

INT B [Size 1] = {0}, i, j, x, v; / * B array is an array after insertion, which is newly inserted into the original number group, add 1 * /

Printf ("Please Input INSERT AddR:");

Scanf ("% D", & x); / * Insert value location * /

PRINTF ("Please Input INSERT VALUE:");

Scanf ("% d", & v); / * insert value * /

For (i = 0; i <= size-1; i ) {

IF (i == x) {/ * loop array A, when the position of the array A element value is equal to the position of the insert value * /

B [i] = v; / * assigns the insert value to the array and the number of groups of array a-element value I * /

B [i 1] = a [i]; / * assigns a value of the I position in the original array A. * /

}

If (i> x) b [i 1] = a [i]; / * When I is greater than the position X of the insert value, the position placed in each element is retired later.

* /

IF (i

}

Printf ("This Array IS: N");

For (j = 0; j <= size; j )

Printf ("% 5d", B [J]); / * Print array * /

Printf ("n");

Return 0;

}

Array deletion:

#include

#define size 10

int main ()

{

INT A [size] = {10, 12, 14, 16, 18, 20, 13, 200, 134, 59};

INT B [Size-1] = {0}, I, J, X, V;

Printf ("Please Input INSERT AddR:");

Scanf ("% D", & x);

For (i = 0; i <= size-1; i ) {

IF (i == x) b [i-1] = a [i];

IF (i> x) b [i-1] = a [i];

IF (i

}

Printf ("This Array IS: N");

For (j = 0; j <= size-2; j )

Printf ("% 5d", B [J]);

Printf ("n");

Return 0;

}

The insertion of the array is substantially different. When the element is inserted, each element behind the insertion point is moved backward, and the deletion is moving forward.

Sort by array (foam sorting method):

/ * This program is rolled by the value of the array * /

#include

#define Size 15

Main ()

{

INT A [size] = {900, 2, 3, 58, 34, 76, 32, 43, 56, 70, 35, 234, 532, 543, 2500};

INT I, Pass, HOLD, J = 0, K = 0;

Printf ("Data Items in ORAGINALN");

For (i = 0; i <= size-1; i ) / * array before printing * /

Printf ("% 6d", A [i]);

For (Pass = 1; Pass <= size-1; pass ) {/ * comparison * /

For (i = 0; i <= size- (pass 1); i ) {/ * compare, note that the conditions here are Size- (Pass 1) * /

IF (a [i]> a [i 1]) {

HOLD = a [i];

a [i] = a [i 1];

a [i 1] = HOLD;

}

}

}

Printf ("NData Items In Ascending ORDERN);

For (i = 0; i <= size-1; i ) / * Multiple groups after printing * /

Printf ("% 6d", A [i]);

Printf ("n");

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-124041.html

New Post(0)