Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language sorts integer arrays by segments, with even numbers before and odd numbers after, from small to large.
C language sorts integer arrays by segments, with even numbers before and odd numbers after, from small to large.
# Contains? & ltstdio.h & gt

# Contains? & ltconio.h & gt

# Definition? Ryan. 100? /* Maximum array length */

# Definition? elemType? int? /* Element type */

/* Input array */

Invalid? inputArr? (elemType? arr[],? int? len)? {

int? Me;

For what? (I = 0; ? Me & ltlen? i++)

scanf? (" %d ",& amparr[I]);

putchar? (' \ n ');

}

/* Sort by bubbling ascending order */

/* Parameter Description: */

/*int? Arr[]: sort target array */

/*int? Len: number of elements */

Invalid? bubbleSortAsc? (elemType? arr[],? int? len)? {

elemType? Temperature;

int? Me? j;

For what? (I = 0; ? I< len-1; ? i++)? /* The outer loop controls the sorting times, and the number of len- 1 is len- 1 */

For what? (j = 0; j & ltlen- 1-I; ? j++)? {? /* The number of comparisons for each pass of the inner loop, and the j-th comparison len-j */

What if? (arr[j]? & gt? arr[j+ 1])? {? /* Compare adjacent elements and exchange the reverse order */

Temporary workers? =? arr[j];

arr[j]? =? arr[j+ 1];

arr[j+ 1]? =? Temperature;

}

}

}

/* Print array */

Invalid? printArr? (elemType? arr[],? int? len)? {

int? Me;

For what? (I = 0; ? Me & ltlen? i++)

printf? (" %d\t ",arr[I]);

putchar? (' \ n ');

}

int? Mainly? (void)? {

elemType? arr[LEN];

elemType? arrod[LEN],? arr even[LEN]; ? /* Store odd and even numbers */?

int? Ryan. Odlum? evenLen

int? Me? j? ,k;

printf? ("Please enter array length:");

scanf? (" %d ",& amplen);

printf? ("Please enter array contents: \ n");

inputArr? (arr,? len);

/* Divide parity first, then sort separately, and finally merge */

For what? (i=0,? j=0,? k = 0; ? Me & ltlen? i++)? {

What if? (arr[i]%2)? {

arrod[j]? =? arr[I];

j++;

}

Or what? {

arrEven[k]? =? arr[I];

k++;

}

}

Odlum? =? j;

evenLen? =? k;

bubbleSortAsc? (arrOdd,? Odlum);

bubbleSortAsc? (Arvin,? even len);

For what? (k=0,? I = 0; ? k & ltevenLen? k++、? i++)

arr[i]? =? arrEven[k];

For what? (j = 0; ? J< odlum; ? j++、? i++)

arr[i]? =? arr odd[j];

printArr? (arr,len);

putchar? (' \ n ');

getch? (); ? /* Screen Persistence */

Free? (arr);

Return? 0;

} running results

Note: The input array part is omitted from the running results.