Sorting by category

Source: Internet
Author: User

An array contains three types of numbers: Negative, zero, and positive. You must scan the array only once, that is, divide the array into three parts: Negative, zero, and positive. I tried to write the code.


# Include <stdio. h ># define N 9 void swap (int * p, int * q) {int temp; temp = * p; * p = * q; * q = temp; return ;} int main () {int array [N] = {-1,-3, 0,-2, 2,-2, 0, 1}; int * p_neg, * p, * p_pos; for (p_neg = & array [0], p = & array [0], p_pos = & array [N-1]; p <= p_pos; p ++) {if (* p <0) {swap (p, p_neg); p_neg ++;} if (* p> 0) {swap (p, p_pos); p_pos --; p --;}};}


This article is from the "gifted birds and eggs" blog, please be sure to keep this source http://curely.blog.51cto.com/1627940/1297308

Related Article

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.