Programming Algorithm-code for sorting arrays by absolute values (c)

Source: Internet
Author: User
Code for sorting arrays by absolute value (c)


Address: http://blog.csdn.net/caroline_wendy


The sorted array may contain positive and negative values. You can use the semi-query to determine the position of the median, and then use two pointers to sort the two ends in sequence.

The solution is:

1. The elements in the array are all positive and return;

2. The elements in the array are all negative and return;

3. If there is a positive number and a negative number in the array, you can use the binary lookup Method to Determine the symbols of the intermediate element.

A) if the intermediate element is positive, continue to judge the symbol of the element before the intermediate element;

B) if the intermediate element is negative, judge the symbol of the next element of the intermediate element;

C) The intermediate element is zero, so that it is equal to the returned result value;


Time complexity O (nlogn)

Code:

/* * main.cpp * *  Created on: 2014.9.12 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include <stdio.h>#include <stdlib.h>using namespace std;void Reverse(int* begin, int* end) {while (begin < end) {int tmp = *begin;*begin++ = *end;*end-- = tmp;}}void AbsoluteSort(int res[], int data[], const int length) {if (data == NULL || length <= 0) {res = data;return;}if ((data[0]<0 && data[length-1]<0) || (data[0]>0 && data[length-1]>0)) {res = data;return;}int left = 0;int right = length-1;int mid;while (left < right) {mid = left + ((right-left)>>1);if (data[mid]>0 && data[mid-1]<=0) {right = mid; left = mid-1;break;} else if (data[mid]>0) {right = mid;} else if (data[mid]<0 && data[mid+1]>=0) {left = mid; right = mid+1;break;} else if (data[mid]<0) {left = mid;} else {break;}}int num=0;while (right < length && left >=0) {if (abs(data[right]) <= abs(data[left])) {res[num++] = data[right++];} else {res[num++] = data[left--];}}while (right < length)res[num++] = data[right++];while (left >=0 ) {res[num++] = data[left--];}}int main (void) {int data[] = {-5, -4, -2, -1, 0, 1, 2, 6};const int length = sizeof(data)/sizeof(data[0]);int res[length];AbsoluteSort(res, data, length);for (int i=0; i<length; ++i) {printf("%d ", res[i]);}printf("\n");}


Output:

0 1 -1 2 -2 -4 -5 6 





Programming Algorithm-code for sorting arrays by absolute values (c)

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.